#!/bin/bash
readonly host="oss-cn-beijing.aliyuncs.com"
readonly bucket="xxx"
readonly Id="xxx"
readonly Key="xxx"
readonly source=$1
readonly dest=$2
readonly ossHost=${bucket}.${host}
readonly date=$(date '+%Y-%m-%d')
readonly uploadLog=./oss_upload_${date}.log
readonly resultLog=./oss_upload_${date}_result.log
function info() {
time=$(date '+%Y-%m-%d %H:%M:%S')
echo "[INFO][${time}] $1" >> ${uploadLog}
}
function error() {
time=$(date '+%Y-%m-%d %H:%M:%S')
echo "[ERROR][${time}] $1" >> ${uploadLog}
info "================END================"
exit 1
}
function checkPath() {
# 校验上传目标路径
info "start check path"
if test -z "$source" || test -z "$dest"
then
error "no source or dest path"
fi
info "source: ${source}"
info "dest: ${dest}"
info "ossHost: ${ossHost}"
}
function checkResult() {
if [ ! -f ${resultLog} ];then
error "no found resultLog: ${resultLog}"
fi
count=$(cat ${resultLog}|grep '200 OK'|wc -l)
if [ ${count} -ge 1 ];then
costTime=$(cat ${resultLog}|grep 'x-oss-server-time'|awk '{print $NF}')
info "UPLOAD SUCCESS, Cost Time: ${costTime}"
else
error "UPLOAD ERROR"
fi
}
function upload() {
method=PUT
resource="/${bucket}/${dest}"
contentType=`file -ib ${source} |awk -F ";" '{print $1}'`
dateValue="`TZ=GMT env LANG=en_US.UTF-8 date +'%a, %d %b %Y %H:%M:%S GMT'`"
stringToSign="${method}\n\n${contentType}\n${dateValue}\n${resource}"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${Key} -binary | base64`
url=http://${ossHost}/${dest}
info "stringToSign: ${stringToSign}"
info "signature: ${signature}"
info "upload ${source} to ${url}"
curl -i -q -X PUT -T "${source}" -o ${resultLog} \
-H "Host: ${ossHost}" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: OSS ${Id}:${signature}" \
${url}
checkResult
info "Finished."
}
info "==============BEGIN=================="
checkPath
upload
info "================END================"
exit 0
有些系统为中文,导致date验证不通过:
HTTP/1.1 100 Continue
HTTP/1.1 403 Forbidden
Server: AliyunOSS
Date: Mon, 20 Apr 2020 07:23:35 GMT
Content-Type: application/xml
Content-Length: 269
Connection: keep-alive
x-oss-request-id: 5E9D4DF7B12E8436376A9762
x-oss-server-time: 15
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>AccessDenied</Code>
<Message>OSS authentication requires a valid Date.</Message>
<RequestId>5E9D4DF7B12E8436376A9762</RequestId>
<HostId>ainemo-k8stxdev.oss-cn-beijing-internal.aliyuncs.com</HostId>
</Error>
故替换一下
LANG=en_US.UTF-8 date +'%a, %d %b %Y %H:%M:%S GMT'
替换
date +'%a, %d %b %Y %H:%M:%S GMT'