我正在關注this tutorial,其中涉及使用cognito將UIImage上傳到s3存儲桶進行身份驗證。我能夠連接到cognito,因爲我的設備顯示在身份池中。然而,當我嘗試上傳圖片一斗,我得到這個錯誤:如何在iOS上使用AWS上傳圖片時解決錯誤
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。謝謝! – GeeGoldz