Skip to content

Instantly share code, notes, and snippets.

@hr3lxphr6j
Last active November 30, 2020 06:53
Show Gist options
  • Save hr3lxphr6j/be697492de9b500b6a5e59bb420c2752 to your computer and use it in GitHub Desktop.
Save hr3lxphr6j/be697492de9b500b6a5e59bb420c2752 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
_date() {
LANG=us_EN.UTF-8 TZ=GMT date '+%a, %d %b %Y %H:%M:%S %Z'
}
gen_signature() {
method=$1
content_type=$2
bucket=$3
file=$4
access_key_secret=$5
content="$method\n\n$content_type\n$(_date)\n/$bucket/${file}"
echo -en $content | openssl dgst -sha1 -hmac $access_key_secret -binary | base64
}
download_file() {
access_key_id=$1
access_key_secret=$2
endpoint=$3
bucket=$4
bucket_endpoint=https://$bucket.$endpoint
file=$5
output=$6
signature=$(gen_signature "GET" "" "$bucket" "$file" "$access_key_secret")
curl -fsSLo $output \
-H "Date: $(_date)" \
-H "Authorization: OSS ${access_key_id}:${signature}" \
$bucket_endpoint/$file
}
upload_file() {
access_key_id=$1
access_key_secret=$2
endpoint=$3
bucket=$4
bucket_endpoint=https://$bucket.$endpoint
file=$5
content_type=$6
target_name=$7
signature=$(gen_signature "PUT" "$content_type" "$bucket" "$target_name" "$access_key_secret")
curl -fsSXPUT -T "$file" \
-H "Date: $(_date)" \
-H "Content-type: $content_type" \
-H "Authorization: OSS ${access_key_id}:${signature}" \
$bucket_endpoint/$target_name
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment