如何从AmazonS3-iOS下载
我试图从我的AmazonS3存储桶(v2)下载一个图像,但我想不出来。我一直从[transferManager下载:downloadRequest]获取空值 已填写所有帐户详细信息/密钥。感谢您的帮助如何从AmazonS3-iOS下载,ios,objective-c,xcode,amazon-web-services,amazon-s3,Ios,Objective C,Xcode,Amazon Web Services,Amazon S3,我试图从我的AmazonS3存储桶(v2)下载一个图像,但我想不出来。我一直从[transferManager下载:downloadRequest]获取空值 已填写所有帐户详细信息/密钥。感谢您的帮助 -(void) startApp:(NSDictionary *)launchOptions { // create credentials AWSCognitoCredentialsProvider *credentialsProvider = [AWSCognitoCredentialsPro
-(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;
}
首先,您误用了
AWSS3TransferManagerDownloadRequest
的downloadingFileURL
属性downloadingFileURL
不应该指向您正在获取的S3位置;相反,它应该是指将文件写入的磁盘位置,例如:
一旦该文件完成下载,它将显示在该文件路径上
不过,为了知道文件何时准备就绪,我建议在如下块中执行下载请求:
[[transferManager download:downloadRequest]continueWithExecutor
[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
if (task.result) {
AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;
self.fetchedImage = [UIImage imageWithContentsOfFile:downloadingFilePath];
} else if (task.error) {
NSLog(@"Error: %@", task.error);
}
}];
您在代码中的何处检查下载的文件?
[[transferManager download:downloadRequest]continueWithExecutor
[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
if (task.result) {
AWSS3TransferManagerDownloadOutput *downloadOutput = task.result;
self.fetchedImage = [UIImage imageWithContentsOfFile:downloadingFilePath];
} else if (task.error) {
NSLog(@"Error: %@", task.error);
}
}];