github.com/jingweno/gh@v2.1.1-0.20221007190738-04a7985fa9a1+incompatible/script/s3-put (about) 1 #!/usr/bin/env bash 2 # Usage: s3-put <FILE> <S3_BUCKET>[:<PATH>] [<CONTENT_TYPE>] 3 # 4 # Uploads a file to the Amazon S3 service. 5 # Outputs the URL for the newly uploaded file. 6 # 7 # Requirements: 8 # - AMAZON_ACCESS_KEY_ID 9 # - AMAZON_SECRET_ACCESS_KEY 10 # - openssl 11 # - curl 12 # 13 # Author: Mislav Marohnić 14 15 set -e 16 17 authorization() { 18 local signature="$(string_to_sign | hmac_sha1 | base64)" 19 echo "AWS ${AMAZON_ACCESS_KEY_ID?}:${signature}" 20 } 21 22 hmac_sha1() { 23 openssl dgst -binary -sha1 -hmac "${AMAZON_SECRET_ACCESS_KEY?}" 24 } 25 26 base64() { 27 openssl enc -base64 28 } 29 30 bin_md5() { 31 openssl dgst -binary -md5 32 } 33 34 string_to_sign() { 35 echo "$http_method" 36 echo "$content_md5" 37 echo "$content_type" 38 echo "$date" 39 echo "x-amz-acl:$acl" 40 printf "/$bucket/$remote_path" 41 } 42 43 date_string() { 44 LC_TIME=C date "+%a, %d %h %Y %T %z" 45 } 46 47 file="$1" 48 bucket="${2%%:*}" 49 remote_path="${2#*:}" 50 content_type="$3" 51 52 if [ -z "$remote_path" ] || [ "$remote_path" = "$bucket" ]; then 53 remote_path="${file##*/}" 54 fi 55 56 http_method=PUT 57 acl="public-read" 58 content_md5="$(bin_md5 < "$file" | base64)" 59 date="$(date_string)" 60 61 url="https://$bucket.s3.amazonaws.com/$remote_path" 62 63 curl -qsSf -T "$file" \ 64 -H "Authorization: $(authorization)" \ 65 -H "x-amz-acl: $acl" \ 66 -H "Date: $date" \ 67 -H "Content-MD5: $content_md5" \ 68 -H "Content-Type: $content_type" \ 69 "$url" 70 71 echo "$url"