Table of Contents
Setup AWS SDK in Android for S3 Upload/Download
Setup S3 Bucket
- Firstly, go to the Amazon S3 Console and click on
- Type in the bucket name.
- Select the region for which you want the bucket to be created and click on
- Grant public access to the bucket if you wish to access it from anywhere and through any authenticated mode.
- Now store this bucket name and region safely. If you have a backend, you could store it there using a RESTful API
Setup an Identity Pool Id
- Before going to the below link, make sure you are in the right region. For eg: Asia Pacific (Mumbai) ap-south-1 for India
- Go to AWS Cognito and choose
Manage Identity Pools
Create New Identity Poolbutton on the top left of the console.
- Give a name for the Identity pool and check
Enable access to unauthenticated identitiesunder the
Unauthenticated Identitiessection, click
Create poolbutton on the bottom right.
- To enable Cognito Identities to access your resources, expand the
View Detailssection to see the two roles that are to be created. Make a note of the
unauthrole whose name is of the form
Cognito_<IdentityPoolName>Unauth_Role. Now click
Allowbutton in the bottom right of the console.
Get AWSCredentialssection, in the code snippet to create
CognitoCachingCredentialsProvider, find the Identity pool ID and the AWS region and make note of them. You will need to add to the sample application later.
- Go to Amazon IAM Console and select “Roles”.
- Select the
unauthrole you just created in step 1, which is of the form
Attach Policy, then find
AmazonS3FullAccessand attach it it to the role.
- Note: This will grant users in the identity pool full access to all buckets and operations in S3. In a real app, you should restrict users to only have access to the resources they need.
Setup AWS SDK in Android
Add the following libraries in your
Make sure following permissions are present in your project’s
Create a single instance of
AmazonS3Clientthroughout your application by adding below lines in your
Application Class. You’ll need to pass the
IdentityPoolId that we created above.
Upload a File
Well there you go, now you know how to setup a S3 bucket, configure the AWS SDK for android and upload an image.
If you have any questions or suggestions, please post them below. I would be more than happy to receive feedback from the community.