简体   繁体   English

403 - 在AWS EC2实例上运行的服务上列出对象时的AccessDenied

[英]403 - AccessDenied when listing objects on service running on AWS EC2 instance

I have an EC2 instance running with an IAM role attached to it. 我有一个运行时带有IAM角色的EC2实例。

If I run the following on the terminal of that instance it correctly lists the content of the bucket. 如果我在该实例的终端上运行以下命令,则会正确列出存储桶的内容。

aws s3 ls --region my-region s3://my-bucket-name

But if I run what I think is the same code, but in Java, I get a 403 但是,如果我运行我认为是相同的代码,但在Java中,我得到403

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
        .withRegion(my-region)
        .withCredentials(new InstanceProfileCredentialsProvider(false))
        .withPayloadSigningEnabled(true)
        .build();
s3Client.listObjectsV2(my-bucket-name);

Generates the following 生成以下内容

com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied

Same goes for the original listObjects 原始listObjects也是如此

s3Client.listObjects(TestArtefactsBucket);

Since I am using InstanceProfileCredentialsProvider, this should be using the same profile as used by the CLI, so therefore be using the same permissions? 由于我使用的是InstanceProfileCredentialsProvider,因此应使用与CLI使用的相同的配置文件,因此请使用相同的权限?

I'm using com.amazonaws:aws-java-sdk-s3:1.11.93 and since it was mentioned in another thread, I'm using joda-time:joda-time:2.9.7. 我正在使用com.amazonaws:aws-java-sdk-s3:1.11.93并且因为它在另一个线程中提到过,我使用的是joda-time:joda-time:2.9.7。

Version of Java is java-1.8.0-openjdk.x86_64. Java的版本是java-1.8.0-openjdk.x86_64。

Any help would be appreciated, I'm convinced the permissions are set up correctly, but I am having no luck getting access. 任何帮助将不胜感激,我确信权限设置正确,但我没有运气获取访问权限。

I should also add that I can list the bucket content running the following cli command on the EC2 instance as wel 我还要补充一点,我可以在EC2实例上列出运行以下cli命令的存储桶内容

aws s3api list-objects-v2 --region my-region --bucket my-bucket

Thanks 谢谢

At first you have to check your IAM role [ Access Key & Secret Key ] which is being used for the API. 首先,您必须检查用于API的IAM角色[访问密钥和密钥]。

If [ Access Key & Secret Key ] is ok and you are facing same problem then you need to check IAM role policy, set S3 Full Access. 如果[访问密钥和密钥]正常并且您遇到同样的问题,则需要检查IAM角色策略,设置S3完全访问权限。 This access permission is very much important and most of the time this permission setting solve access denied problem. 此访问权限非常重要,大多数情况下此权限设置解决了访问被拒绝的问题。 Hope it will work for you. 希望它能为你效劳。

S3 access permission S3访问权限

Still not working? 还是行不通? Then check bucket policy, who can read and write objects in the bucket. 然后检查存储桶策略,谁可以读取和写入存储桶中的对象。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM