簡體   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