Amazon Simple Storage Service
开发人员指南 (API Version 2006-03-01)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。点 击 Getting Started with Amazon AWS to see specific differences applicable to the China (Beijing) Region.

使用 AWS SDK for Java 获取对象

当您下载对象时,将获取对象的所有元数据和流,从中可以读取内容。您应该尽快读取流的内容,因为数据直接源自 Amazon S3,在您读取所有数据或关闭输入流之前,您的网络连接将保持打开状态。

下载对象

1

创建 AmazonS3Client 类的实例。

2

执行 AmazonS3Client.getObject() 方法之一。您需要提供请求信息,例如存储桶名称和键名称。您可以通过创建 GetObjectRequest 类的实例提供此信息。

3

对返回的对象执行 getObjectContent() 方法之一以获取关于对象数据的流,并处理响应。

以下 Java 代码示例演示了上述任务。

Copy
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); S3Object object = s3Client.getObject( new GetObjectRequest(bucketName, key)); InputStream objectData = object.getObjectContent(); // Process the objectData stream. objectData.close();

GetObjectRequest 对象提供多种选项,包括基于修改次数有条件地下载对象、ETag 和选择性地下载某个范围的对象。以下 Java 代码示例将演示您可以如何指定数据字节范围来从对象中进行检索。

Copy
AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); GetObjectRequest rangeObjectRequest = new GetObjectRequest( bucketName, key); rangeObjectRequest.setRange(0, 10); // retrieve 1st 11 bytes. S3Object objectPortion = s3Client.getObject(rangeObjectRequest); InputStream objectData = objectPortion.getObjectContent(); // Process the objectData stream. objectData.close();

在检索对象时,您可以选择通过使用 ResponseHeaderOverrides 对象并设置相应的请求属性来替换响应标头值 (请参阅 获取对象),如以下 Java 代码示例所示。

Copy
GetObjectRequest request = new GetObjectRequest(bucketName, key); ResponseHeaderOverrides responseHeaders = new ResponseHeaderOverrides(); responseHeaders.setCacheControl("No-cache"); responseHeaders.setContentDisposition("attachment; filename=testing.txt"); // Add the ResponseHeaderOverides to the request. request.setResponseHeaders(responseHeaders);

以下 Java 代码示例从指定的 Amazon S3 存储桶检索对象。有关如何创建和测试有效示例的说明,请参阅 测试 Java 代码示例

Copy
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import com.amazonaws.services.s3.AmazonS3; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.s3.model.GetObjectRequest; import com.amazonaws.services.s3.model.S3Object; public class GetObject { private static String bucketName = "*** provide bucket name ***"; private static String key = "*** provide object key ***"; public static void main(String[] args) throws IOException { AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider()); try { System.out.println("Downloading an object"); S3Object s3object = s3Client.getObject(new GetObjectRequest( bucketName, key)); System.out.println("Content-Type: " + s3object.getObjectMetadata().getContentType()); displayTextInputStream(s3object.getObjectContent()); // Get a range of bytes from an object. GetObjectRequest rangeObjectRequest = new GetObjectRequest( bucketName, key); rangeObjectRequest.setRange(0, 10); S3Object objectPortion = s3Client.getObject(rangeObjectRequest); System.out.println("Printing bytes retrieved."); displayTextInputStream(objectPortion.getObjectContent()); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which" + " means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means"+ " the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } } private static void displayTextInputStream(InputStream input) throws IOException { // Read one text line at a time and display. BufferedReader reader = new BufferedReader(new InputStreamReader(input)); while (true) { String line = reader.readLine(); if (line == null) break; System.out.println(" " + line); } System.out.println(); } }