Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). Is it possible to upload file with 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)?

Right now I'm using this approach, but it's for 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.

like image 844
JakubKnejzlik Avatar asked Jul 29 '14 23:07

JakubKnejzlik


People also ask

How do I authenticate to Amazon S3?

For Amazon S3 request authentication, use your AWS secret access key ( YourSecretAccessKey ) as the key, and the UTF-8 encoding of the StringToSign as the message. The output of HMAC-SHA1 is also a byte string, called the digest. The Signature request parameter is constructed by Base64 encoding this digest.

What is AWS4 Hmac SHA256?

AWS4-HMAC-SHA256. The algorithm that was used to calculate the signature. You must provide this value when you use AWS Signature Version 4 for authentication. The string specifies AWS Signature Version 4 ( AWS4 ) and the signing algorithm ( HMAC-SHA256 ). Credential.

What is signature version in S3?

s3:signatureversion. Identifies the version of AWS Signature that you want to support for authenticated requests. For authenticated requests, Amazon S3 supports both Signature Version 4 and Signature Version 2. You can add this condition in your bucket policy to require a specific signature version.


1 Answers

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.
  • Implement your own credentials provider by conforming to 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.
  • Assign your credentials provider to defaultServiceConfiguration or pass it to initWithConfiguration:.

As a side note, when using initWithConfiguration:, you need to manually retain a strong reference to an instance of AWSS3. Using defaultS3 will eliminate the need for this.

Hope this helps,

like image 63
Yosuke Matsuda Avatar answered Sep 20 '22 17:09

Yosuke Matsuda