Update AWSS3 with Cocoapods

566 views Asked by At

In project I have two frameworks: AWSRuntime & AWSS3 and I use it to upload images.

- (void)updateAWSCredentials:(NSDictionary *)AWSObject {
    if(AWSObject && [AWSObject isKindOfClass:[NSDictionary class]]) {
        self.key = AWSObject[@"AccessKeyId"];
        self.secret = AWSObject[@"SecretAccessKey"];
        self.token = AWSObject[@"SessionToken"];
        self.profileImagePath = AWSObject[@"FilePrefixProfile"];
        self.postImagePath = AWSObject[@"FilePrefixPost"];
        self.bucket = AWSObject[@"BucketName"];
    }
}

- (NSString *)uploadImageObject:(HAAmazonImageContainer *)imageObject {
    NSData *imageData = UIImageJPEGRepresentation(imageObject.image, 1.0);

    NSString *imageKey = [NSString stringWithFormat:@"%@_%f", imageObject.userId, [[NSDate date] timeIntervalSince1970]];
    imageKey = [imageKey stringByReplacingOccurrencesOfString:@"." withString:@"0"];

    NSString *imagePath = self.postImagePath;
    if(imageObject.imagePath != HAAmazonPostImagePath) {
        imagePath = self.profileImagePath;
    }

    @try{
        // Create the S3 Client.
        AmazonCredentials *lCredentials = [[AmazonCredentials alloc] initWithAccessKey:self.key
                                                                         withSecretKey:self.secret
                                                                     withSecurityToken:self.token];

        AmazonS3Client *s3 = [[AmazonS3Client alloc] initWithCredentials:lCredentials];
        S3PutObjectRequest *por = [[S3PutObjectRequest alloc] initWithKey:[NSString stringWithFormat:@"%@/%@.jpeg", imagePath, imageKey]
                                                                 inBucket:self.bucket];
        por.contentType = @"image/jpeg";
        por.cannedACL   = [S3CannedACL publicRead];
        por.data        = imageData;
        por.delegate    = self;
        s3.endpoint = [AmazonEndpoints s3Endpoint:US_WEST_2];
        [s3 putObject:por];
    }
    @catch (AmazonClientException *exception) {
        NSLog(@"exception");
    }
    return [NSString stringWithFormat:@"http://%@/%@", self.bucket, [NSString stringWithFormat:@"%@/%@.jpeg", imagePath, imageKey]];
}

-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response {
    NSLog(@"complete with response");
    if([request isKindOfClass:[S3PutObjectRequest class]]) {
        S3PutObjectRequest *requestObj = (S3PutObjectRequest *)request;

        NSString *key = @"";
        NSArray *keys = [requestObj.key componentsSeparatedByString:@"/"];
        if(keys.count == 2) {
            key = keys[1];
        }
        NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:key, IMAGE_KEY, nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:kHAAmazonDidUploadImage object:nil userInfo:userInfo];
    }
}

Now I want to move to Cocoapods. I've imported AWSS3 with pod 'AWSS3' and I get an error:

"Cannot find protocol declaration for 'AmazonServiceRequestDelegate'"

Where I get find AmazonServiceRequestDelegate declaration or is it deprecated?

1

There are 1 answers

1
Yosuke On BEST ANSWER

You are using the version 1 of the AWS Mobile SDK for iOS. We officially started supporting CocoaPods with the version 2 of the SDK, and AWSS3 is used to pull down only the version 2 of the SDK. You cannot use CocoaPods to install the AWS Mobile SDK until you migrate your app to use the AWS Mobile SDK for iOS v2.

AWS Mobile SDK for iOS Developer Guide may help you get started with the version 2 of the SDK. Please note that it does not have backward compatibility with the version 1 of the SDK.