Image Uploads with AWS S3, Elixir + Phoenix (:ex_aws) — Step 2
A Complete Step-by-Step Guide
Road Map
- Step 1 AWS (IAM) — Create an IAM user with permissions to upload and read from S3 (Full Access)
- Step 2 AWS (S3) — Create an S3 bucket with the correct permissions and policies so that it can be accessed by our application
- Step 3 Application Code — Write code to facilitate the image upload within your Elixir/Phoenix application
AWS (S3)
Hello again! And welcome to Step 2 of Image Uploads with S3, Elixir + Phoenix.
In Step 1 we created a new IAM user with permissions to upload to, and read from, AWS S3. By the end of this step you will have created an S3 bucket that used to store all of your images.
Let’s get started!
Firstly, sign in to your AWS account and navigate to the S3 service. The service can be found just as we did the IAM service in Step 1. If you haven’t created an AWS account yet you can do so here.
Click on the blue “Create bucket” button just below the search bar. You will then see a modal which will ask you to create some details about the bucket you want to create. Enter a bucket name and select a region that you want the bucket to apply to and then click “Create”:
You should see the newly created bucket in your dashboard. Click on it:
(PLEASE NOTE: It isn’t recommended that you give public access to your buckets. This setup is for learning purposes only. For more details on bucket permissions go to https://aws.amazon.com/premiumsupport/knowledge-center/secure-s3-resources/)
Click on the “Permissions” tab at the top and then click on “Everyone”. Select the tick boxes for read only and then click “Save”. You should see a user that already has read and write access at the top:
Next click on the “Bucket Policy” button beneath the tabs. It will then prompt you to enter a bucket policy. Enter the following, replacing “your_bucket_name” with the name you chose for your bucket, and then press “Save”:
{
“Version”: “2012–10–17”,
“Statement”: [
{
“Sid”: “AddPerm”,
“Effect”: “Allow”,
“Principal”: “*”,
“Action”: [
“s3:GetObject”,
“s3:PutObject”
],
“Resource”: “arn:aws:s3:::your_bucket_name/*”
}
]
}
Here is a link to the bucket policy that you can copy and paste into your AWS console: https://pastebin.com/PMrdkxZs
Cool! We have now successfully created an S3 bucket with the correct permissions and policies that will allow us to access it within our application. Check out Step 3 where we will write the application code that will interact with our newly created bucket!