bash实现S3文件上传

转自秦牧羊https://my.oschina.net/diluga/blog/785749


#!/bin/bash
ACCESS_KEY=""
SECRET_KEY=""
HOST="s3.demo.com"
BUCKET="demo"
CONTENT_TYPE="application/octet-stream"
FILENAME=/tmp/demo
ACL="x-amz-acl:public-read"
META_DATA="x-amz-meta-ukey:value"
FILESIZE=$(stat -c%s "$FILENAME")
FILEMD5=`cat ${FILENAME}| openssl dgst -md5 -binary | openssl enc -base64`
AUTH_PATH="/${BUCKET}${FILENAME}"
CURRENT_TIME=`TZ=GMT LANG=en_US date "+%a, %d %b %Y %H:%M:%S GMT"`
stringToSign="PUT\n${FILEMD5}\n${CONTENT_TYPE}\n${CURRENT_TIME}\n${ACL}\n${META_DATA}\n${AUTH_PATH}"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${SECRET_KEY} -binary | base64`

curl -s -v -X PUT "http://${HOST}${AUTH_PATH}" \
-H "Authorization: AWS ${ACCESS_KEY}:${signature}" \
-H "Date: ${CURRENT_TIME}" \
-H "Host: ${HOST}" \
-H "Content-Length: ${FILESIZE}" \
-H "Content-MD5: ${FILEMD5} " \
-H "Content-Type: ${CONTENT_TYPE} " \
-H "${ACL} " \
-H "${META_DATA} " \
-T "${FILENAME}"

你可能感兴趣的:(分布式存储)