linux本地远程上传&下载阿里云oss的shell脚本实现方法

当项目早服务器中运行时会产生大量的日志,如果日志不处理全部放在本服务器中显然没有那么大的内存,除了要做必要的删除也要做日志必要备份.

创建oss.sh脚本:

复制代码
#!/bin/bash
host="oss-cn-beijing.aliyuncs.com"
bucket="****"
###bucket名字###
Id="****************"
###Access ID###
Key="******************"
###Access Key###
# 参数1,PUT:上传,GET:下载
method=$1
# 参数2,上传时为本地源文件路径,下载时为oss源文件路径
source=$2
# 参数3,上传时为OSS目标文件路径,下载时为本地目标文件路径
dest=$3
 
osshost=$bucket.$host
 
# 校验method
if test -z "$method"
then
    method=GET
fi
 
if [ "${method}"x = "get"x ] || [ "${method}"x = "GET"x ]
then
    method=GET
elif [ "${method}"x = "put"x ] || [ "${method}"x = "PUT"x ]
then
    method=PUT
else
    method=GET
fi
 
#校验上传目标路径
if test -z "$dest"
then
    dest=$source
fi
 
echo "method:"$method
echo "source:"$source
echo "dest:"$dest
 
#校验参数是否为空
if test -z "$method" || test -z "$source" || test -z "$dest"
then
    echo $0 put localfile objectname
    echo $0 get objectname localfile
    exit -1
fi
 
if [ "${method}"x = "PUT"x ]
then
    resource="/${bucket}/${dest}"
    contentType=`file -ib ${source} |awk -F ";" '{print $1}'`
    dateValue="`TZ=GMT 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`
    echo $stringToSign
    echo $signature
    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}
else
    resource="/${bucket}/${source}"
    contentType=""
    dateValue="`TZ=GMT 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}/${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}
fi
复制代码

接下来执行shell脚本

上传:sh /oss.sh put /要上传文件/ /到oss指定地址

eg:sh oss.sh put /home/log.zip logs/log.zip
下载:sh /oss.sh get /要下载的文件/ /到linux本地地址
eg:sh oss.sh get /logs/log.zip /home/log.zip

当执行完以上命令出现如下图就代表成功

 

posted @   情定今生~~~  阅读(2921)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示