oss.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/bash
  2. host="oss-cn-shanghai.aliyuncs.com"
  3. inner_host="oss-cn-shanghai-internal.aliyuncs.com"
  4. bucket="third-part-tool"
  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