Uploading files to an AWS S3 bucket is a common task in many apps. But it can be tricky, especially if you want to preserve the file extension. That's because when you use a PUT request with a pre-signed URL, the file extension may not be retained during the upload process. This can lead to complications, such as triggering specific actions based on file formats.
I created a library to make file uploads to S3 easier
To address this issue, I created an Android library. It's a simple, easy-to-use and makes it a breeze to upload files to AWS S3 while keeping the file extension intact.In this article, we'll dive into the details of this library and guide you through the steps to seamlessly upload files to AWS S3
Here's how it works
Step 1: Setting Up the Basics
Begin by logging into the AWS Management Console and navigating to the S3 service. Create a new S3 bucket to store your files securely.
Step 2: Establishing Identity Pool
Search for "Cognito" in the management console and access "Federated Entities."
Enable access to unauthenticated identities to allow seamless and secure access.
Step 3: Granting Permissions
Access the AWS IAM Console and select "Roles" from the side menu.
Search for the role named "Cognito_appnameUnath_Role" (replace "appname" with your Identity Pool name).
Attach the "AmazonS3FullAccess" policy to this role to grant necessary permissions.
Step 4: Integrating Dependencies
Add JitPack repository to your project's settings.gradle file.
Include the library dependency for the AWS-S3-Upload-Library in your app's Gradle file.
Initialize the CognitoCachingCredentialsProvider with your Identity Pool ID and region.
Create an object that holds your AWS keys, including Cognito Pool ID, bucket name, and regions.
Instantiate the S3Uploader object using the previously defined keys.
Use the uploadFile function to upload your desired file to S3, preserving the file extension.
val s3uploaderObj = S3Uploader(
context,
AWSKeys.BUCKET_NAME,
AWSKeys.COGNITO_POOL_ID,
AWSKeys.COGNITO_REGION,
AWSKeys.BUCKET_REGION
)
fun uploadVideoToS3(videoUri: Uri, context: Context): String {
val fileUrl = s3uploaderObj.uploadFile(
videoUri.path!!,
"video/mp4",
true
)
return fileUrl
}
Step 6: Listening to Upload Events
Implement an event listener to capture upload events such as progress, success, error, and state changes. This ensures better control and feedback during the upload process.
Uploading files to AWS S3 no longer needs to be a complex endeavour with the AWS-S3-Upload-Library, it becomes easy to upload files to S3 while keeping the file extension intact. So you can be sure that your files will be uploaded correctly, without any problems.
A library that makes upload to AWS S3 bucket fasteasy, without any issues with missing file extension.
AWS-S3-Upload-Library
A library that makes upload to AWS S3 bucket fasteasy, without any issues with missing file extension.
Follow the steps below:
Step 1: Login to AWS management console and go to S3:
Create an s3 bucket
Step 2: Create identity pool:
Search for cognito from the management console
Click on federated entities
Enable access to unauthenticated identities
Open federated entities to create a new identity pool
Click on create new identity pool
Enter your App name for identity pool name and enable access to unauthenticated identities and click on create pool.
Click on the Allow button to create two default roles associated with your identity pool: one for unauthenticated users and one for authenticated users
Your identity pool id will be shown to you in the code snippet copy and save for later.