jeudi 1 janvier 2015

How to resolve an error when uploading an image with AWS on iOS

I am following this tutorial which involves uploading a UIImage to an s3 bucket using cognito for authentication. I am able to connect to cognito because my device is showing in the identity pool. However, when I try to upload the image to a bucket, I get this error:



Error Domain=com.amazonaws.AWSGeneralErrorDomain Code=3 "The request signature we calculated does not match the signature you provided. Check your key and signing method." UserInfo=0x17dc0f40 {NSLocalizedDescription=The request signature we calculated does not match the signature you provided. Check your key and signing method.}


The cognito authentication policy looks like this:



{
"Version": "2012-10-17",
"Statement": [{
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*",
"s3:*"
],
"Effect": "Allow",
"Resource": [
"*"
]
}]
}


The code for setting up the credentials looks like this:



AWSCognitoCredentialsProvider *credentialsProvider = [AWSCognitoCredentialsProvider
credentialsWithRegionType:AWSRegionUSEast1
accountId:@"#######"
identityPoolId:@"######"
unauthRoleArn:@"#####"
authRoleArn:@"######"];

AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1
credentialsProvider:credentialsProvider]


and the code for uploading the image to s3 looks like this:



NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"image.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:tempPath atomically:YES];


NSURL *url = [[NSURL alloc] initFileURLWithPath:tempPath];

AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
uploadRequest.bucket = @"##########";
//uploadRequest.ACL = AWSS3ObjectCannedACLPublicRead;
uploadRequest.key = @"image.png";
//uploadRequest.contentType = @"image/png";
uploadRequest.body = url;

uploadRequest.uploadProgress =^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){
dispatch_sync(dispatch_get_main_queue(), ^{

});
};

AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
[[transferManager upload:uploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {
if (task.error) {
NSLog(@"%@", task.error);
}else{
//success
NSLog(@"success");
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
}
return nil;
}];




Aucun commentaire:

Enregistrer un commentaire