6.1 AmazonS3Client s3 app code snippets using standard AWSS3 SDK

Get an Object Using the AWS SDK for android

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:

The following Java code sample demonstrates the preceding tasks.

6.1.1 Downloading Objects using standard AmazonS3Client S3

AmazonS3Client s3 = new AmazonS3Client(credentialsProvider, appContext);       
S3Object object = s3.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.

GetObjectRequest rangeObjectRequest = new GetObjectRequest(
        bucketName, key);
rangeObjectRequest.setRange(0, 10\); // retrieve 1st 11 bytes.
S3Object objectPortion = s3.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);

6.1.2 Uploading Objects using standard AmazonS3Client S3

AmazonS3Client s3 = new AmazonS3Client(credentialsProvider, appContext);       
s3.putObject(new PutObjectRequest(bucketName, keyName, file));

Last updated