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.
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.
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.
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.
I recommend the following approach:
refresh
also should initiate the credentials retrieval process.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,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With