Amazon Simple Storage Service
开发人员指南 (API 版本 2006-03-01)
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 Amazon AWS 入门

使用 AWS SDK for Java 获取对象

当您通过AWS SDK for Java下载某个对象时,Amazon S3 将返回该对象的所有元数据以及从中读取该对象的内容的输入流。

要检索对象,请执行以下操作:

  • 执行 AmazonS3Client.getObject() 方法,并在请求中提供存储桶名称和对象键。

  • 执行 S3Object 实例方法之一以处理输入流。

注意

您的网络连接将保持打开状态,直到您读取所有数据或关闭输入流。建议您尽快读取流的内容。

下面是您可能使用的一些变体:

  • 您可以通过在请求中指定所需的字节范围来仅读取部分对象数据,而不是读取整个对象。

  • 您可以选择通过使用 ResponseHeaderOverrides 对象并设置相应的请求属性来替代响应标头值 (请参阅获取对象)。例如,您可以使用此功能指示应该将对象下载到具有与对象键名称不同的文件名的文件中。

下面的例子通过三种方式从 Amazon S3 存储桶中检索对象:作为完整的对象、作为来自该对象的一系列字节以及作为包含被覆盖的响应标头值的完整对象。有关从 Amazon S3 中获取对象的更多信息,请参阅 GET Object。有关创建和测试有效示例的说明,请参阅测试 Amazon S3 Java 代码示例

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3ClientBuilder; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.ResponseHeaderOverrides; import com.amazonaws.services.s3.model.S3Object; public class GetObject { public static void main(String[] args) throws IOException { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String key = "*** Object key ***"; S3Object fullObject = null, objectPortion = null, headerOverrideObject = null; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withRegion(clientRegion) .withCredentials(new ProfileCredentialsProvider()) .build(); // Get an object and print its contents. System.out.println("Downloading an object"); fullObject = s3Client.getObject(new GetObjectRequest(bucketName, key)); System.out.println("Content-Type: " + fullObject.getObjectMetadata().getContentType()); System.out.println("Content: "); displayTextInputStream(fullObject.getObjectContent()); // Get a range of bytes from an object and print the bytes. GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName, key) .withRange(0,9); objectPortion = s3Client.getObject(rangeObjectRequest); System.out.println("Printing bytes retrieved."); displayTextInputStream(objectPortion.getObjectContent()); // Get an entire object, overriding the specified response headers, and print the object's content. ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides() .withCacheControl("No-cache") .withContentDisposition("attachment; filename=example.txt"); GetObjectRequest getObjectRequestHeaderOverride = new GetObjectRequest(bucketName, key) .withResponseHeaders(headerOverrides); headerOverrideObject = s3Client.getObject(getObjectRequestHeaderOverride); displayTextInputStream(headerOverrideObject.getObjectContent()); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } finally { // To ensure that the network connection doesn't remain open, close any open input streams. if(fullObject != null) { fullObject.close(); } if(objectPortion != null) { objectPortion.close(); } if(headerOverrideObject != null) { headerOverrideObject.close(); } } } private static void displayTextInputStream(InputStream input) throws IOException { // Read the text input stream one line at a time and display each line. BufferedReader reader = new BufferedReader(new InputStreamReader(input)); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } System.out.println(); } }