简体   繁体   中英

How do I download from Amazon S3 - iOS

I'm trying to download an image from my Amazon S3 Bucket (v2), but I can't figure it out. I keep getting null from [transferManager download:downloadRequest];

All account details/keys are filled in. Any help is appreciated.

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

For starters, you're misusing the AWSS3TransferManagerDownloadRequest 's downloadingFileURL property. downloadingFileURL isn't supposed to refer to the S3 location you're fetching from; instead, it's supposed to refer to the disk location you're writing that file to , ex:

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

Once that file's finished downloading, it will appear at that file path.

To know when your file is ready though, I recommend executing your download request in a block like so:

[[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);
    }
}];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM