Skip to content

Instantly share code, notes, and snippets.

@xav-b
Created December 23, 2015 09:24
Show Gist options
  • Save xav-b/6e0d1c68fc55da5dbb5a to your computer and use it in GitHub Desktop.
Save xav-b/6e0d1c68fc55da5dbb5a to your computer and use it in GitHub Desktop.
Upload files to S3 with a few lines of Bash
# You don't need Fog in Ruby or some other library to upload to S3 -- shell works perfectly fine
# This is how I upload my new Sol Trader builds (http://soltrader.net)
# Based on a modified script from here: http://tmont.com/blargh/2014/1/uploading-to-s3-in-bash
# export S3KEY="my aws key"
# export S3SECRET="my aws secret" # pass these in
BUCKET="apps-crawler-prod"
putS3 () {
path=$1
file=$2
aws_path=$3
datetime=$(date +"%a, %d %b %Y %T %z")
echo "uploading $file to s3:$BUCKET$aws_path ($datetime)"
acl="x-amz-acl:public-read"
content_type='application/x-compressed-tar'
string="PUT\n\n$content_type\n$datetime\n$acl\n/${BUCKET}$aws_path$file"
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64)
curl -X PUT -T "$path/$file" \
-H "Host: ${BUCKET}.s3.amazonaws.com" \
-H "Date: $datetime" \
-H "Content-Type: $content_type" \
-H "$acl" \
-H "Authorization: AWS ${S3KEY}:$signature" \
"https://${BUCKET}.s3.amazonaws.com$aws_path$file"
}
# TODO multiple files
putS3 "." "$1" "/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment