AWS S3 Java SDK-拒绝访问

我正在尝试使用AWS开发工具包访问存储桶及其所有对象,但是在运行代码时,出现错误,线程“ main”中出现异常

com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service:

Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: X), S3

Extended Request ID: Y=

请提出建议,尽管我已经对存储桶采取了以下所有许可,但我缺少的地方以及为什么发生拒绝访问错误:

s3:GetObject

s3:GetObjectVersion

s3:GetObjectAcl

s3:GetBucketAcl

s3:GetBucketCORS

s3:GetBucketLocation

s3:GetBucketLogging

s3:ListBucket

s3:ListBucketVersions

s3:ListBucketMultipartUploads

s3:GetObjectTorrent

s3:GetObjectVersionAcl

代码如下:

AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

ClientConfiguration clientConfig = new ClientConfiguration();

clientConfig.setProtocol(Protocol.HTTP);

AmazonS3 conn = new AmazonS3Client(credentials, clientConfig);

conn.setEndpoint(bucketName);

Bucket bucket = conn.createBucket(bucketName);

ObjectListing objects = conn.listObjects(bucket.getName());

do {

for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {

System.out.println(objectSummary.getKey() + "\t" +

objectSummary.getSize() + "\t" +

StringUtils.fromDate(objectSummary.getLastModified()));

}

objects = conn.listNextBatchOfObjects(objects);

} while (objects.isTruncated());

回答:

现在问题已解决。代码存在以下问题:

  1. 终点不正确,应该有一个正确的终点。
  2. 没有足够的权限授予该存储桶。在AWS开发工具包中使用存储桶之前,应获取完整权限列表。

下面是正确的代码

AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

ClientConfiguration clientConfig = new ClientConfiguration();

clientConfig.setProtocol(Protocol.HTTP);

AmazonS3 conn = new AmazonS3Client(credentials, clientConfig);

conn.setEndpoint("correct end point");

Bucket bucket = conn.createBucket(bucketName);

ObjectListing objects = conn.listObjects(bucket.getName());

do {

for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {

System.out.println(objectSummary.getKey() + "\t" +

objectSummary.getSize() + "\t" +

StringUtils.fromDate(objectSummary.getLastModified()));

}

objects = conn.listNextBatchOfObjects(objects);

} while (objects.isTruncated());

以上是 AWS S3 Java SDK-拒绝访问 的全部内容, 来源链接: utcz.com/qa/398208.html

回到顶部