Create a public S3 bucket using the aws cli

Given is an application requiring public network storage.
I’ve deciced to give AWS S3 a try.
My given setup options are:

  • aws-cdk aka Infrastructure as code
  • boto3
  • aws cli bash script

Due to the non-repeating nature of that job, I’m going with the aws cli bash script

Create an arbitrary bucket name

bucket=$(python -m uuid)
echo $bucket
cd643901-34ab-48ec-9435-5ffd23c0580b

Create the bucket

aws s3 mb \
s3://$bucket \
--region us-east-1
make_bucket: cd643901-34ab-48ec-9435-5ffd23c0580b

Delete public access block

The necessity of this step requires further investigation

aws s3api delete-public-access-block --bucket \
$bucket 

Create a policy to allow public read access on the bucket

/tmp/policy.json:

{
  "Statement":[
    {
      "Sid": "AddPerm",
      "Effect": "Allow",
      "Principal": "*",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::cd643901-34ab-48ec-9435-5ffd23c0580b"
    }
  ]
}

Apply the policy to the bucket

aws s3api put-bucket-policy --bucket \
$bucket \
--policy file:///tmp/policy.json

Create an example file

echo foobar >> /tmp/foo.txt

Upload the example file to our bucket

aws s3 cp /tmp/foo.txt \
s3://$bucket

[Assert] Download the file using wget

wget -O /tmp/downloaded_foo.txt \
"https://$bucket.s3.us-east-1.amazonaws.com/foo.txt"

Delete all files in the bucket

aws s3 rm \
s3://$bucket/*

Delete the bucket

aws s3api delete-bucket --bucket \
$bucket