oss.sh 2.2 KB

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