简体   繁体   English

AWS S3-com.amazonaws.AmazonServiceException:请求ARN无效

[英]AWS S3 - com.amazonaws.AmazonServiceException: Request ARN is invalid

I'm trying to make my android app download images from AWS S3. 我正在尝试让我的Android应用程序从AWS S3下载图像。 However, the following exception keeps coming up: 但是,不断出现以下异常:

com.amazonaws.AmazonServiceException: Request ARN is invalid (Service: AWSSecurityTokenService; Status Code: 400; Error Code: ValidationError; Request ID: 3481bd5f-1db2-11e5-8442-cb6f713243b6)
            at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:710)
            at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:385)
            at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
            at com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient.invoke(AWSSecurityTokenServiceClient.java:875)
            at com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient.assumeRoleWithWebIdentity(AWSSecurityTokenServiceClient.java:496)
            at com.amazonaws.auth.CognitoCredentialsProvider.populateCredentialsWithSts(CognitoCredentialsProvider.java:671)
            at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:555)
            at com.amazonaws.auth.CognitoCredentialsProvider.refresh(CognitoCredentialsProvider.java:503)
            at com.application.app.utils.helper.S3Utils.getCredProvider(S3Utils.java:35)
            at com.application.app.utils.helper.S3Utils.getS3Client(S3Utils.java:45)
            at com.application.app.integration.volley.CustomImageRequest.parseNetworkError(CustomImageRequest.java:73)
            at com.android.volley.NetworkDispatcher.parseAndDeliverNetworkError(NetworkDispatcher.java:144)
            at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:135)

I have a bucket and an identity pool. 我有一个水桶和一个身份池。 Also, created required roles. 另外,创建所需的角色。

My Cognito_APPUnauth_Role has the following INLINE POLICY: 我的Cognito_APPUnauth_Role具有以下INLINE政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt1435504517000",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::mybucket/*"
            ]
        }
    ]
}

I have a java class named S3Utils that has some helper methods. 我有一个名为S3Utils的Java类,它具有一些帮助程序方法。

public class S3Utils {
    private static AmazonS3Client sS3Client;

    private static CognitoCachingCredentialsProvider sCredProvider;

    public static CognitoCachingCredentialsProvider getCredProvider(Context context){
        if (sCredProvider == null) {
            sCredProvider = new CognitoCachingCredentialsProvider(
                    context,
                    Definitions.AWS_ACCOUNT_ID,
                    Definitions.COGNITO_POOL_ID,
                    Definitions.COGNITO_ROLE_UNAUTH,
                    null,
                    Regions.US_EAST_1
            );
        }

        sCredProvider.refresh();
        return sCredProvider;
    }

    public static String getPrefix(Context context) {
        return getCredProvider(context).getIdentityId() + "/";
    }

    public static AmazonS3Client getS3Client(Context context) {
        if (sS3Client == null) {
            sS3Client = new AmazonS3Client(getCredProvider(context));
        }
        return sS3Client;
    }

    public static String getFileName(String path) {
        return path.substring(path.lastIndexOf("/") + 1);
    }

    public static boolean doesBucketExist() {
        return sS3Client.doesBucketExist(Definitions.BUCKET_NAME.toLowerCase(Locale.US));
    }

    public static void createBucket() {
        sS3Client.createBucket(Definitions.BUCKET_NAME.toLowerCase(Locale.US));
    }

    public static void deleteBucket() {
        String name = Definitions.BUCKET_NAME.toLowerCase(Locale.US);
        List<S3ObjectSummary> objData = sS3Client.listObjects(name).getObjectSummaries();
        if (objData.size() > 0) {
            DeleteObjectsRequest emptyBucket = new DeleteObjectsRequest(name);
            List<DeleteObjectsRequest.KeyVersion> keyList = new ArrayList<DeleteObjectsRequest.KeyVersion>();
            for (S3ObjectSummary summary : objData) {
                keyList.add(new DeleteObjectsRequest.KeyVersion(summary.getKey()));
            }
            emptyBucket.withKeys(keyList);
            sS3Client.deleteObjects(emptyBucket);
        }
        sS3Client.deleteBucket(name);
    }
}

Part of the method where the exception occurs, in CustomImageRequest.java: CustomImageRequest.java中发生异常的方法的一部分:

s3Client = S3Utils.getS3Client(context);
            ObjectListing objects = s3Client.listObjects(new ListObjectsRequest().withBucketName(Definitions.BUCKET_NAME).withPrefix(this.urlToRetrieve));
            List<S3ObjectSummary> objectSummaries = objects.getObjectSummaries();
            //This isn't just an id, it is a full picture name in S3 bucket.
            for (S3ObjectSummary summary : objectSummaries)
            {
                String key = summary.getKey();
                if (!key.equals(this.urlToRetrieve)) continue;
                S3ObjectInputStream content = s3Client.getObject(Definitions.BUCKET_NAME, key).getObjectContent();
                try {
                    this.s3Image = IOUtils.toByteArray(content);

                } catch (IOException e) {
                }

                return new Object();
            }

What am I doing wrong that causes this exception to be thrown every time. 我做错了什么,导致每次都抛出此异常。 Thanks in advance. 提前致谢。

I'm guessing there might be an error in the role ARN you specified. 我猜您指定的角色ARN中可能存在错误。 A role ARN should look something like 角色ARN应该看起来像

arn:aws:cognito-identity:us-east-1:ACCOUNTNUMBER:identitypool/us-east-1:UUID 阿尔恩:AWS:cognito身份:美国东部-1:ACCOUNTNUMBER:identitypool /美东1:UUID

If it is misspelled, or part is left off you may get the error. 如果拼写错误,或者遗漏了一部分,则可能会出现错误。 You may also want to consider user the new CognitoCachingCredentialsProvider constructor. 您可能还需要考虑为用户提供新的CognitoCachingCredentialsProvider构造函数。

sCredProvider = new CognitoCachingCredentialsProvider(
                context,
                Definitions.COGNITO_POOL_ID,
                Regions.US_EAST_1
        );

However note that you will have to make sure that you have specified your role ARN in the Cognito console, but it should help prevent this issue in the future. 但是,请注意,您必须确保已在Cognito控制台中指定了角色ARN,但这将有助于防止将来出现此问题。

Edited for clarity, formatting, and added that you need to modify your ARN in the console if using new constructor. 为清楚起见,进行了格式设置和编辑,并添加了如果使用新的构造函数,则需要在控制台中修改ARN。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pyspark S3 NoClassDefFoundError: com/amazonaws/AmazonClientException - Pyspark S3 NoClassDefFoundError: com/amazonaws/AmazonClientException AWS Lambda Java 到 S3 - com.amazonaws.services.s3.AmazonS3ClientBuilder 上的 java.lang.ClassNotFoundException - AWS Lambda Java to S3 - java.lang.ClassNotFoundException on com.amazonaws.services.s3.AmazonS3ClientBuilder aws s3 withRegion:无法解析方法“withRegion(com.amazonaws.regions.Regions)” - aws s3 withRegion: Cannot resolve method 'withRegion(com.amazonaws.regions.Regions)' OSB代理从AWS S3检索内容-java.lang.NoClassDefFoundError:com / amazonaws / services / s3 / model / S3ObjectInputStream - OSB proxy retrieving contents from AWS S3 - java.lang.NoClassDefFoundError:com/amazonaws/services/s3/model/S3ObjectInputStream 如何解决线程“main”中的异常com.amazonaws.AmazonClientException:无法构建密码:使用aws s3非法密钥大小 - how to resolve Exception in thread “main” com.amazonaws.AmazonClientException: Unable to build cipher: Illegal key size using aws s3 使用 Java 中的 ARN 角色连接到 AWS S3 - connect to AWS S3 using ARN Role in Java com.amazonaws.services.s3.model.AmazonS3Exception:Forbidden(Service:Amazon S3; Status Code:403; Error Code:403 Forbidden; Request ID:XXXXXXXX) - com.amazonaws.services.s3.model.AmazonS3Exception: Forbidden (Service: Amazon S3; Status Code: 403; Error Code: 403 Forbidden; Request ID: XXXXXXXX) 如何从Java中的bucket.s3.amazonaws.com/key更改s3.amazonaws.com/bucket/key之类的Amazon S3文件URL? - How to Change Amazon S3 file URL Like s3.amazonaws.com/bucket/key from bucket.s3.amazonaws.com/key in Java? 在s3上上传文件失败,导致com.amazonaws.AmazonClientException异常 - File upload on s3 fail getting the com.amazonaws.AmazonClientException Exception java.lang.NoClassDefFoundError:Spark中的com / amazonaws / services / s3 / AmazonS3Client - java.lang.NoClassDefFoundError: com/amazonaws/services/s3/AmazonS3Client in Spark
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM