上传后从存储桶Amazon Web Service iOS获取图像URL

时间:2014-09-08 14:34:07

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

我有一张图片,我正在以这种方式上传到我的存储桶:

BFTask *task = [BFTask taskWithResult:nil];

        [[task continueWithBlock:^id(BFTask *task) {

            self.URL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"test"]];

            NSData *data = UIImagePNGRepresentation(image);
            //NSMutableString *dataString = [NSMutableString new];
            [data writeToURL:self.URL atomically:YES];
            return nil;

        }]continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {

            self.uploadRequest1 = [AWSS3TransferManagerUploadRequest new];
            self.uploadRequest1.bucket = S3BucketName;
            self.uploadRequest1.key = S3KeyUploadName1;


            self.uploadRequest1.body = self.URL;
            return nil;
        }];

            AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];

            [[transferManager upload:self.uploadRequest1] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
                if (task.error != nil) {
                    if( task.error.code != AWSS3TransferManagerErrorCancelled
                       &&
                       task.error.code != AWSS3TransferManagerErrorPaused
                       )
                    {
                        NSLog(@"Upload Failed!");
                    }
                } else {
                    self.uploadRequest1 = nil;
                    NSLog(@"Uploaded!");
                                        }
                return nil;
            }];

上传图片的代码工作得很好。当我打开我的水桶时,我看到那里的图像。

现在我要做的是获取该图片的网址,有没有办法获取网址而不再获取图片?

3 个答案:

答案 0 :(得分:6)

你没有得到它,你创建它:

<强> https://s3.amazonaws.com/BUCKET_NAME/FILE_NAME.jpg

答案 1 :(得分:4)

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];

uploadRequest.body = [NSURL fileURLWithPath:filePath];
uploadRequest.key = fileName;
uploadReuest.bucket = S3BucketName;
  

[uploadRequest setACL:AWSS3ObjectCannedACLPublicRead];

以上答案是正确的,但您必须将“ACL”设置为uploadRequest。

在您的问题中,您忘记设置“ACL”。

谢谢

答案 2 :(得分:2)

我能够从Amazon S3获取路径 - &gt;铲斗 - &gt;文件夹 - &gt;图像文件 - &gt;属性,在右侧,你会看到这样的东西..

Get image accessible path from here