前提:
一般情況下對OSS操作都會通過SDK,但是很多情況下對OSS進行簡單的上傳下載下傳的操作,那麼SDK就顯得有些臃腫,先要下載下傳sdk包,然後再寫些簡單的操作腳本,而通過shell腳本就會簡單很多。
而且很多場景:線上網站、資料庫等,生産出來的網站資料、資料庫資料、日志資料都需要來備份,如果備份再本地磁盤,那麼沒過多久磁盤空間就占用光了,如果是NAS來備份資料,價格又比較貴,如果把資料備份到OSS 低頻存儲就比較實惠一些。
使用方法:
上傳檔案到OSS:PUT
#!/bin/bash
host="oss-cn-shanghai.aliyuncs.com"
bucket="bucket名"
Id="AccessKey ID"
Key="Access Key Secret"
osshost=$bucket.$host
source="localfilename"
dest="objecetename"
resource="/${Bucket}/${dest}"
contentType=`file -ib ${source} |awk -F ";" '{print $1}'`
dateValue="`TZ=GMT env %a, %d %b %Y %H:%M:%S GMT'`"
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
signature=`echo -en $stringToSign | openssl sha1 -hmac ${Key} -binary | base64`
url=http://${OssHost}/${dest}
echo "upload ${source} to ${url}"
curl -i -q -X PUT -T "${source}" \
-H "Host: ${OssHost}" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: OSS ${Id}:${signature}" \
${url}
下載下傳OSS的檔案:GET
#!/bin/bash
host="oss-cn-shanghai.aliyuncs.com"
bucket="bucket名"
Id="AccessKey ID"
Key="Access Key Secret"
osshost=$bucket.$host
source="objecetename"
dest="localfilename"
resource="/${bucket}/${source}"
contentType=""
dateValue="`TZ=GMT env %a, %d %b %Y %H:%M:%S GMT'`"
stringToSign="GET\n\n${contentType}\n${dateValue}\n${resource}"
signature=`echo -en $stringToSign | openssl sha1 -hmac ${Key} -binary | base64`
url=http://${osshost}/${source}
echo "download ${url} to ${dest}"
curl --create-dirs \
-H "Host: ${osshost}" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: OSS ${Id}:${signature}" \
${url} -o ${dest}
原文位址:
http://blog.daobidao.com/shell-put-get-oss.html