1 回答
TA贡献1790条经验 获得超9个赞
是的,您可以使用getObject()的实例AmazonS3来下载文件:
String bucketName = "BUCKET_NAME";
String key = "KEY_OF_OBJECT";
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withRegion(Regions.DEFAULT_REGION)
.withCredentials(new ProfileCredentialsProvider())
.build();
System.out.println("Downloading an object");
S3Object fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key));
System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType());
System.out.println("Content: ");
// fullObject.getObjectContent() is an InputStream
BufferedReader reader = new BufferedReader(new InputStreamReader(fullObject.getObjectContent()));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
如果您只需要部分数据,可以使用修饰符withRange()onGetObjectRequest()创建范围请求。
添加回答
举报