2017-05-05 106 views
0

我正在使用AWSS3上傳圖片的應用程序。以下是我的代碼。不過,我收到有關池ID未找到的錯誤。我不確定發生了什麼,我是否需要添加更多。如何上傳圖片AWSS3 TransferManager上傳請求目標c?

這是獲得

錯誤域= com.amazonaws.AWSCognitoIdentityErrorDomain代碼= 10 「(空)」 的UserInfo = {__類型= ResourceNotFoundException,消息= IdentityPool 'AP-東北-1:XXXXXXX' 錯誤未找到

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@""]; 
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:credentialsProvider]; 
AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration; 

// get the image from a UIImageView that is displaying the selected Image 

// create a local image that we can use to upload to s3 
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"image.png"]; 
NSData *imageData = UIImagePNGRepresentation(selectedImage); 
[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 
AWSS3TransferManagerUploadRequest *_uploadRequest = [AWSS3TransferManagerUploadRequest new]; 
// set the bucket 
_uploadRequest.bucket = @"chatify"; 
// 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 = @"ios/image.png"; 
// set the content type 
_uploadRequest.contentType = @"image/png"; 
// we will track progress through an AWSNetworkingUploadProgressBlock 
_uploadRequest.body = url; 

__weak ClaimViewController *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:[AWSExecutor mainThreadExecutor] withBlock:^id(AWSTask *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; 
}]; 

回答

0

喜@kashif錯誤明確說,你需要添加池ID

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@""]; 

,但在上面的代碼中你只是傳遞了黑色的identityPoolI d

這也是另一種方法,其提供憑證

AWSStaticCredentialsProvider *credentialsProvider = [AWSStaticCredentialsProvider credentialsWithAccessKey:@"AccessKey" secretKey :@"secretKey"]; 
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSWest2 
                     credentialsProvider:credentialsProvider]; 
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration; 
+0

它只是用於發佈,我刪除池ID,否則我已經通過池ID previosly – Kashif

+0

我剛剛更新了我的回答,請用第二溶液 –

+0

嘗試@Kashif可能是你需要檢查你IAM策略,因爲您沒有對訪問進行身份驗證 –

0

這個錯誤是因爲你的Cognito身份池正在從您的帳戶刪除或錯誤地設置區域。根據我在發佈的代碼中看到的內容,您的區域應設置爲 AWSRegionAPNorthEast1

感謝, 羅漢