How to Manage S3 Buckets with the AWS CLI

The AWS CLI manages S3 buckets with aws s3 for everyday jobs (list, create, copy, delete) and aws s3api for everything else, one command per S3 API call. Below: listing buckets, checking a bucket’s access settings, creating one in a chosen region, and adding, listing and removing files.

You need the CLI installed and signed in first: on Windows or on Ubuntu. The commands are the same in PowerShell and in a Linux shell, which is how I ran them here.

List Your Buckets

PowerShell
aws s3 ls
aws s3 ls listing the S3 buckets in an AWS account

This lists every bucket in the account, whatever its region. To list what is inside one, add its name: aws s3 ls s3://my-bucket --recursive --human-readable --summarize.

Check Who Can Access a Bucket

PowerShell
# Change my-bucket to your bucket name
aws s3api get-public-access-block --bucket my-bucket
aws s3api get-bucket-acl --bucket my-bucket
aws s3api get-bucket-acl showing the owner and grants of an S3 bucket

The public access block is the setting that matters first: all 4 values true means nothing in the bucket can be made public by mistake. That command shows the bucket’s own setting; if it errors with NoSuchPublicAccessBlockConfiguration, nothing is set on the bucket itself and only the account-level setting applies. Since April 2023, new buckets have ACLs switched off by default, so on a new bucket get-bucket-acl just shows the owner with full control, and access is controlled by bucket policies and IAM instead. To see which policies your IAM users have, see AWS CLI: List All IAM Users.

Create a Bucket in a Chosen Region

PowerShell
# Set your default region (or add --region eu-west-1 to each command)
aws configure set region eu-west-1

# Create the bucket. Change the name: bucket names are global, so pick one nobody else has
aws s3 mb s3://pw-blog-bucket-101

# Confirm which region it is in
aws s3api get-bucket-location --bucket pw-blog-bucket-101 --output text
aws s3 mb creating a bucket in the default region and get-bucket-location confirming eu-west-1

aws s3 mb creates the bucket in your default region. The lower-level aws s3api create-bucket needs --create-bucket-configuration LocationConstraint=eu-west-1 for any region other than us-east-1, which is the usual reason it fails for people.

Add, List and Remove Files

PowerShell
# Copy a local file into a "folder" in the bucket. Keep the trailing slash
aws s3 cp ./Upload2S3.txt s3://pw-blog-bucket-101/folder1/

# List everything in the bucket
aws s3 ls s3://pw-blog-bucket-101 --recursive --human-readable

# Remove one file
aws s3 rm s3://pw-blog-bucket-101/folder1/Upload2S3.txt

# Delete the bucket and everything still in it (no undo)
aws s3 rb s3://pw-blog-bucket-101 --force
My 2022 run: aws s3 cp without the trailing slash, which saved the file as an object called folder1, then the bucket removed with --force

That screenshot is my 2022 run, and it shows the first mistake below: I left the slash off, and the listing shows the file stored as an object called folder1.

Two details catch people out. The trailing slash on the copy matters: without it, s3://bucket/folder1 saves your file as an object called folder1 instead of putting it inside that folder. And aws s3 rb refuses to delete a bucket that still has anything in it unless you add --force, which empties it first. On a bucket with versioning switched on, --force is not enough, because old versions stay behind; empty those in the console or with a lifecycle rule first. S3 folders are only name prefixes, so you do not need to create one before copying into it.

My screenshots are from 2022; the commands are the same today. The full list is in the AWS CLI s3 command reference.


Related

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *