Skip to content

Instantly share code, notes, and snippets.

@mulatinho
Last active October 26, 2016 12:59
Show Gist options
  • Save mulatinho/cf148b96248ad5d1d49e8fd84c982454 to your computer and use it in GitHub Desktop.
Save mulatinho/cf148b96248ad5d1d49e8fd84c982454 to your computer and use it in GitHub Desktop.
upload files to S3 using shell script
#!/bin/bash
# amazon s3 upload file code
# written by Alexandre Mulatinho < alex @ mulatinho . net >
# Wed Oct 26 09:30:54 BRT 2016
# use -> ./s3upload.sh hostAmazon yourBucketName yourFilename
[ ! "$3" ] && echo "use: $0 <host> <bucket> <file>" && exit 1
dateNow="`TZ=Africa/Bumako date "+%a, %0e %b %Y %T %z"`"
host="$1"
fileName="$3"
urlFileName="/$2/$fileName"
echo list files:
ls -l $fileName
fileMD5=`openssl dgst -md5 -binary "$fileName" | base64`
stringToSign="PUT\n$fileMD5\napplication/octet-stream\n$dateNow\nx-amz-acl:public-read\n$urlFileName"
trysignature=`echo -en $stringToSign | openssl sha1 -hmac ${AWS_SECRET_ACCESS_KEY} -binary`
signature=`echo -en $stringToSign | openssl sha1 -hmac ${AWS_SECRET_ACCESS_KEY} -binary | base64`
authorization="AWS ${AWS_ACCESS_KEY_ID}:${signature}"
echo sending ${fileName}...
curl -v -L -X PUT -T "$fileName" \
-H "Host: ${host}" \
-H "Date: ${dateNow}" \
-H "x-amz-acl: public-read" \
-H "Content-Type: application/octet-stream" \
-H "Content-MD5: ${fileMD5}" \
-H "Authorization: $authorization" \
https://${host}${urlFileName}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment