繁体   English   中英

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

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

我正在尝试在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;
}];

我会感激任何想法。

我推荐以下方法:

  • 在服务器上生成访问密钥密钥会话令牌 您有许多语言选项,包括Java,.NET,PHP,Ruby,Python和Node.js.
  • 通过符合AWSCredentialsProvider实现您自己的凭据提供程序。 此凭据提供程序应:
    • 从服务器检索访问密钥,密钥和会话密钥。
    • 坚持他们直到他们到期。
    • 请求时返回凭据。
    • 如果它们已过期,请从服务器重新检索它们。
    • 调用refresh也应启动凭据检索过程。
  • 将您的凭证提供程序分配给defaultServiceConfiguration或将其传递给initWithConfiguration: .

作为旁注,当使用initWithConfiguration: ,您需要手动保留对AWSS3实例的强引用。 使用defaultS3将消除对此的需要。

希望这可以帮助,

暂无
暂无

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

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