Uploading Files to AWS S3 Using CLI

AWS

I’ll assume you have your AWS CLI configured so you are able to login to an AWS account. If you need any help on this see: https://geekmungus.co.uk/?p=1167. Here’s a bit of a cheat sheet on how to use AWS S3 via the CLI.

Listing Buckets

aws s3 ls

Upload One File

Upload the file called “myfile.txt” into the S3 bucket called “mybucket1”.

aws s3 cp c:\localcopy\myfile.txt s3://mybucket1/

List Contents of a Bucket

aws s3 ls s3://mybucket1/

Upload Multiple Files

Upload the contents of the “c:\localcopy” directory into the S3 bucket called “mybucket1”, the “recursive” argument is added which ensures that all the files and sub-directories (and the files within) are also uploaded.

aws s3 cp c:\localcopy s3://mybucket1/ --recursive

Upload Multiple Files (Include and Exclude)

In this example, uploading all files to a bucket that have the “.txt” file extension.

aws s3 cp c:\localcopy s3://mybucket1/ --recursive --exclude * --include *.txt

Download Files (and Multiple Files)

Downloading is just a reverse of the upload essentially, you can specify the “–recursive” option if you want to download all files (and sub-directories including their contents).

aws s3 cp s3://mybucket1/ c:\localcopy
aws s3 cp s3://mybucket1/ c:\localcopy --recursive

Copying Files (Objects) Between S3 Locations

To copy a file (object) between S3 locations, you can perform this as follows, so in this example copying a file called “myfile.txt” from S3 “mybucket1” to S3 “mybucket2”.

aws s3 cp s3://mybucket1/myfile.txt s3://mybucket2/

Sync Files (Objects)

Let’s say you want to sync changed files, you can use the “sync” command, this has a number of ways to use it but a simple upload sync would be:

aws s3 sync c:\localcopy s3://mybucket1/ --recursive

While a sync but which also deletes files that have been deleted from your source local directory:

aws s3 sync c:\localcopy s3://mybucket1/ --recursive --delete

Conclusion

A bit of a whistle stop tour, but this gives you an overview of some simple commands you can use, which can be helpful for scripting, or just managing files in your S3 buckets.

Leave a Reply

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