简体   繁体   English

Amazon S3 iOS SDK v2使用AWSAccessKeyId上传:签名

[英]Amazon S3 iOS SDK v2 Upload with AWSAccessKeyId:Signature

I'm trying to upload file on S3 bucket and device is getting access information from another server (AWSAccessKeyId and Signature). 我正在尝试在S3存储桶上传文件,设备正在从另一台服务器(AWSAccessKeyId和Signature)获取访问信息。 Is it possible to upload file with AWS iOS SDK v2? 是否可以使用AWS iOS SDK v2上传文件? If not are there any chances to use another approach possible for iOS (eg. generate Pre-Signed URL and do the http post/put)? 如果没有机会使用另一种可能的iOS方法(例如生成预先签名的URL并进行http post / put)?

Right now I'm using this approach, but it's for access_key/access_secret: 现在我正在使用这种方法,但它适用于access_key / access_secret:

AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:awsAccessKey secretKey:awsSecretKey];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;

AWSS3 *transferManager = [[AWSS3 alloc] initWithConfiguration:configuration];
AWSS3PutObjectRequest *getLog = [[AWSS3PutObjectRequest alloc] init];
getLog.bucket = awsS3Bucket;
getLog.key = awsS3FileNameString;
getLog.contentType = @"text/plain";
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:logFileName];
long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:fileName error:nil][NSFileSize] longLongValue];
getLog.body = [NSURL fileURLWithPath:fileName];
getLog.contentLength = [NSNumber numberWithUnsignedLongLong:fileSize];

[[transferManager putObject:getLog] continueWithBlock:^id(BFTask *task) {        
    if(task.error)
    {
        NSLog(@"Error: %@",task.error);
    }
    else
    {
        NSLog(@"Got here: %@", task.result);

    }
    return nil;
}];

I'll be grateful for any ideas. 我会感激任何想法。

I recommend the following approach: 我推荐以下方法:

  • Generate the access key , secret key , and session token on your server. 在服务器上生成访问密钥密钥会话令牌 You have many language options including Java, .NET, PHP, Ruby, Python, and Node.js. 您有许多语言选项,包括Java,.NET,PHP,Ruby,Python和Node.js.
  • Implement your own credentials provider by conforming to AWSCredentialsProvider . 通过符合AWSCredentialsProvider实现您自己的凭据提供程序。 This credentials provider should: 此凭据提供程序应:
    • Retrieve the access key, secret key, and session key from your server. 从服务器检索访问密钥,密钥和会话密钥。
    • Persist them until they expire. 坚持他们直到他们到期。
    • Return the credentials when requested. 请求时返回凭据。
    • If they are expired, re-retrieve them from your server. 如果它们已过期,请从服务器重新检索它们。
    • Calling refresh also should initiate the credentials retrieval process. 调用refresh也应启动凭据检索过程。
  • Assign your credentials provider to defaultServiceConfiguration or pass it to initWithConfiguration: . 将您的凭证提供程序分配给defaultServiceConfiguration或将其传递给initWithConfiguration: .

As a side note, when using initWithConfiguration: , you need to manually retain a strong reference to an instance of AWSS3 . 作为旁注,当使用initWithConfiguration: ,您需要手动保留对AWSS3实例的强引用。 Using defaultS3 will eliminate the need for this. 使用defaultS3将消除对此的需要。

Hope this helps, 希望这可以帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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