How To Upload the profile images to AWS S3 Bucket

 



Lia infraservices-being an AWS partner company, we always use AWS cloud migration services to ensure durable application for our customers. The AWS cloud migration is the path to reduce the TCO and increase the productivity of an organization.



We have decided to use AWS S3 and the AWS Cognito service for any user to store their profile picture while registering into the mobile application.

What we have done:

  1. Created an S3 bucket in the Bahrain region as our customer was from the Middle east, and so to reduce the latency, we wanted to have all the services in that region.

  2. Set up the permissions necessary like bucket policy and CORS.

How to upload the profile images to AWS S3 bucket

{

  “Version”: “2012-10-17”,

   “Id”: “Policy1626866950426”,

   “Statement”: [

       {

           “Sid”: “Stmt1626866945113”,

           “Effect”: “Allow”,

           “Principal”: “*”,

           “Action”: “s3:*”,

           “Resource”: “arn:aws:s3:::bucket_name/*”

       }

   ]

}

CORS

[

   {

       “AllowedHeaders”: [

           “*”

       ],

       “AllowedMethods”: [

           “PUT”,

           “POST”,

           “DELETE”

       ],

       “AllowedOrigins”: [

           “*”

       ],

       “ExposeHeaders”: []

   },

   {

       “AllowedHeaders”: [],

       “AllowedMethods”: [

           “GET”

       ],

       “AllowedOrigins”: [

           “*”

       ],

       “ExposeHeaders”: []

   }

]

Therefore Once the bucket was created, we had to enable AWS Cognito to allow the user to authenticate to access AWS resources.

From the Cognito console, selected “Manage Identity Pool”

Gave a name to the identity pool

Under “Unauthenticated identities“ Enabled the access to unauthenticated identities

Created the Pool.

That automatically created two IAM roles

  1.            Unauthenticated role

  2.            Authenticated role 

Note: To make the permission even granular, we can add the S3 bucket the Cognito to have permission to. But, in our case, we did not need to do any changes to the existing inline policy.

Once all are set correctly, provided the information to the developer as

BUCKET_NAME=”Bucket_name”

ENDPOINT=”https://s3.me-south-1.amazonaws.com”

BASE_S3_URL=”https://s3.me-south-1.amazonaws.com/bucket_name/”

POOL_ID=”me-south-1:ec9f9bfe-5c19-419a-91bc-f757957d080e”

The Android developers updated that into their code,

Once they upload the code and tried to test, they were getting the error as below

IdentityPool ‘me-south-1:ec9f9bfe-5c19-419a-91bc-f757957d080e’ not found. (Service: AmazonCognitoIdentity; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: a9cb68-9e9a-4693-aa34-9ca45e)

After some research, it was found that the S3 service in the Bahrain region does not accept any data transfer, it needs some other component to get that done.

To reduce the overhead, we decided to move the S3 bucket and Cognito to other supported AWS regions and then the code was working fine.

The application was able to upload the user profile during the registration.

Conclusion 

Lia infraservices AWS cloud migration Ensure data integrity & security as our primary focus.  Migrate Your Data To A More Safe and Secure Cloud Platform using Lia infraservices cloud migration services

Click on the link to know more about the AWS cloud migration services

REFERENCE

To know more about how to Add a bucket policy using the Amazon S3 console


Comments

Popular posts from this blog

Top 15 Mobile App Design Trends in 2022

9 Secret Techniques to do Off-Page SEO in 2022

How to build a Web application like an Enterprise based Application?