如何在AWS s3中上传图片,在上传时收到错误

时间:2016-04-18 11:33:37

标签: objective-c iphone amazon-web-services amazon-s3 amazon-cognito

我正在使用亚马逊最新的SDK库将图片上传到存储桶。但是得到错误,这是我的代码

App委托代码

- (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;
}

我的视图控制器代码

    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.}]

2 个答案:

答案 0 :(得分:1)

Cognito Identity支持的端点(取自此处: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 Identity。尝试上面列表中的其他区域。

答案 1 :(得分:1)

Amazon Cognito Identity目前仅适用于us-east-1,eu-west-1和ap-northeast-1。在实例化AWSCognitoCredentialsProvider时,您需要使用创建Cognito Identity Pool的区域。

您仍然可以将Cognito Identity获取的凭据用于其他地区的其他服务。