一佳一

记录像1+1一样简洁的代码
随笔 - 396, 文章 - 0, 评论 - 95, 阅读 - 107万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

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

Posted on   一佳一  阅读(761)  评论(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脚本是大小写敏感的,在此处同时体现了脚本语言的劣根性,写错了只能靠肉眼看,好处是可以随便写

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示