Skip to content

Instantly share code, notes, and snippets.

@ty4z2008
Created December 4, 2019 09:44
Show Gist options
  • Save ty4z2008/ffcfb99e44ddd0349e5168878a4cc633 to your computer and use it in GitHub Desktop.
Save ty4z2008/ffcfb99e44ddd0349e5168878a4cc633 to your computer and use it in GitHub Desktop.
aliyun oss uploader with curl
#!/usr/bin/env bash
#
# Replace below arguments before use
# OSS_ACCESS_KEY_ID 、OSS_ACCESS_KEY_SECRET、BUCKET、ENDPOINT、DIRECTORY
#
# This example will upload file to hangzhou Aliyun object stroage system
# Usage: ./oss.sh test.sh
#
echo "upload file $1"
OSS_ACCESS_KEY_ID="your oss access key id"
OSS_ACCESS_KEY_SECRET="your oss key secret"
BUCKET="your bucket name"
ENDPOINT="oss-cn-hangzhou.aliyuncs.com"
DIRECTORY='upload directory prefix'
FILENAME=$(basename "$1")
RESOURCE="/${BUCKET}${DIRECTORY}/${FILENAME}"
CONTENT_MD5=$(openssl dgst -md5 -binary "$1" | openssl enc -base64)
CONTENT_TYPE=$(file -ib "$1" |awk -F ";" '{print $1}')
DATE_VALUE="`TZ=GMT date +'%a, %d %b %Y %H:%M:%S GMT'`"
STRING_TO_SIGN="PUT\n${CONTENT_MD5}\n${CONTENT_TYPE}\n${DATE_VALUE}\n${RESOURCE}"
SIGNATURE=$(echo -e -n $STRING_TO_SIGN | openssl dgst -sha1 -binary -hmac $OSS_ACCESS_KEY_SECRET | openssl enc -base64)
URL="http://${BUCKET}.${ENDPOINT}${DIRECTORY}/${FILENAME}"
curl -i -q -X PUT -T "$1" \
-H "Host: ${BUCKET}.${ENDPOINT}" \
-H "Date: ${DATE_VALUE}" \
-H "Content-Type: ${CONTENT_TYPE}" \
-H "Content-MD5: ${CONTENT_MD5}" \
-H "Authorization: OSS ${OSS_ACCESS_KEY_ID}:${SIGNATURE}" \
${URL}
echo "update file $1 success"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment