Copy a Local Folder to an S3 Bucket

avatar

Borislav Hadzhiev

Tue Sep 21 20212 min read

banner

Photo by Kane Taylor

Copy a Local Folder to an S3 Bucket #

To copy the files from a local folder to an S3 bucket, run the s3 sync command, passing in the source directory and the destination bucket as inputs.

Let's look at an example, which copies the files from the current directory to an S3 bucket. Open your terminal in the directory which contains the files you want to copy and run the s3 sync command.

shell
aws s3 sync . s3://YOUR_BUCKET

copy local folder to bucket

The output shows that the files and folders contained in the local directory were successfully copied to the s3 bucket.

You can also pass the directory as an absolute path, for example:

shell
# on Linux or macOS aws s3 sync /home/john/Desktop/my-folder s3://YOUR_BUCKET # on Windows aws s3 sync C:\Users\USERNAME\my-folder s3://YOUR_BUCKET
To make sure the command does what you expect, you can run the command in test mode by adding the --dryrun parameter. This enables us to show the command's outputs, without actually running it.
shell
aws s3 sync . s3://YOUR_BUCKET --dryrun

run s3 cp in test mode

You might be wondering what would happen if the bucket contains a file with the same name and path as a file in the local folder.

The s3 sync command copies the objects from the local folder to the destination bucket, if:

  • the size of the objects differs
  • the last modified time of the source is newer than the last modified time of the destination
  • the s3 object does not exist under the specified prefix in the destination bucket

This means that if we had a document.pdf file in both the local directory and the destination bucket, it would only get copied if:

  • the size of the document differs
  • the last modified time of the document in the local directory is newer than the last modified time of the document in the destination bucket

To copy a local folder to a specific folder in an S3 bucket, run the s3 sync command, passing in the source directory and the full bucket path, including the directory name.

The following command copies the contents of the current folder to a my-folder directory in the S3 bucket.

shell
aws s3 sync . s3://YOUR_BUCKET/my-folder/

copy local folder to folder in bucket

The output shows that example.txt was copied to bucket/my-folder/example.txt.

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee