AWS S3访问被拒绝 – 使用Java

huangapple 未分类评论54阅读模式
标题翻译

AWS S3 access denied with Java

问题

我正在尝试在Amazon S3上上传文件,但返回以下错误:

com.amazonaws.services.s3.model.AmazonS3Exception: 拒绝访问(服务:Amazon S3;状态码:403;错误代码:AccessDenied;请求ID:26B9B4844CEA580C),S3扩展请求ID:S0Ds3cvubCSZTAd1ESUG5rMRifGLHRAHBviyUHzDVI5W8FQxtRDMBNSrhVme6K86UryWxqORv30=

我已经配置了IAM和AWS上的存储桶,并且我正在使用正确的访问/密钥(AWS提供的访问/密钥)。问题是什么?谢谢!

S3Client 代码:

s3client = AmazonS3ClientBuilder
    .standard()
    .withRegion(Regions.CA_CENTRAL_1) // 尝试首先针对的区域
    .withForceGlobalBucketAccessEnabled(true) // 如果存储桶位于不同的区域,请在正确的区域中重试
    .withCredentials(AWSStaticCredentialsProvider(BasicAWSCredentials(accessKey, secretKey)))
    .build()

上传文件代码:

private fun uploadFileToS3Bucket(fileName: String, file: File) {
    s3client.putObject(PutObjectRequest(bucketName, fileName, file)
        .withCannedAcl(CannedAccessControlList.PublicRead))
}

IAM用户:

AWS S3访问被拒绝 – 使用Java

S3权限:
AWS S3访问被拒绝 – 使用Java

英文翻译

I'm trying to upload files on Amazon S3, but it returns this error :

com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 26B9B4844CEA580C), S3 Extended Request ID: S0Ds3cvubCSZTAd1ESUG5rMRifGLHRAHBviyUHzDVI5W8FQxtRDMBNSrhVme6K86UryWxqORv30=

I have already configured IAM and the bucket on AWS and I use the good access/secret (the one aws gave me). What's the problem? Thank you!

S3Client Code:

s3client = AmazonS3ClientBuilder
    .standard()
    .withRegion(Regions.CA_CENTRAL_1) // The first region to try your request against
    .withForceGlobalBucketAccessEnabled(true) // If a bucket is in a different region, try again in the correct region
    .withCredentials(AWSStaticCredentialsProvider(BasicAWSCredentials(accessKey, secretKey)))
    .build()

Upload file code:

private fun uploadFileTos3bucket(fileName: String, file: File) {
    s3client.putObject(PutObjectRequest(bucketName, fileName, file)
        .withCannedAcl(CannedAccessControlList.PublicRead))
  }

IAM user :

AWS S3访问被拒绝 – 使用Java

S3 permissions :
AWS S3访问被拒绝 – 使用Java

答案1

得分: -1

我遇到了相同的问题。我通过禁用第一个选项使其工作。

阻止通过新访问控制列表(ACL)授予的公共访问权限

如果您想获取有关默认的预设 ACL 的更多信息,请参阅链接

如果您不想禁用任何这些策略,您可以使用预签名 URL。

您可以在此处看到示例。

英文翻译

I had the same problem. I made it work disabling the first option

Block public access to buckets and objects granted through new access control lists (ACLs)

If you want to get more information about the default Canned ACL, here is a link

If you don't want to disable any of these policies you can use pre signed urls

You can see examples here

huangapple
  • 本文由 发表于 2020年3月17日 00:49:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/60709985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定