AWS S3:存储区域和identityPoolId(Swift)的混淆

时间:2017-03-17 20:22:05

标签: swift amazon-s3

我在从AWS S3存储桶(Swift / iOS应用)下载文件时遇到问题。这似乎是两个问题之一,但我无法分辨:

  1. 我的identityPoolId问题
  2. 铲斗区域问题
  3. 如果我像这样创建credentialsProvider:

    AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@"us-west-2:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"];
    

    我收到以下错误:

    [Error Domain=com.amazonaws.AWSCognitoIdentityErrorDomain Code=10 "(null)" UserInfo={__type=ResourceNotFoundException, message=Identity 'us-west-2:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' not found.}]
    

    我注意到我创建凭证提供程序的行不匹配:regionType是AWSRegionUSEast1,但我有" us-west-2"作为identityPoolId的一部分。

    如果我将regionType更改为AWSRegionUSWest2(以匹配我的id),则ResourceNotFoundExeception将消失。但后来我明白了:

    <Error><Code>PermanentRedirect</Code><Message>The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.</Message><Bucket>sportsmanregs</Bucket><Endpoint>s3.amazonaws.com</Endpoint><RequestId>AEFD833FF4E7B6F1</RequestId><HostId>mYxbBKlzTeWgVZW4W060+ESiBhWuxmfDDFE6UriXG7bqxA5NWuRbH9lu4NuGCaU/7H8f1hjAukA=</HostId></Error>
    

    有趣的是,在完成处理程序中的数据对象中发现了上述错误,如&#34; data&#34;在下面:

    completionHandler = {
            (task, location, data, error) -> Void in DispatchQueue.main.async( execute: {
    

    所以completionHandler被调用OK。在S3控制台中,我尝试下载的文件的URL以&#34; https://s3.amazonaws.com/ / xxx / xxx.tiff&#34;开头。有没有人知道这里的问题是什么?

2 个答案:

答案 0 :(得分:1)

听起来您的身份池和S3存储桶都位于us-west-2区域。您在自己处理了身份池部分,这是正确的。为了修复代码中的存储区域,您需要将该区域传递给S3调用,否则它将使用默认的us-east-1并为您提供该错误。

另一种方法是用[bucketname] .s3.amazonaws.com指定你的网址,有些人似乎已经取得了成功,但它可能只是通过修复该区域。

答案 1 :(得分:-2)

AWSCognitoCredentialsProvider *credentialsProvider = [[AWSCognitoCredentialsProvider alloc] initWithRegionType:AWSRegionUSEast1 identityPoolId:@"us-west-2:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"];

这是正确的,但您需要添加一些代码。请添加此代码:

 NSString *cognitoId = credentialsProvider.identityId;
    NSString *cognitopoolId = credentialsProvider.identityPoolId;
    NSLog(@"This is cognitoId: %@",cognitoId);
    NSLog(@"This is cognitopoolId: %@",cognitopoolId);

    [[credentialsProvider getIdentityId] continueWithBlock:^id(AWSTask *task){

        if (task.error == nil) {
            NSString* cognitoId = credentialsProvider.identityId;
            NSLog(@"cognitoId: %@", cognitoId);
        } else {
            NSLog(@"Error : %@", task.error);
        }
        return nil;
    }];