DEV Community

Cover image for Simplifying AWS S3 File Uploads with the Android Library
mavinomichael
mavinomichael

Posted on

Simplifying AWS S3 File Uploads with the Android Library

Uploading files to AWS S3 can be tricky

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.

Login to AWS management console and go to S3:

Step 2: Establishing Identity Pool

  1. Search for "Cognito" in the management console and access "Federated Entities."
  2. Enable access to unauthenticated identities to allow seamless and secure access.

Create identity pool:

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.

Step 3: Granting Permissions

  1. Access the AWS IAM Console and select "Roles" from the side menu.
  2. Search for the role named "Cognito_appnameUnath_Role" (replace "appname" with your Identity Pool name).
  3. Attach the "AmazonS3FullAccess" policy to this role to grant necessary permissions.

Grant permission to S3 bucket:

Search for Cognito_appnameUnath_Role replace app name with your Pool Id name

Select Unauth role and click on add permissions to reveal attach policy option, click on attach policy.

Search for AmazonS3FullAccess

Attach policy or permission

Step 4: Integrating Dependencies

  1. Add JitPack repository to your project's settings.gradle file.
  2. Include the library dependency for the AWS-S3-Upload-Library in your app's Gradle file.
implementation 'com.github.mavinomichael:AWS-S3-Upload-Library:1.0.1'

Enter fullscreen mode Exit fullscreen mode

Step 5: Utilizing the S3Uploader Object

  1. Initialize the CognitoCachingCredentialsProvider with your Identity Pool ID and region.
  2. Create an object that holds your AWS keys, including Cognito Pool ID, bucket name, and regions.
  3. Instantiate the S3Uploader object using the previously defined keys.
  4. 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
}

Enter fullscreen mode Exit fullscreen mode

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.

s3uploaderObj.setOnUploadListener(object : S3UploadListener {
    override fun onSuccess(id: Int, response: String?) {
        // Handle upload success
    }

    override fun onError(id: Int, response: String?) {
        // Handle upload error
    }

    override fun onProgress(id: Int, progress: Int) {
        // Handle upload progress
    }

    override fun onStateChanged(id: Int, state: UploadState?) {
        // Handle upload state changes
    }
})

Enter fullscreen mode Exit fullscreen mode

Conclusion

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.

GitHub logo mavinomichael / AWS-S3-Upload-Library

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:

  1. Create an s3 bucket

Screenshot 2023-04-25 at 15 01 07

Step 2: Create identity pool:

  1. Search for cognito from the management console
  2. Click on federated entities
  3. Enable access to unauthenticated identities

Screenshot 2023-04-25 at 15 06 21

Open federated entities to create a new identity pool

Screenshot 2023-04-25 at 15 09 13

Click on create new identity pool

Screenshot 2023-04-25 at 15 13 27

Enter your App name for identity pool name and enable access to unauthenticated identities and click on create pool.

Screenshot 2023-04-25 at 15 15 58

Click on the Allow button to create two default roles associated with your identity pool: one for unauthenticated users and one for authenticated users

Screenshot 2023-04-25 at 15 19 55

Your identity pool id will be shown to you in the code snippet copy and save for later.

Screenshot 2023-04-25 at 15 22 30

Step 3: Grant permission to S3 bucket:

  1. Go to AWS IAM CONSOLE.
  2. Select Roles from…

Sentry blog image

The countdown to March 31 is on.

Make the switch from app center suck less with Sentry.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay