Sha256: a6ed5b99112931ac92e713aa1ba3584c8a9d17149fb16d572c59a1f1d967b7c2

Contents?: true

Size: 1.31 KB

Versions: 18

Compression:

Stored size: 1.31 KB

Contents

#!/usr/bin/env bash
# Usage: s3-put <FILE> <S3_BUCKET>[:<PATH>] [<CONTENT_TYPE>]
#
# Uploads a file to the Amazon S3 service.
# Outputs the URL for the newly uploaded file.
#
# Requirements:
# - AMAZON_ACCESS_KEY_ID
# - AMAZON_SECRET_ACCESS_KEY
# - openssl
# - curl
#
# Author: Mislav Marohnić

set -e

authorization() {
  local signature="$(string_to_sign | hmac_sha1 | base64)"
  echo "AWS ${AMAZON_ACCESS_KEY_ID?}:${signature}"
}

hmac_sha1() {
  openssl dgst -binary -sha1 -hmac "${AMAZON_SECRET_ACCESS_KEY?}"
}

base64() {
  openssl enc -base64
}

bin_md5() {
  openssl dgst -binary -md5
}

string_to_sign() {
  echo "$http_method"
  echo "$content_md5"
  echo "$content_type"
  echo "$date"
  echo "x-amz-acl:$acl"
  printf "/$bucket/$remote_path"
}

date_string() {
  LC_TIME=C date "+%a, %d %h %Y %T %z"
}

file="$1"
bucket="${2%%:*}"
remote_path="${2#*:}"
content_type="$3"

if [ -z "$remote_path" ] || [ "$remote_path" = "$bucket" ]; then
  remote_path="${file##*/}"
fi

http_method=PUT
acl="public-read"
content_md5="$(bin_md5 < "$file" | base64)"
date="$(date_string)"

url="https://$bucket.s3.amazonaws.com/$remote_path"

curl -qsSf -T "$file" \
  -H "Authorization: $(authorization)" \
  -H "x-amz-acl: $acl" \
  -H "Date: $date" \
  -H "Content-MD5: $content_md5" \
  -H "Content-Type: $content_type" \
  "$url"

echo "$url"

Version data entries

18 entries across 18 versions & 6 rubygems

Version Path
devise-3.5.10 script/s3-put
devise-3.5.9 script/s3-put
devise-3.5.8 script/s3-put
devise-3.5.7 script/s3-put
devise-3.5.6 script/s3-put
devise-3.5.5 script/s3-put
devise-3.5.4 script/s3-put
devise-3.5.3 script/s3-put
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/devise-3.5.2/script/s3-put
devise-3.5.2 script/s3-put
devise-3.5.1 script/s3-put
deviseOne-1.0.0 script/s3-put
faraday-0.9.1 script/s3-put
devise-3.4.1 script/s3-put
devise-3.4.0 script/s3-put
brainsome_devise-3.3.0 script/s3-put
devise-3.3.0 script/s3-put
hub-1.11.0 script/s3-put