5. Store And Retrieve Files securely from Amazon S3
Let us first look at how a typical app is created using AWSS3 SDK and then look at the components of the S3 app that get affected using Bayun AWSS3 wrapper classes.
5.1 TransferUtility S3 app code snippets using standard AWSS3 SDK:
- Add the following to the import statements:import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver; import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
Create a credentials provider so you can easily integrate AWS S3 with your Android application. You pass the credentials provider object to the constructor of the AWS client you are using. The credentials provider looks like this:
5.1.1 Initialize the S3 TransferUtility
First, pass your Amazon credentials provider to the AmazonS3Client constructor. Then, pass the client to the TransferUtility constructor along with the application context:
5.1.2 Upload a File to Amazon S3
To upload a file to S3, instantiate a TransferObserver object. Call upload() on your TransferUtility object and assign it to the observer, passing the following parameters:
- bucket_name – Name of the S3 bucket to store the file
- key – Name of the file, once stored in S3
- file – java.io.File object to upload
Uploads automatically use S3’s multi-part upload functionality on large files to enhance throughput.
5.1.3 Download a File from Amazon S3
To download a file from S3, instantiate a TransferObserver object. Call download() on your TransferUtility object and assign it to the observer, passing the following parameters:
- bucket_name – A string representing the name of the S3 bucket where the file is stored
- key – A string representing the name of the S3 object (a file in this case) to download
- file – the java.io.File object where the downloaded file will be written
5.1.4 Tracking S3 Transfer Progress
With the TransferUtility, the download() and upload() methods return a TransferObserver object. This object gives access to:
- The state (now specified as an enum)
- The total bytes transferred thus far
- The total bytes to transfer (for easily calculating progress bars)
- A unique ID that you can use to keep track of distinct transfers
Given the transfer ID, this TransferObserver
object can be retrieved from anywhere in your app, including if the app is killed. It also lets you create a TransferListener
, which will be updated on state or progress change, as well as when an error occurs.
To get the progress of a download or upload, call setTransferListener()
on your TransferObserver
. This requires you to implement onStateChanged
, onProgressChanged
, and onError
. For example:
5.2 SecureTransferUtility 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:
As you can see from the code snippets below, in general it should be possible to simply query-replace the following type-names appropriately to their secure versions, and in most situations that should be sufficient.
- AmazonS3Client --> SecureAmazonS3Client
- TransferUtility --> SecureTransferUtility
Add the following to the import statements: