Mallow's Blog

How to upload files from an Android app to aws s3

In this blog, we will see about how to upload the files from android app to aws s3. We need to implement the feature that stores the files (like user profile image, user documents etc., into the server. We can simply store the files into the aws s3 bucket rather than to our own server. Here the step by step process to integrate the aws s3 bucket and upload the files.

1. Include the aws dependencies to the app level build.gradle

implementation 'com.amazonaws:aws-android-sdk-core:2.6.+'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.2.+'
implementation 'com.amazonaws:aws-android-sdk-s3:2.6.+'
implementation 'com.amazonaws:aws-android-sdk-ddb:2.2.+'

2. Import the following files

import com.amazonaws.auth.BasicSessionCredentials;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;

3. Code to upload the files

a) Create the amazon s3 client:

AmazonS3 s3Client = new AmazonS3Client(new BasicSessionCredentials(awsAccessKey, awsSecretKey, sessionToken));

b)Set the region for s3Client :

s3Client.setRegion(Region.getRegion(Regions.US_WEST_2));

c) Create the TransferUtility to upload the file :

TransferUtility transferUtility = new TransferUtility(s3Client, context);

d) Upload the file : 

TransferObserver transferObserver = transferUtility.upload(bucketName, pathToStore, file, CannedAccessControlList.PublicRead);

(Note : If you want the file url read by public, include CannedAccessControlList.PublicRead value.)

e) To know whether the file is uploaded successfully or not :

Include the TransferObserver to listen the file upload status.

transferObserver.setTransferListener(new TransferListener() {

    @Override

    public void onStateChanged(int id, TransferState state) {

//Implement the code for handle the file status changed.

   }

    @Override

    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

        //Implement the code to handle the file uploaded progress.

    }

    @Override

    public void onError(int id, Exception exception) {

//Implement the code to handle the file upload error.

  }

});

You can access the file by the URL: https://s3-cn-north-1.amazonaws.com/bucketName/pathToStore.

Conclusion:

we have seen about the complete solution for upload the files into AWS S3 securely and efficiently. It is the easy service to store the file and retrieve anywhere from the web.


Sivavishnu R,
Android Team,
Mallow Technologies.

 

Leave a Reply

%d bloggers like this: