使用ACCESSKEY和SecretKey的下载从S3服务器安全文件服务器、文件、安全、ACCESSKEY

2023-09-11 09:51:22 作者:多情客串

我想下载从S3服务器使用NSURLSessionDownloadTask一个安全的文件,但它返回我的403错误(拒绝访问)。 我的code:

I'm trying to download a secure file from S3 server using NSURLSessionDownloadTask,but it returns me 403 error(Access Denied). My Code:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://xxx.amazonaws.com/bucket-name/file_name"]];
request.HTTPMethod = @"GET";

[request setValue:@"kAccessKey" forHTTPHeaderField:@"accessKey"];
[request setValue:@"kSecretKey" forHTTPHeaderField:@"secretKey"];

NSURLSessionDownloadTask *downloadPicTask = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    UIImage *downloadedImage = [UIImage imageWithData:
                                [NSData dataWithContentsOfURL:location]];
    dispatch_async(dispatch_get_main_queue(), ^{
        weakSelf.imageView.image = downloadedImage;
        [weakSelf.activityIndicator stopAnimating];
    });

}];
[downloadPicTask resume];

修改

我发现这个code:

I found this code :

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc]initWithRegionType:AWSRegionUSWest2 identityId:@"xxxxxxx" identityPoolId:@"xxxxxxxx" logins:nil];
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc]initWithRegion:AWSRegionUSWest2 credentialsProvider:credentialsProvider];

// Construct the NSURL for the download location.
NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"sample_img.png"];
NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];

// Construct the download request.
AWSS3TransferManagerDownloadRequest *downloadRequest = [[AWSS3TransferManagerDownloadRequest alloc]init];
AWSS3TransferManager * transferManager = [AWSS3TransferManager S3TransferManagerForKey:[[configuration credentialsProvider]sessionKey]];

downloadRequest.bucket = @"test-upload-bucket";
downloadRequest.key = @"sample_img.png";
downloadRequest.downloadingFileURL = downloadingFileURL;

[[transferManager download:downloadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
                                                       withBlock:^id(AWSTask *task){
                                                           return nil;
                                                       }];

什么是IdentityId和IdentityPoolId?

What is the input value for IdentityId and IdentityPoolId ?

推荐答案

所有的HTTP请求必须正确签名之前发送到AWS服务器,签名过程是非常复杂的签名版本4签名过程所以我建议尝试的 AWS移动SDK为iOS V2

All HTTP Request need to be properly signed before send to AWS server, the signing Process is very complicated Signature Version 4 Signing Process so I suggest to try AWS Mobile SDK for iOS v2

由Arun_给出的例子是code片段,如何使用transferManager下载通过AWS移动SDK文件中的iOS V2。

The example shown by Arun_ is a code snippets that how to use transferManager to download a file via AWS Mobile SDK for iOS v2.