Question:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
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.
Answer:
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 toinitWithConfiguration:
.
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,