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

Jak*_*lik 6 amazon-s3 amazon-web-services ios

我正在尝试在S3存储桶上传文件,设备正在从另一台服务器(AWSAccessKeyId和Signature)获取访问信息.是否可以使用AWS iOS SDK v2上传文件?如果没有机会使用另一种可能的iOS方法(例如生成预先签名的URL并进行http post/put)?

现在我正在使用这种方法,但它适用于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;
}];
Run Code Online (Sandbox Code Playgroud)

我会感激任何想法.

Yos*_*uda 3

我推荐以下方法:

  • 在您的服务器上生成访问密钥秘密密钥会话令牌。您有多种语言可供选择,包括 Java、.NET、PHP、Ruby、Python 和 Node.js。
  • 通过遵守AWSCredentialsProvider来实现您自己的凭证提供程序。该凭证提供者应该:
    • 从服务器检索访问密钥、秘密密钥和会话密钥。
    • 坚持它们直到它们过期。
    • 根据要求返回凭据。
    • 如果它们已过期,请从您的服务器重新检索它们。
    • 调用refresh还应启动凭据检索过程。
  • 将您的凭据提供程序分配给defaultServiceConfiguration或传递给initWithConfiguration:

附带说明一下,使用 时initWithConfiguration:,您需要手动保留对 实例的强引用AWSS3。使用defaultS3将消除对此的需要。

希望这可以帮助,