
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.
and for us non programmers? is there an extant app for this on android?
No
Useful starting place, thank you. In implementing this I found that new TransferUtility() is now deprecated – replace it with
TransferUtility transferUtility = TransferUtility.builder().context(cx).s3Client(s3Client).build();
Also you need to have the s3 transderutility service declared in the project Manifest
I have a question, that what is session token and how can I find it in s3 bucket ?
We are getting the session token from our backend. The backend has to use the AWS STS(Security Token Service) to get the session tokens.
But how i get pathToStore? i am not understanding what will be actual file path to access the file. And uploaded file name will be same as in local name?
Path to store:
It is the combination of the folder path and the file name. You can specify a folder path which will be created in your bucket, if it isn’t present already. The file name is the name of the file that you have locally. Once the file is uploaded, it will be available under the folder path that you have specified.
Actual file path to access the file:
folder path + filename is the actual path to access the file.
And Yes, uploaded file name will be the same as in local name.