Logo

dev-resources.site

for different kinds of informations.

Simplifying AWS S3 File Uploads with the Android Library

Published at
8/9/2023
Categories
android
s3
aws
upload
Author
mavino
Categories
4 categories in total
android
open
s3
open
aws
open
upload
open
Author
6 person written this
mavino
open
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…
upload Article's
30 articles in total
Favicon
UploadThing: A Modern File Upload Solution for Next.js Applications
Favicon
NestJS - Armazenamento nas nuvens
Favicon
NestJS - criar um endpoint para upload de diversos arquivos
Favicon
NestJS - Validando o envio de arquivos
Favicon
NestJS - Armazenamento local de upload
Favicon
NestJS - criar endpoint para upload de 1 arquivo
Favicon
Next.js: Upload de imagem para a Cloudflare R2 Utilizando Presigned URL
Favicon
A file uploader built with shadcn/ui and react-dropzone
Favicon
Elevate Your GraphQL API: Mastering File Uploads with Yoga GraphQL
Favicon
Mastering File Uploads in Laravel 11: A Comprehensive Guide
Favicon
How To Upload Multiple File In Laravel 10
Favicon
Different approaches to reduce AWS S3 file upload time using AWS-SDK v3 in NodeJS.
Favicon
How would you handle image upload?
Favicon
Upload File di Node JS
Favicon
Uploading Images with Node.js and React.js
Favicon
Simplifying AWS S3 File Uploads with the Android Library
Favicon
Laravel 10 Crop Image Before Upload Cropper Js
Favicon
Enhance Job Seeker Experience: Quick Image Upload for Portals
Favicon
A file upload sample with NextJs
Favicon
Azure Storage Account: Unleash the Potential of Cloud-Based Storage
Favicon
Spheron Network: Browser Upload
Favicon
Uploading files from terminal on file hosting service, and unlimited cloud storage
Favicon
Process FormData file sent to Node server
Favicon
Express file upload using Multer
Favicon
Maximize Your Client Upload Efficiency with the Bulk Upload
Favicon
Upload images in Ckeditor 5 with Laravel
Favicon
Eteot Logo
Favicon
Install & Upload WordPress Plugin
Favicon
Laravel 9 Multiple Image Upload Example
Favicon
How To Enhance JavaScript File Upload In Your Web App

Featured ones: