我正在关注本教程,其中涉及使用 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;
}];