我是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("Downloading an object");
S3Object object = s3.getObject(new GetObjectRequest("exports.xyz.t-z", "abc/2015/12/07/62542f4f0164689f5d18cf6-2c324750-6c47-11e5-0e29-00deb82fd81f"));
System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
错误信息是:
Downloading an object
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: 3DD21D3934A4456D)
HTTP Status Code: 404
AWS Error Code: NoSuchKey
Error Type: Client
Request ID: 3DD21D3934A4456D
现在正如错误消息所示:键不存在我不确定键是什么。数据由供应商提供给s3。
但是,我可以使用以下代码访问数据以查看s3目录中列出的内容。 (您也可以参考我的previous post)
System.out.println("Listing objects");
ObjectListing objectListing = s3.listObjects(
new ListObjectsRequest().withBucketName("exports.xyz.t-z")
.withPrefix("abc/2015/12/07/62542f4f0164689f5d18cf6-2c324750-6c47-11e5-0e29-00deb82fd81f")
);
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " +
"(size = " + objectSummary.getSize() + ")");
}
下载数据的最佳推荐方法是什么?
答案 0 :(得分:1)
看起来您正在尝试下载“目录”而不是该目录中的特定键。
您的代码列出了具有该前缀的对象。当您尝试使用getObject(new GetObjectRequest())
下载文件时,GetObjectRequest
构造函数的第二个参数必须是您致电objectSummary.getKey()
时返回的值之一。