| « PreviousNext » | |
![]() ![]() ![]() | Did this page help you? Yes | No | Tell us about it... |
When you download an object, you get all of object's metadata and a stream from which to read the contents. You should read the content of the stream as quickly as possible because the data is streamed directly from Amazon S3 and your network connection will remain open until you read all the data or close the input stream.
Downloading Objects
1 | Create an instance of the |
2 | Execute one of the |
3 | Execute one of the |
The following Java code sample demonstrates the preceding tasks.
AWSCredentials myCredentials = new BasicAWSCredentials(
myAccessKeyID, mySecretKey);
AmazonS3 s3Client = new AmazonS3Client(myCredentials);
S3Object object = s3Client.getObject(
new GetObjectRequest(bucketName, key));
InputStream objectData = object.getObjectContent();
// Process the objectData stream.
objectData.close();The GetObjectRequest object provides several options, including
conditional downloading of objects based on modification times, ETags, and
selectively downloading a range of an object. The following Java code sample
demonstrates how you can specify a range of data bytes to retrieve from an
object.
AWSCredentials myCredentials = new BasicAWSCredentials(
myAccessKeyID, mySecretKey);
AmazonS3 s3Client = new AmazonS3Client(myCredentials);
GetObjectRequest rangeObjectRequest = new GetObjectRequest(
bucketName, key);
rangeObjectRequest.setRange(0, 10); // retrieve 1st 10 bytes.
S3Object objectPortion = s3Client.getObject(rangeObjectRequest);
InputStream objectData = objectPortion.getObjectContent();
// Process the objectData stream.
objectData.close();When retrieving an object, you can optionally override the response header values (see
Getting Objects) by using the ResponseHeaderOverrides object and setting the
corresponding request property, as shown in the following Java code sample.
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);Example
The following Java code example retrieves an object from a specified Amazon S3 bucket. For instructions on how to create and test a working sample, see Testing the Java Code Examples
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.PropertiesCredentials;
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 S3Sample {
private static String bucketName = "*** Provide bucket name ***";
private static String key = "*** Provide Key ***";
public static void main(String[] args) throws IOException {
AmazonS3 s3Client = new AmazonS3Client(new PropertiesCredentials(
S3Sample.class.getResourceAsStream(
"AwsCredentials.properties")));
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();
}
}