jeudi 4 juin 2015

Update AWSS3 with Cocoapods

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?




Aucun commentaire:

Enregistrer un commentaire