2015-01-02 86 views
3

我正在關注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; 
}]; 
+1

什麼是您所使用的SDK版本是指更詳細?您應該使用最新版本的SDK。您的存儲桶是否包含任何特殊字符?另外,您應通過調用'[AWSLogger defaultLogger] .logLevel = AWSLogLevelVerbose;'來啓用詳細日誌記錄。 –

+0

通過重新添加SDK來修復它。由於某種原因我有v2.0.5。謝謝! – GeeGoldz

回答

0

以下是在亞馬遜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

+0

這位作家做了我所指的視頻。這就是我的代碼的外觀,我只是沒有上傳進度。這沒有幫助 – GeeGoldz

+0

@GeeGoldz可以請你仔細檢查憑證(accountId等)。 –

+0

我仔細檢查了憑據,一切都正確。此外,應用程序委託中提供的憑據必須正確,因爲我可以通過將設備添加到身份池進行身份驗證。我認爲我的上傳方法或AWS門戶中的一些設置存在問題 – GeeGoldz