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:
1
Create an instance of the AmazonS3Client class.
2
Execute one of the AmazonS3Client.getObject() method. You need to provide the request information, such as bucket name, and key name. You provide this information by creating an instance of the GetObjectRequest class.
3
Execute one of the getObjectContent() methods on the object returned to get a stream on the object data and process the response.
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();
val s3 = AmazonS3Client(credentialsProvider, appContext)
val `object`: S3Object = s3.getObject(GetObjectRequest(bucketName, key))
val objectData: InputStream = `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();
val rangeObjectRequest = GetObjectRequest(bucketName, key)
rangeObjectRequest.setRange(0, 10)
val objectPortion : S3Object = s3.getObject(rangeObjectRequest)
val objectData: InputStream = 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);
val request = GetObjectRequest(bucketName, key)
val responseHeaders = 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));
val s3 = AmazonS3Client(credentialsProvider, appContext)
s3.putObject(PutObjectRequest(bucketName, keyName, file))