A cheat sheet for the AWS S3 CLI
The AWS command line interface is the goto tools from which you can perform pretty much any operation that relates to AWS. This post only cover the most common operations:
- list files / buckets
- create and sync files
- delete / move files
Assumption: the AWS CLI is installed in your system.
List Files and buckets with ‘aws s3 ls’
list S3 buckets:
~/$ aws s3 ls
2021-07-25 23:15:52 mybucket
2022-05-11 16:45:11 filestash-config
~/$ # or using the s3api directly:
~/$ aws s3api list-buckets --output text
BUCKETS 2021-07-25T13:15:52+00:00 mybucket
BUCKETS 2022-05-11T06:45:11+00:00 filestash-config
OWNER mickael 8b7a5480bc570db5e2898b4ebaea25fad30839b8a8c165ecfaef4edbf92eba58
2021-07-25 23:15:52 mybucket
2022-05-11 16:45:11 filestash-config
~/$ # or using the s3api directly:
~/$ aws s3api list-buckets --output text
BUCKETS 2021-07-25T13:15:52+00:00 mybucket
BUCKETS 2022-05-11T06:45:11+00:00 filestash-config
OWNER mickael 8b7a5480bc570db5e2898b4ebaea25fad30839b8a8c165ecfaef4edbf92eba58
list files / objects in a bucket:
~/$ aws s3 ls mybucket/
PRE test/
2020-05-17 02:20:18 557 releasenote.org
~/$ aws s3api list-objects --bucket mybucket --output text
None
CONTENTS "1068cea62cf71ec63b5824cc85508bb8" releasenote.org 2020-05-17 02:20:18 557 STANDARD
OWNER mickael 8b7a5480bc570db5e2898b4ebaea25fad30839b8a8c165ecfaef4edbf92eba58
CONTENTS "244abb13c672c19841b2e74d80283e3a" test/notes.org 2020-04-14T09:25:58+00:00 11850 STANDARD
OWNER mickael 8b7a5480bc570db5e2898b4ebaea25fad30839b8a8c165ecfaef4edbf92eba58
PRE test/
2020-05-17 02:20:18 557 releasenote.org
~/$ aws s3api list-objects --bucket mybucket --output text
None
CONTENTS "1068cea62cf71ec63b5824cc85508bb8" releasenote.org 2020-05-17 02:20:18 557 STANDARD
OWNER mickael 8b7a5480bc570db5e2898b4ebaea25fad30839b8a8c165ecfaef4edbf92eba58
CONTENTS "244abb13c672c19841b2e74d80283e3a" test/notes.org 2020-04-14T09:25:58+00:00 11850 STANDARD
OWNER mickael 8b7a5480bc570db5e2898b4ebaea25fad30839b8a8c165ecfaef4edbf92eba58
list files / objects under a key:
~/$ aws s3 ls mybucket/test/
2020-04-14 19:25:58 11850 notes.org
~/$ aws s3api list-objects --bucket mybucket --prefix test/ --output text
None
CONTENTS "244abb13c672c19841b2e74d80283e3a" test/notes.org 2020-04-14T09:25:58+00:00 11850 STANDARD
OWNER mickael 8b7a5480bc570db5e2898b4ebaea25fad30839b8a8c165ecfaef4edbf92eba58
~/$ aws s3api list-objects-v2 --bucket mybucket --prefix test/ --output text
None
CONTENTS "244abb13c672c19841b2e74d80283e3a" test/notes.org 2020-04-14T09:25:58+00:00 11850 STANDARD
2020-04-14 19:25:58 11850 notes.org
~/$ aws s3api list-objects --bucket mybucket --prefix test/ --output text
None
CONTENTS "244abb13c672c19841b2e74d80283e3a" test/notes.org 2020-04-14T09:25:58+00:00 11850 STANDARD
OWNER mickael 8b7a5480bc570db5e2898b4ebaea25fad30839b8a8c165ecfaef4edbf92eba58
~/$ aws s3api list-objects-v2 --bucket mybucket --prefix test/ --output text
None
CONTENTS "244abb13c672c19841b2e74d80283e3a" test/notes.org 2020-04-14T09:25:58+00:00 11850 STANDARD
list files in a bucket in a recursive manner:
~/$ aws s3 ls --recursive mybucket/
2020-05-17 02:20:18 557 releasenote.org
2020-04-14 19:25:58 11850 test/notes.org
2020-05-17 02:20:18 557 releasenote.org
2020-04-14 19:25:58 11850 test/notes.org
Manage data on S3 via the CLI
create a bucket:
~/$ aws s3 mb s3://mybucket
make_bucket: mybucket
make_bucket: mybucket
create a folder:
~/$ aws s3api put-object --bucket mybucket --key foldername/
{
"ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
"ServerSideEncryption": "AES256"
}
{
"ETag": "\"d41d8cd98f00b204e9800998ecf8427e\"",
"ServerSideEncryption": "AES256"
}
upload a folder:
~/$ aws s3 sync local s3://mybucket/local/
upload: local/bar.txt to s3://mybucket/local/bar.txt
upload: local/foo.txt to s3://mybucket/local/foo.txt
~/$ echo "lorem ipsum" > local/foo.txt
~/$ aws s3 sync local s3://mybucket/local/
upload: local/foo.txt to s3://mybucket/local/foo.txt
upload: local/bar.txt to s3://mybucket/local/bar.txt
upload: local/foo.txt to s3://mybucket/local/foo.txt
~/$ echo "lorem ipsum" > local/foo.txt
~/$ aws s3 sync local s3://mybucket/local/
upload: local/foo.txt to s3://mybucket/local/foo.txt
upload a file:
~/$ aws s3 cp local/foo.txt s3://mybucket/local/foobar.txt
upload: local/foo.txt to s3://mybucket/local/foobar.txt ~/$ # or using the s3api directly:
~/$ aws s3api put-object --bucket mybucket --key local/foo.txt --body foobar.txt
TODO
upload: local/foo.txt to s3://mybucket/local/foobar.txt ~/$ # or using the s3api directly:
~/$ aws s3api put-object --bucket mybucket --key local/foo.txt --body foobar.txt
TODO
download a file:
~/$ aws s3 cp s3://mybucket/local/foo.txt -
lorem ipsum ~/$ aws s3 cp s3://mybucket/local/foo.txt -
lorem ipsum aws s3api get-object --bucket filestash-config --key clovertex.json /dev/stdout
lorem ipsum ~/$ aws s3 cp s3://mybucket/local/foo.txt -
lorem ipsum aws s3api get-object --bucket filestash-config --key clovertex.json /dev/stdout
download a folder:
~/$ aws s3 sync s3://mybucket/local/ local
download: s3://mybucket/local/foo/foo.txt to local/foo/foo.txt
download: s3://mybucket/local/foo/foo.txt to local/foo/foo.txt
rename a file:
~/$ aws s3 mv s3://mybucket/local/foobar.txt s3://mybucket/local/foo/foo.txt
move: s3://mybucket/local/foobar.txt to s3://mybucket/local/foo/foo.txt
move: s3://mybucket/local/foobar.txt to s3://mybucket/local/foo/foo.txt
remove a file:
~/$ aws s3 rm s3://mybucket/local/foo.txt
delete: s3://mybucket/foo.txt
delete: s3://mybucket/foo.txt
remove a folder:
~/$ aws s3 rm --recursive s3://mybucket/local/
delete: s3://mybucket/local/bar.txt
delete: s3://mybucket/local/foo/foo.txt
delete: s3://mybucket/local/bar.txt
delete: s3://mybucket/local/foo/foo.txt
remove an entire bucket with its content:
~/$ aws s3 rb --force s3://mybucket
remove_bucket: mybucket
remove_bucket: mybucket
Manager data on S3 via our web client
The S3 CLI is great but sometime you might need something usable by people who would get scared of the terminal, in those scenario, the Filestash S3 browser is very handy: