如何从Amazon S3 - iOS下载

时间:2015-02-17 00:09:38

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

我试图从我的Amazon S3 Bucket(v2)下载图像,但我无法弄明白。我一直从[transferManager download:downloadRequest];

获得null

所有帐户详细信息/密钥均已填写。欢迎任何帮助。

-(void) startApp:(NSDictionary *)launchOptions {
// create credentials
AWSCognitoCredentialsProvider *credentialsProvider = [AWSCognitoCredentialsProvider credentialsWithRegionType:AWSRegionUSEast1
                                                                                                accountId:@"xxxx-xxxx-xxxx"
                                                                                               identityPoolId: @"us-east-1:xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
                                                                                                unauthRoleArn:@"Cognito_AppUnauth_DefaultRole"
                                                                                                  authRoleArn:nil];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1
                                                                      credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
}

- (instancetype) init {
AWSS3TransferManagerDownloadRequest *downloadRequest = [AWSS3TransferManagerDownloadRequest new];
downloadRequest.bucket = @"mybucket.s3.amazonaws.com";
downloadRequest.key = @"XXXXXXXXXXXXXXX";
downloadRequest.downloadingFileURL = [NSURL URLWithString:@"https://s3.amazonaws.com/MYBUCKET/hello.png"];
[self download:downloadRequest];

return self;
}
- (BFTask *)download:(AWSS3TransferManagerDownloadRequest *)downloadRequest {
NSLog(@"Download request: %@", downloadRequest);
AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
NSLog(@"%@", [transferManager download:downloadRequest]);
return nil;
}

1 个答案:

答案 0 :(得分:3)

对于初学者,您滥用AWSS3TransferManagerDownloadRequest的{​​{1}}财产。 downloadingFileURL不应该引用您从中获取的S3位置;相反,它应该引用您将该文件写入的磁盘位置,例如:

downloadingFileURL

该文件下载完成后,将显示在该文件路径中。

要知道你的文件何时准备就绪,我建议你在一个块中执行你的下载请求:

NSString *downloadingFilePath = [NSTemporaryDirectory()stringByAppendingPathComponent:@"hello.png"];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];