• 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}
    
    ${contentType}
    ${dateValue}
    ${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}
    
    ${contentType}
    ${dateValue}
    ${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

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

  • 相关阅读:
    Adobe Reader XI 自动闪退问题
    NoSQL非关系型数据库Redis (键值对(key-value)数据库) 学习笔记
    MarkdownPad2报错: Html Rendering Error (An error occurred with the Html rendering component.)
    Thymeleaf学习笔记
    Elasticsearch学习笔记2--Spring Data
    Redis5.0学习笔记
    Xshell6 评估期已过——解决办法
    Windows版抓包工具Wireshark3.0
    PHP 判断数据是否为空 ‘0’判断为空可选
    python3 多线程,多进程 ,IO多路复用
  • 原文地址:https://www.cnblogs.com/songbao/p/12627876.html
Copyright © 2020-2023  润新知