使用 Cocoapods 更新 AWSS3

Update AWSS3 with Cocoapods

在项目中我有两个框架:AWSRuntime 和 AWSS3,我用它来上传图片。

- (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];
    }
}

现在我想转到 Cocoapods。我使用 pod 'AWSS3' 导入了 AWSS3,但出现错误:

"Cannot find protocol declaration for 'AmazonServiceRequestDelegate'"

我在哪里可以找到 AmazonServiceRequestDelegate 声明或者它是否已弃用?

您正在使用 iOS 的 AWS Mobile SDK 版本 1。我们正式开始支持版本2的CocoaPods SDK,AWSS3用于只下拉版本2的SDK。在迁移您的应用程序以使用适用于 iOS v2.

的 AWS 移动开发工具包之前,您无法使用 CocoaPods 安装 AWS 移动开发工具包

AWS Mobile SDK for iOS Developer Guide 可以帮助您开始使用 SDK 版本 2。请注意,它与 SDK 版本 1 不具有向后兼容性。