Skip to content

Instantly share code, notes, and snippets.

@cesarmiquel
Last active April 2, 2020 21:13
Show Gist options
  • Save cesarmiquel/3a83e500dbad235e396d7f8b13e51882 to your computer and use it in GitHub Desktop.
Save cesarmiquel/3a83e500dbad235e396d7f8b13e51882 to your computer and use it in GitHub Desktop.
Upload a file to an Amazon S3 bucket with this bash script (requires OpenSSL and Curl)
#!/bin/bash
#
# Usage: copy-to-s3.sh /full/path/to/the/file.txt /path/in/amazon/fle.txt
#
# Caveat: the path to the file in AWS needs to be URL Encoded (in case there are spaces, etc)
#
# Minimum tweak from this Gist: https://gist.github.com/chrismdp/6c6b6c825b07f680e710
#
# 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
S3KEY="[AWS KEY HERE]"
S3SECRET="[AWS SECRET HERE]"
function putS3
{
path="${1}"
aws_path="${2}"
bucket='[YOUR BUCKET HERE]'
date=$(date +"%a, %d %b %Y %T %z")
acl="x-amz-acl:public-read"
content_type='application/x-compressed-tar'
string="PUT\n\n$content_type\n$date\n$acl\n/$bucket$aws_path"
signature=$(echo -en "${string}" | openssl sha1 -hmac "${S3SECRET}" -binary | base64)
curl -X PUT -T "$path" \
-H "Host: $bucket.s3.amazonaws.com" \
-H "Date: $date" \
-H "Content-Type: $content_type" \
-H "$acl" \
-H "Authorization: AWS ${S3KEY}:$signature" \
"https://$bucket.s3.amazonaws.com$aws_path"
}
echo "Uploading [$2]..."
putS3 "$1" "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment