Skip to content

Instantly share code, notes, and snippets.

@jfeilbach
Last active September 29, 2020 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfeilbach/e56d19886dc8fb9929d314a3cfe0c7ed to your computer and use it in GitHub Desktop.
Save jfeilbach/e56d19886dc8fb9929d314a3cfe0c7ed to your computer and use it in GitHub Desktop.
Copy a directory structure from STDIN to AWS S3 bucket using aws s3 cp command AWS CLI
$#/bin/bash
# --acl bucket-owner-full-control is on by default
SECONDS=0
src=$1 # the path of the source directory
dest=$2 # the s3 bucket destination path
echo -e "Copying from ${src} to ${dest}\n"
for entry in "$src"/*; do
# getting the name of the file or directory
name=`echo $entry | sed 's/.*\///'`
# if it is a directory
if [[ -d $entry ]]; then
aws s3 cp --acl bucket-owner-full-control --recursive "$name" "$dest/$name/"
# if it is a file
else
aws s3 cp --acl bucket-owner-full-control "$name" "$dest/"
fi
done
echo $SECONDS
exit 0
@jfeilbach
Copy link
Author

jfeilbach commented Sep 29, 2020

Example

/path/to/s3cp.sh </source path> < S3 bucket path>

~/s3cp.sh /tmp/files s3:///my-s3-bucket/folder/

or

~/s3cp.sh s3://mybucket/ s3://mybucket2/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment