一佳一

记录像1+1一样简洁的代码

导航

使用shell脚本上传文件至阿里云OSS(无需装任何sdk)

Posted on 2022-10-10 13:51  一佳一  阅读(556)  评论(0编辑  收藏  举报

 

 

#!/bin/bash


host="oss-cn-hangzhou.aliyuncs.com"
bucket="bucket" # BucketName
id="id" # AccessKeyId
key="key" # AccessKeySecret
osshost=$bucket.$host
echo $osshost


source="/mnt/dumpfile/test.txt"
dest="dumpfiles/test.txt"


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="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}
 

 

自行替换里面的如下配置:

  • bucket="bucket" # BucketName
  • id="id" # AccessKeyId
  • key="key" # AccessKeySecret
  • source="/mnt/dumpfile/test.txt"
  • dest="dumpfiles/test.txt"

 

该脚本依赖了如下几个程序(如果缺失,需要安装一下,以当前环境的debian为例):

  • echo
  • file
  • awk
  • openssl
  • curl

 

sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
apt update

apt install file vim -y

vim  upload.sh

# 给对应脚本修改权限
chmod 755 upload.sh

./upload.sh

 

参考资料:

https://developer.aliyun.com/article/698777
http://mirrors.ustc.edu.cn/help/debian.html

注意:shell脚本是大小写敏感的,在此处同时体现了脚本语言的劣根性,写错了只能靠肉眼看,好处是可以随便写