Skip to content

Instantly share code, notes, and snippets.

@antonioribeiro
Created November 18, 2016 01:38
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 antonioribeiro/65909c8a2013377101e157e404d621d0 to your computer and use it in GitHub Desktop.
Save antonioribeiro/65909c8a2013377101e157e404d621d0 to your computer and use it in GitHub Desktop.
#!/bin/bash
FILENAME="$1"
S3_KEY="YOUR-KEY"
S3_SECRET="YOUR-SECRET"
BUCKET="YOUR-BUCKET"
FILEPATH="DESTINATION-DIRECTORY/$file"
CONTENT_TYPE="application/octet-stream"
DATE_TIME="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
MD5="$(openssl md5 -binary < "$FILENAME" | base64)"
SIG="$(printf "PUT\n$MD5\n$CONTENT_TYPE\n$DATE_TIME\n/$BUCKET/$FILEPATH" | openssl sha1 -binary -hmac "$S3_SECRET" | base64)"
curl -T $FILENAME http://$BUCKET.s3.amazonaws.com/$FILEPATH \
-H "date: $DATE_TIME" \
-H "Authorization: AWS $S3_KEY:$SIG" \
-H "Content-Type: $CONTENT_TYPE" \
-H "Content-MD5: $MD5"
@antonioribeiro
Copy link
Author

antonioribeiro commented Nov 18, 2016

Full version

#!/bin/bash

function upload() {
    FILENAME="$1"
    S3_KEY="YOUR-KEY"
    S3_SECRET="YOUR-SECRET"
    BUCKET="YOUR-BUCKET"
    FILEPATH="DESTINATION-DIRECTORY/$file"        
    CONTENT_TYPE="application/octet-stream"
    DATE_TIME="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
    MD5=`cat $FILENAME | openssl md5 -binary | base64`
    SIG="$(printf "PUT\n$MD5\n$CONTENT_TYPE\n$DATE_TIME\n/$BUCKET/$FILEPATH" | openssl sha1 -binary -hmac "$S3_SECRET" | base64)"
    curl -T $FILENAME http://$BUCKET.s3.amazonaws.com/$FILEPATH \
        -H "date: $DATE_TIME" \
        -H "Authorization: AWS $S3_KEY:$SIG" \
        -H "Content-Type: $CONTENT_TYPE" \
        -H "Content-MD5: $MD5"
}

for dir in "$@"; do
    if [ ! -d "$dir" ]; then
        echo Uploading $dir...
        upload "$dir"
    fi
done

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