【发布时间】:2015-02-28 12:42:06
【问题描述】:
我正在关注this tutorial,其中涉及使用 cognito 将 UIImage 上传到 s3 存储桶进行身份验证。我能够连接到 cognito,因为我的设备显示在身份池中。但是,当我尝试将图像上传到存储桶时,出现此错误:
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.}
cognito 身份验证策略如下所示:
{
"Version": "2012-10-17",
"Statement": [{
"Action": [
"mobileanalytics:PutEvents",
"cognito-sync:*",
"s3:*"
],
"Effect": "Allow",
"Resource": [
"*"
]
}]
}
设置凭据的代码如下所示:
AWSCognitoCredentialsProvider *credentialsProvider = [AWSCognitoCredentialsProvider
credentialsWithRegionType:AWSRegionUSEast1
accountId:@"#######"
identityPoolId:@"######"
unauthRoleArn:@"#####"
authRoleArn:@"######"];
AWSServiceConfiguration *configuration = [AWSServiceConfiguration configurationWithRegion:AWSRegionUSEast1
credentialsProvider:credentialsProvider]
上传图片到s3的代码如下:
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;
}];
【问题讨论】:
-
您使用的 SDK 版本是多少?您应该使用最新版本的 SDK。您的存储桶是否包含任何特殊字符?此外,您应该通过调用
[AWSLogger defaultLogger].logLevel = AWSLogLevelVerbose;来启用详细日志记录。 -
通过重新添加 SDK 来修复它。出于某种原因,我有 v2.0.5。谢谢!
标签: ios amazon-web-services amazon-s3 upload amazon-cognito