我是S3的新手。我们的供应商之一与我们共享一个桶和对象。我们创建了一个AWS账户,并将我们的团队成员添加为用户。我们可以通过amazon aws cli访问存储桶中的数据。我正在寻找java api以编程方式访问数据。
我创建了一个独立的代码
/*
* Create your credentials file at ~/.aws/credentials (C:\Users\USER_NAME\.aws\credentials for Windows users)
* and save the following lines after replacing the underlined values with your own.
*
* [default]
* aws_access_key_id = YOUR_ACCESS_KEY_ID
* aws_secret_access_key = YOUR_SECRET_ACCESS_KEY
*/
AmazonS3 s3 = new AmazonS3Client();
Region usEast1 = Region.getRegion(Regions.US_EAST_1);
s3.setRegion(usEast1);
System.out.println("Listing objects");
ObjectListing objectListing = s3.listObjects(new ListObjectsRequest()
.withBucketName("exports.abx.t-z/xyz")
);
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " +
"(size = " + objectSummary.getSize() + ")");
}
System.out.println();
现在我遇到了异常
Listing objects
Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.
Error Message: The specified key does not exist. (Service: Amazon S3; Status Code: 404; Error Code: NoSuchKey; Request ID: C80B0460828347D0)
HTTP Status Code: 404
AWS Error Code: NoSuchKey
Error Type: Client
Request ID: C80B0460828347D0
我没有访问密钥。任何帮助表示赞赏。
谢谢, 阿米特
答案 0 :(得分:2)
404响应表明您的凭据没问题,但您要求的内容不存在。
这部分代码看起来很可疑:
new ListObjectsRequest().withBucketName("exports.abx.t-z/xyz")
应为withBucketName
方法指定存储桶名称。我怀疑在你的情况下,存储桶名称应该只是“exports.abx.t-z”。如果您要列出该存储桶中“xyz”“文件夹”中的所有对象,那么您可能希望执行以下操作:
new ListObjectsRequest().withBucketName("exports.abx.t-z").withPrefix("xyz")