How to Upload Files to AWS S3 Using Android

Amazon Web Services (AWS) offers Simple Storage Service (S3), a scalable, secure, and highly available storage solution widely used for storing and managing data in the cloud. If you’re developing an Android app and need to upload files (such as images, videos, documents, etc.) to AWS S3, there are various ways you can accomplish this.

In this guide, we will walk you through the process of uploading files to AWS S3 from an Android app using the AWS SDK for Android and Amazon Cognito for authentication. By the end, you'll have a working knowledge of how to handle file uploads to AWS S3 using Android.


Prerequisites

Before you start uploading files to S3 from your Android device, make sure you have the following:

  1. AWS Account: Sign up for an AWS account if you don’t have one already.
  2. AWS IAM Role and Permissions: Set up an AWS Identity and Access Management (IAM) role with the necessary permissions to access the S3 bucket.
  3. AWS S3 Bucket: Create an S3 bucket in your AWS account where files will be uploaded.
  4. AWS SDK for Android: The AWS SDK for Android provides the tools necessary for Android apps to interact with AWS services.
  5. Android Studio: Ensure that you have Android Studio set up on your machine.

Step 1: Set Up Your AWS S3 Bucket

To upload files to AWS S3, you first need to have an S3 bucket created in your AWS account:

  1. Log into AWS Console and navigate to S3.
  2. Click on the Create Bucket button.
  3. Choose a unique name for your bucket (for example, myapp-upload-bucket) and select a region.
  4. Set the bucket's permissions according to your needs. For development purposes, you may want to allow public access, but for production, it’s advised to keep it private and manage permissions with IAM roles.
  5. Click Create Bucket once all settings are configured.

Step 2: Set Up AWS IAM Role for Permissions

You will need an IAM role with permissions to interact with the S3 bucket from your Android app:

  1. In the AWS Console, go to IAM > Roles > Create role.
  2. Select AWS service and choose Lambda (for testing purposes) or EC2, depending on your use case.
  3. Attach the AmazonS3FullAccess policy (or create a custom policy with restricted permissions, such as only allowing uploads to the specific bucket).
  4. Review and give the role a meaningful name, then click Create role.

You can use AWS Cognito or AWS IAM for managing your credentials securely in the app.


Step 3: Add AWS SDK Dependencies in Android Studio

Next, you need to add the necessary dependencies for AWS SDK in your Android app project:

  1. Open your build.gradle (Module: app) file and add the following dependencies in the dependencies block:
gradle
dependencies { implementation 'com.amazonaws:aws-android-sdk-core:2.16.12' implementation 'com.amazonaws:aws-android-sdk-s3:2.16.12' }
  1. Sync your project to download the required libraries.

Step 4: Configure AWS SDK in Your Android App

Now, you need to initialize the AWS SDK and set up the necessary credentials. For simplicity, we'll use Cognito Identity for authentication. Cognito allows you to authenticate users without directly managing AWS credentials on the device.

Step 4.1: Set Up Cognito Identity Pool

  1. Go to the Amazon Cognito Console.
  2. Create a new Cognito Identity Pool.
  3. Enable access to unauthenticated identities if you don’t have a user management system (or integrate with a custom authentication system if needed).
  4. Attach the IAM role you created earlier that allows access to S3.
  5. Copy the Identity Pool ID and save it.

Step 4.2: Configure AWS Credentials in Android

In your Android app, configure the AWS credentials:

  1. In your MainActivity.java (or the appropriate activity), initialize AWS with your Cognito Identity Pool:
java
import com.amazonaws.auth.AWSCredentialsProvider; import com.amazonaws.auth.CognitoCachingCredentialsProvider; import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility; import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver; import com.amazonaws.regions.Regions; import com.amazonaws.services.s3.AmazonS3Client; public class MainActivity extends AppCompatActivity { private AmazonS3Client s3Client; private TransferUtility transferUtility; private String bucketName = "your-s3-bucket-name"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the Cognito credentials provider CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider( getApplicationContext(), "your-identity-pool-id", // Identity Pool ID from Cognito Regions.US_EAST_1 // Specify your region ); // Initialize the S3 client s3Client = new AmazonS3Client(credentialsProvider); // Set up the transfer utility for file uploads transferUtility = TransferUtility.builder() .context(getApplicationContext()) .s3Client(s3Client) .build(); } }

Step 4.3: Set Permissions in AndroidManifest.xml

Add the necessary permissions to your AndroidManifest.xml:

xml
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Step 5: Upload a File to AWS S3

Now, let’s upload a file to the S3 bucket from your Android device. You can upload files such as images, text files, or videos.

Step 5.1: Create a File Upload Function

Inside your activity or fragment, create a function to handle the file upload:

java
public void uploadFileToS3(File file) { // Create a TransferObserver to monitor the upload TransferObserver observer = transferUtility.upload( bucketName, // S3 bucket name "uploads/" + file.getName(), // S3 object key (file path in the bucket) file // The file to upload ); observer.setTransferListener(new TransferListener() { @Override public void onStateChanged(int id, TransferState state) { Log.d("S3Upload", "State: " + state.toString()); // Handle upload state changes (e.g., progress, completed) } @Override public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) { // Handle upload progress Log.d("S3Upload", "Progress: " + bytesCurrent + "/" + bytesTotal); } @Override public void onError(int id, Exception ex) { // Handle errors Log.e("S3Upload", "Error uploading file", ex); } }); }

Step 5.2: Trigger the File Upload

To trigger the upload, you need to select a file from the device. This can be done using Android's Intent system for file selection:

java
private static final int PICK_FILE_REQUEST = 1; @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); startActivityForResult(intent, PICK_FILE_REQUEST); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_FILE_REQUEST && resultCode == RESULT_OK) { Uri fileUri = data.getData(); File file = new File(getRealPathFromURI(fileUri)); uploadFileToS3(file); } } private String getRealPathFromURI(Uri uri) { String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = getContentResolver().query(uri, projection, null, null, null); int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(columnIndex); }

Step 6: Testing and Debugging

After implementing the above steps, test your Android app to make sure that files are being uploaded correctly to your AWS S3 bucket. Check the S3 Console to see if the files appear in your bucket.

  • Monitor logs in Android Studio for debugging (Log.d("S3Upload", ...)).
  • Ensure your S3 permissions are correctly configured for access and upload.
  • Use AWS CloudWatch logs to debug issues related to your AWS resources.

Conclusion

Uploading files to AWS S3 from an Android app is a powerful way to manage data storage in the cloud. With the help of the AWS SDK for Android and Cognito Identity Pools for secure authentication, you can easily upload files such as images, documents, and videos to S3.

By following the steps outlined in this guide, you can integrate AWS S3 file uploads into your Android application, giving you the flexibility to manage large amounts of data while ensuring scalability, security, and availability in the cloud.