AWS S3的Java SDK - 访问被拒绝 [英] AWS S3 Java SDK - Access Denied
问题描述
我试图访问一个桶和所有使用AWS SDK对象,但在运行codeI正在一个错误,因为在线程异常主要 com.amazonaws.services.s3。 model.AmazonS3Exception:拒绝访问(服务:亚马逊S3;状态code:403;错误code:AccessDenied;请求ID:X),S3扩展请求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
code是如下:
AWSCredentials凭据=新BasicAWSCredentials(ACCESSKEY,SecretKey的);
ClientConfiguration clientConfig =新ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTP);
AmazonS3康恩=新AmazonS3Client(凭证,clientConfig);
conn.setEndpoint(bucketName);
桶桶= conn.createBucket(bucketName);
ObjectListing对象= conn.listObjects(bucket.getName());
做 {
为(S3ObjectSummary objectSummary:objects.getObjectSummaries()){
的System.out.println(objectSummary.getKey()+\ t+
objectSummary.getSize()+\ t+
StringUtils.fromDate(objectSummary.getLastModified()));
}
对象= conn.listNextBatchOfObjects(对象);
}而(objects.isTruncated());
现在的问题是解决了。有以下问题向code:
- 的终点是不正确的,应该有一个正确的终点。
- 有没有给予足够的斗许可。完整的权限列表应该用桶在AWS SDK前进行。
下面是正确的code
AWSCredentials凭据=新BasicAWSCredentials(ACCESSKEY,SecretKey的);
ClientConfiguration clientConfig =新ClientConfiguration();
clientConfig.setProtocol(Protocol.HTTP);
AmazonS3康恩=新AmazonS3Client(凭证,clientConfig);
conn.setEndpoint(正确的终点);
桶桶= conn.createBucket(bucketName);
ObjectListing对象= conn.listObjects(bucket.getName());
做 {
为(S3ObjectSummary objectSummary:objects.getObjectSummaries()){
的System.out.println(objectSummary.getKey()+\ t+
objectSummary.getSize()+\ t+
StringUtils.fromDate(objectSummary.getLastModified()));
}
对象= conn.listNextBatchOfObjects(对象);
}而(objects.isTruncated());
I am trying to access a bucket and all its object using AWS SDK but while running the code i am getting an error as Exception in thread "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=
Kindly suggest, where i am lacking and why access denied error is occurring although i have taken all following permission to the bucket:
s3:GetObject
s3:GetObjectVersion
s3:GetObjectAcl
s3:GetBucketAcl
s3:GetBucketCORS
s3:GetBucketLocation
s3:GetBucketLogging
s3:ListBucket
s3:ListBucketVersions
s3:ListBucketMultipartUploads
s3:GetObjectTorrent
s3:GetObjectVersionAcl
Code is as follows:
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());
The problem is now solved. There were following issue to the code:
- The end point was not correct, There should be a correct end point.
- There was not enough permission given to the bucket. A list of complete permission should be taken before using the bucket in AWS SDK.
Below is the correct code
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 - 访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!