Store and Retrieve Files with Amazon S3 Transfer Utility
Amazon Simple Storage Service (Amazon S3) provides mobile developers with secure, durable, highly-scalable object storage. Amazon S3 is easy to use, with a simple web services interface to store and retrieve any amount of data from anywhere on the web.
The AWS Mobile SDK allows you to consume the S3 service in your mobile application via the S3 Transfer Utility, which is replacing the S3 Transfer Manager as of AWS Mobile SDK for Android v 2.2.4. For information on migrating from the S3 Transfer Manger to the S3 Transfer Utility, see Migrating from the Transfer Manager to the Transfer Utility on the AWS Blog.
The tutorial below explains how to integrate the S3 TransferUtility, a high-level utility for using S3 with your app. This tutorial assumes you have already created an S3 bucket. To create an S3 bucket, visit the S3 AWS Console.
You must complete all of the instructions on the Set Up the SDK for Android page before beginning this tutorial.
The default IAM role policy grants your application access to Amazon Mobile Analytics and Amazon Cognito Sync. In order for your Cognito identity pool to access Amazon S3, you must modify the identity pool's roles.
Navigate to the Identity and Access Management Console and click Roles in the left-hand pane.
Type your identity pool name into the search box. Two roles will be listed: one for unauthenticated users and one for authenticated users.
Click the role for unauthenticated users (it will have unauth appended to your Identity Pool name).
Click the Create Role Policy button, select Policy Generator, and then click the Select button.
On the Edit Permissions page, enter the settings shown in the following image. The Amazon Resource Name (ARN) of an S3 bucket looks like
arn:aws:s3:::examplebucket/*and is composed of the region in which the bucket is located and the name of the bucket. The settings shown below will give your identity pool full to access to all actions for the specified bucket.
Click the Add Statement button and then the Next Step button.
The Wizard will show you the configuration that you generated. Click the Apply Policy button.
For more information on granting access to S3, see Granting Access to an Amazon S3 Bucket.
Add the following declaration to your
<service android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService" android:enabled="true" />
First, pass your Amazon Cognito credentials provider to the S3 client constructor. Then, pass the client to the TransferUtility constructor along with the application context:
AmazonS3 s3 = new AmazonS3Client(credentialsProvider); TransferUtility transferUtility = new TransferUtility(s3, getApplicationContext());
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
TransferObserver observer = transferUtility.upload( MY_BUCKET, /* The bucket to upload to */ OBJECT_KEY, /* The key for the uploaded object */ MY_FILE /* The file where the data to upload exists */ );
Uploads automatically use S3's multi-part upload functionality on large files to enhance throughput.
To download a file from S3, instantiate a
TransferObserver object. Call
TransferUtility object and assign it to the observer, passing the following
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
TransferObserver observer = transferUtility.download( MY_BUCKET, /* The bucket to download from */ OBJECT_KEY, /* The key for the object to download */ MY_FILE /* The file to download the object to */ );
For more information about accessing Amazon S3 from an Android application, see Store and Retrieve Files with Amazon S3.