簡體   English   中英

如何在AWS s3中上傳圖像,上傳時出錯

[英]How to Upload Image in AWS s3 , getting error at upload time

我正在使用亞馬遜的最新SDK庫將圖像上傳到存儲桶。 但是遇到錯誤,這是我的代碼

應用代表代碼

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionAPSoutheast1
                                                                                                identityPoolId:AWS_IDENTITY_POOL_ID1];
    AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionAPSoutheast1
                                                                     credentialsProvider:credentialsProvider];
    AWSServiceManager.defaultServiceManager.defaultServiceConfiguration = configuration;

    return YES;
}

我的View Controller代碼是

    AWSS3TransferManager *transferManager = [AWSS3TransferManager defaultS3TransferManager];
    NSString *downloadingFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"downloaded-myImage.jpg"];
    NSURL *downloadingFileURL = [NSURL fileURLWithPath:downloadingFilePath];
    imageView.image = [UIImage imageWithContentsOfFile:downloadingFilePath];

    AWSS3TransferManagerUploadRequest *uploadRequest = [AWSS3TransferManagerUploadRequest new];
    uploadRequest.bucket = @"myBucketName";
    uploadRequest.key = @"downloaded-myImage.jpg";
    uploadRequest.body = downloadingFileURL;

    [[transferManager upload:uploadRequest] continueWithExecutor:[AWSExecutor mainThreadExecutor]
                                                   withBlock:^id(AWSTask *task) {
                                                       if (task.error) {
                                                           if ([task.error.domain isEqualToString:AWSS3TransferManagerErrorDomain]) {
                                                               switch (task.error.code) {
                                                                   case AWSS3TransferManagerErrorCancelled:
                                                                   case AWSS3TransferManagerErrorPaused:
                                                                       break;

                                                                   default:
                                                                       NSLog(@"Error: %@", task.error);
                                                                       break;
                                                               }
                                                           } else {
                                                               // Unknown error.
                                                               NSLog(@"Error: %@", task.error);
                                                           }
                                                       }

                                                       if (task.result) {
                                                           AWSS3TransferManagerUploadOutput *uploadOutput = task.result;
                                                           // The file uploaded successfully.
                                                           NSLog(@"LOG %@", task.result);
                                                       }
                                                       return nil;
                                                   }];

   [transferManager upload:uploadRequest];

但是每次遇到這樣的錯誤

 AWSiOSSDKv2 [Error] AWSCredentialsProvider.m line:528 | __40-[AWSCognitoCredentialsProvider refresh]_block_invoke352 | Unable to refresh. 
 Error is [Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." 
 UserInfo={NSUnderlyingError=0x7ffd7300a070 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" 
 UserInfo={_kCFStreamErrorCodeKey=8, _kCFStreamErrorDomainKey=12}}, NSErrorFailingURLStringKey=https://cognito-identity.ap-southeast-1.amazonaws.com/, 
 NSErrorFailingURLKey=https://cognito-identity.ap-southeast-1.amazonaws.com/, _kCFStreamErrorDomainKey=12, _kCFStreamErrorCodeKey=8, 
 NSLocalizedDescription=A server with the specified hostname could not be found.}]

Cognito身份支持的端點是(從此處獲取: http : //docs.aws.amazon.com/general/latest/gr/rande.html#cognito_identity_region ):

Region Name             Region         Endpoint                                      Protocol
US East (N. Virginia)   us-east-1      cognito-identity.us-east-1.amazonaws.com      HTTPS
EU (Ireland)            eu-west-1      cognito-identity.eu-west-1.amazonaws.com      HTTPS
Asia Pacific (Tokyo)    ap-northeast-1 cognito-identity.ap-northeast-1.amazonaws.com HTTPS

看起來新加坡地區(ap-southeast-1)不支持Cognito身份。 嘗試使用上面列表中的其他區域。

Amazon Cognito身份當前僅在us-east-1,eu-west-1和ap-northeast-1中可用。 實例化AWSCognitoCredentialsProvider時,您將需要使用創建Cognito身份池的區域。

您仍然可以將Cognito Identity獲得的憑據用於其他地區的其他服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM