linux本地远程上传&下载阿里云oss的shell脚本实现方法
Posted 宋宝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
当执行完以上命令出现如下图就代表成功
以上是关于linux本地远程上传&下载阿里云oss的shell脚本实现方法的主要内容,如果未能解决你的问题,请参考以下文章