AWS S3 Upload/Download Shell Script

Simple S3 upload/download shell script.


#!/bin/bash

dateValue=$(date -R)
bucket=$AWS_BUCKET_NAME
s3Key=$AWS_ACCESS_KEY_ID
s3Secret=$AWS_SECRET_ACCESS_KEY

contentType=$(file -b --mime-type "$2")
echo contentType: "$contentType"

download () {
  outputFile="./$1"
  amzFile="$1"

  resource="/${bucket}/${amzFile}"

  stringToSign="GET\n\n${contentType}\n${dateValue}\n${resource}"
  signature=$(echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64)

  curl  -H "Host: ${bucket}.s3.amazonaws.com" \
     -H "Date: ${dateValue}" \
     -H "Content-Type: ${contentType}" \
     -H "Authorization: AWS ${s3Key}:${signature}" \
     https://${bucket}.s3.amazonaws.com/${amzFile} -o $outputFile
}

upload () {
  fname=$(basename "$1")
  amzFile="$fname"
  resource="/${bucket}/${amzFile}"
  stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
  signature=$(echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64)
  curl -X PUT -T "$1" \
     -H "Host: ${bucket}.s3.amazonaws.com" \
     -H "Date: ${dateValue}" \
     -H "Content-Type: ${contentType}" \
     -H "Authorization: AWS ${s3Key}:${signature}" \
      https://${bucket}.s3.amazonaws.com/"${amzFile}"
}

if [ "$1" = "upload" ]; then
  echo "Uploading $2"
  upload "$2"
elif [ "$1" = "download" ]; then
  echo "Downloading $2"
  download "$2"
fi

This script requires 3 environment variables:

  • $AWS_BUCKET_NAME
  • $AWS_ACCESS_KEY_ID
  • $AWS_SECRET_ACCESS_KEY

Name the file s3.sh, chmod +x it and upload/download via the two following commands:


./s3.sh upload file.json
./s3.sh download file.json

This script is a modified version of several different scripts that I've made for my general needs. I, unfortunately, cannot remember the original sources.