Linux环境Shell脚本上传下载阿里云OSS文件

Linux环境Shell脚本上传下载阿里云OSS文件

背景

工作中由于我们项目生成的日志文件比较重要,而本地磁盘空间有限存储不了多久,因此考虑备份方案,我们原本打算保存在nas上,然而由于各种原因与运维沟通下来建议保存到oss上面。

由于linux原生支持shell,而网上大多数方案基于python-sdk,因此我们为了减少依赖,考虑直接使用shell脚本上传OSS,网上找了些资料,参见:

然而脚本试用下来有坑,特地记录一下:

  1. 字符比较提示异常

字符比较

上面截图字符比较会提示:

1./oss.sh: 13: ./oss.sh: [get: not found 2./oss.sh: 16: ./oss.sh: [put: not found 3./oss.sh: 32: ./oss.sh: [put: not found

应该改成上面的格式

2.拼接url的时候把bucket也带进去了。 3.拼接签名不对,研究了很久发现不应该用“#!/bin/sh”,而需要使用“#!/bin/bash”,这是个大坑。。。

修改版本

下面给出修改版本,需要自取:

1#!/bin/bash 2 3host="oss-cn-shanghai.aliyuncs.com" 4bucket="bucket名" 5Id="AccessKey ID" 6Key="Access Key Secret" 7# 参数1PUT:上传,GET:下载 8method=$1 9# 参数2,上传时为本地源文件路径,下载时为oss源文件路径 10source=$2 11# 参数3,上传时为OSS目标文件路径,下载时为本地目标文件路径 12dest=$3 13 14osshost=$bucket.$host 15 16# 校验method 17if test -z "$method" 18then 19 method=GET 20fi 21 22if [ "${method}"x = "get"x ] || [ "${method}"x = "GET"x ] 23then 24 method=GET 25elif [ "${method}"x = "put"x ] || [ "${method}"x = "PUT"x ] 26then 27 method=PUT 28else 29 method=GET 30fi 31 32#校验上传目标路径 33if test -z "$dest" 34then 35 dest=$source 36fi 37 38echo "method:"$method 39echo "source:"$source 40echo "dest:"$dest 41 42#校验参数是否为空 43if test -z "$method" || test -z "$source" || test -z "$dest" 44then 45 echo $0 put localfile objectname 46 echo $0 get objectname localfile 47 exit -1 48fi 49 50if [ "${method}"x = "PUT"x ] 51then 52 resource="/${bucket}/${dest}" 53 contentType=`file -ib ${source} |awk -F ";" '{print $1}'` 54 dateValue="`TZ=GMT date +'%a, %d %b %Y %H:%M:%S GMT'`" 55 stringToSign="${method}\n\n${contentType}\n${dateValue}\n${resource}" 56 signature=`echo -en $stringToSign | openssl sha1 -hmac ${Key} -binary | base64` 57 echo $stringToSign 58 echo $signature 59 url=http://${osshost}/${dest} 60 echo "upload ${source} to ${url}" 61 curl -i -q -X PUT -T "${source}" \ 62 -H "Host: ${osshost}" \ 63 -H "Date: ${dateValue}" \ 64 -H "Content-Type: ${contentType}" \ 65 -H "Authorization: OSS ${Id}:${signature}" \ 66 ${url} 67else 68 resource="/${bucket}/${source}" 69 contentType="" 70 dateValue="`TZ=GMT date +'%a, %d %b %Y %H:%M:%S GMT'`" 71 stringToSign="${method}\n\n${contentType}\n${dateValue}\n${resource}" 72 signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${Key} -binary | base64` 73 url=http://${osshost}/${source} 74 echo "download ${url} to ${dest}" 75 curl --create-dirs \ 76 -H "Host: ${osshost}" \ 77 -H "Date: ${dateValue}" \ 78 -H "Content-Type: ${contentType}" \ 79 -H "Authorization: OSS ${Id}:${signature}" \ 80 ${url} -o ${dest} 81fi 82

执行命令:

1#上传 2$ ./oss.sh put a.gz c.gz 3 4#下载 5$ ./oss.sh get c.gz d.gz

2018-11-21更新:

今天看到阿里云提供ossutil64,详见:https://help.aliyun.com/document_detail/50452.html 有了这个方便很多。

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Linux环境Shell脚本上传下载阿里云OSS文件 - HelloWorld