@zphj1987
2016-11-11T13:59:20.000000Z
字数 880
阅读 1304
test
使用bash 实现对S3文件上传,包含metadata,acl、content-type设置,以及文件md5校验。
#!/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}"