如何在iOS中将图像上传到AWS S3?

时间:2015-11-21 21:17:14

标签: ios amazon-web-services amazon-s3

我使用UIImagePicker在我的应用中选择或拍照。选择图像后,我需要将其上传到AWS S3。我做了以下步骤,

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
    self.imgView.image = chosenImage;
    imageUrl = info[UIImagePickerControllerReferenceURL];
    NSString * timestamp = TimeStamp;
    NSString *UUID = [[NSUUID UUID] UUIDString];

    fileName = [NSString stringWithFormat:@"posts/images/%@%@.jpg", UUID, timestamp];

    [picker dismissViewControllerAnimated:YES completion:NULL];

}

并且,为了上传,

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.body = image;
uploadRequest.key = fileName;
uploadRequest.bucket = MY_BUCKET;
uploadRequest.ACL = AWSS3ObjectCannedACLPublicRead;
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

__weak BTEditProfileViewController *weakSelf = self;
[[transferManager upload:uploadRequest] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
            switch (task.error.code) {
                case AWSS3TransferManagerErrorCancelled:
                case AWSS3TransferManagerErrorPaused:
                {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        NSLog(@"COMPLETED!!");
                    });
                }
                    break;

                default:
                    NSLog(@"Upload failed: [%@]", task.error);
                    break;
            }
        } else {
            NSLog(@"Upload failed: [%@]", task.error);
        }
    }

    if (task.result) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"RESULT:::%@",task.result);
        });
    }

    return nil;
}];

当我运行此操作时,我收到以下错误。

Upload failed: [Error Domain=com.amazonaws.AWSS3TransferManagerErrorDomain Code=5 "'body' can not be nil" UserInfo={NSLocalizedDescription='body' can not be nil}]

我该如何解决这个问题?

0 个答案:

没有答案