【问题标题】:How to resolve an error when uploading an image with AWS on iOS在 iOS 上使用 AWS 上传图像时如何解决错误
【发布时间】: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


【解决方案1】:

以下是在 amazon s3 上上传图片的代码,还包含一步一步的描述。

- (void)uploadToS3{
    // get the image from a UIImageView that is displaying the selected Image
    UIImage *img = _selectedImage.image;

    // create a local image that we can use to upload to s3
    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"image.png"];
    NSData *imageData = UIImagePNGRepresentation(img);
    [imageData writeToFile:path atomically:YES];

    // once the image is saved we can use the path to create a local fileurl
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];

    // next we set up the S3 upload request manager
    _uploadRequest = [AWSS3TransferManagerUploadRequest new];
    // set the bucket
    _uploadRequest.bucket = @"s3-demo-objectivec";
    // I want this image to be public to anyone to view it so I'm setting it to Public Read
    _uploadRequest.ACL = AWSS3ObjectCannedACLPublicRead;
    // set the image's name that will be used on the s3 server. I am also creating a folder to place the image in
    _uploadRequest.key = @"foldername/image.png";
    // set the content type
    _uploadRequest.contentType = @"image/png";
    // we will track progress through an AWSNetworkingUploadProgressBlock
    _uploadRequest.body = url;

    __weak ViewController *weakSelf = self;

    _uploadRequest.uploadProgress =^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend){
        dispatch_sync(dispatch_get_main_queue(), ^{
            weakSelf.amountUploaded = totalBytesSent;
            weakSelf.filesize = totalBytesExpectedToSend;
            [weakSelf update];

        });
    };

    // now the upload request is set up we can creat the transfermanger, the credentials are already set up in the app delegate
    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    // start the upload
    [[transferManager upload:_uploadRequest] continueWithExecutor:[BFExecutor mainThreadExecutor] withBlock:^id(BFTask *task) {

        // once the uploadmanager finishes check if there were any errors
        if (task.error) {
            NSLog(@"%@", task.error);
        }else{// if there aren't any then the image is uploaded!
            // this is the url of the image we just uploaded
            NSLog(@"https://s3.amazonaws.com/s3-demo-objectivec/foldername/image.png");
        }

        return nil;
    }];

}

更多详情请参考sledgedev Blog

【讨论】:

  • 作者制作了我所指的视频。这就是我的代码的样子,我只是没有上传进度做任何事情。这没有帮助
  • @GeeGoldz 请您仔细检查凭据(帐户 ID 等)。
  • 我仔细检查了凭据,一切都正确。此外,应用程序委托中提供的凭据必须正确,因为我能够通过将我的设备添加到身份池来进行身份验证。我认为我的上传方法或 AWS 门户中的某些设置存在问题
猜你喜欢
  • 1970-01-01
  • 2016-08-10
  • 2018-12-17
  • 2016-11-10
  • 1970-01-01
  • 2021-04-19
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多