Skip to content

Instantly share code, notes, and snippets.

@rainchen
Last active September 6, 2017 15:38
Show Gist options
  • Save rainchen/a2e4b9bbaa87c703d3d0af2e82e3701c to your computer and use it in GitHub Desktop.
Save rainchen/a2e4b9bbaa87c703d3d0af2e82e3701c to your computer and use it in GitHub Desktop.
#!/bin/bash
# refs:
# https://segmentfault.com/q/1010000003000974
# http://blog.csdn.net/guoer9973/article/details/46459971
# http://www.cnblogs.com/sink_cup/p/cloud_storage_aliyun_oss_vs_qiniu_rs.html
# api:
# https://developer.qiniu.com/kodo/api/1312/upload
# how-to-access-or-locate-the-access-key-and-secret-key
accesskey=$(printenv 'QINIU_AK')
secretkey=$(printenv 'QINIU_SK')
namespace=$(printenv 'QINIU_NS')
if [[ $accesskey == '' ]]; then
echo 'QINIU_AK is blank'
exit;
fi
uploadfile=$1
echo "Upload $uploadfile to $namespace"
echo
scope="$namespace"
deadline=$(expr `date +%s` + 3600)
# build policy json
putPolicy="{\"scope\":\"$scope\",\"deadline\":$deadline}"
echo "putPolicy:$putPolicy"
data=$(echo -en $putPolicy | base64 | tr "+/" "-_")
echo "data:$data"
auth=$(echo -en $data | openssl sha1 -hmac $secretkey -binary | base64 | tr "+/" "-_")
echo "auth:$auth"
uploadToken="$accesskey:$auth:$data";
echo "uploadToken:$uploadToken"
echo "Uploading..."
echo
# add "-v" for debug
curl -v -F token="$uploadToken" -F file="@$uploadfile" -F key="$uploadfile" http://upload.qiniu.com/
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment