6. Operations on Objects

Let us first look at how a typical app is created using AWSS3 SDK AmazonS3Client and then look at the components of the S3 app that get affected using Bayun AWSS3 wrapper class SecureAmazonS3Client.

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();

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));

6.2 SecureAmazonS3Client s3 app code snippets :

Now in order to use Bayun’s S3Wrapper instead of the standard AWS S3 SDK classes, these code snippets above change to using “Secure” versions of the corresponding classes, as below:
  • AmazonS3Client --> SecureAmazonS3Client
6.2.1 Downloading Objects using Bayun's SecureAmazonS3Client :
SecureAmazonS3Client s3 = new SecureAmazonS3Client(credentialsProvider, appContext);       
S3Object object = s3.getObject(
                  new GetObjectRequest(bucketName, key));
InputStream objectData = object.getObjectContent();
// Process the objectData stream.
objectData.close();

GetObjectRequest rangeObjectRequest = new GetObjectRequest(
        bucketName, key);
rangeObjectRequest.setRange(0, 10); // retrieve 1st 11 bytes.
S3Object objectPortion = secureAWSS3Client.getObject(rangeObjectRequest);

InputStream objectData = objectPortion.getObjectContent();
// Process the objectData stream.
objectData.close();
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.2.2 Uploading Objects using Bayun's SecureAmazonS3Client :
SecureAmazonS3Client s3 = new SecureAmazonS3Client(credentialsProvider, appContext);   
s3.putObject(new PutObjectRequest(bucketName, keyName, file));

results matching ""

    No results matching ""