- Published on
Setting up S3 bucket to post and get docs
Learn how to store your web app's image files in an s3 bucket by uploading them directly to the bucket. This technique is especially useful in serverless environemnts like when you're using lambda functions or a Next.js application.
Setting up S3 bucket
Go to S3 and click on Create bucket, then fill out the Bucket name. Remember this name because we’ll need it for the next step.
Select a Region. You should enter the location (eg. “US East (N. Virginia)”) that is nearest to your users for better performance.
Scroll down a little until you get to the Permissions panel. When you set up the permissions, make sure that you allow public ACLs, otherwise uploads will fail. Uncheck Block all public access and check the bottom two checkboxes. You’ll also have to acknowledge that your settings may make the bucket public in the warning at the top.
Creating a policy
- Sign in to AWS Management Console 24 and search for the “IAM” service to access the AWS Identity and Access Management (IAM)
- First, click Policies in the sidebar. Then, click on Create Policy and choose the JSON tab
{
"Version": "2012-10-17",
"Id": "Policy1668112278294",
"Statement": [
{
"Sid": "Stmt1668112275094",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}
- Then click on Review Policy and fill out the Name field, and Create Policy.
Creating a user account
- Now that you’ve created the right policy, you’re ready to create the user account. Click on the Users link on the left side of the IAM console
- Type in a descriptive user name and make sure the “Programmatic access” checkbox is checked.
- The next step in the process is to configure the user’s permissions. Click on the button that says Next: Permissions and then click on «Attach existing policies directly».
- Now search for the policy name you created in the previous step. When you find it, select the checkbox and click on Next: Tags, then Next: Review, then finally Create user.
- Here’s the critical step: Make sure you either download the credentials (Download .csv) or you copy and paste somewhere safe both Access key ID and Secret access key values.
- Enjoy! Your S3 bucket is ready to receive and serve images.
Checkout my code snippet to directly upload images to the S3 container from the browser.