2020-05-10 09:59:34 +02:00
|
|
|
#!/bin/bash
|
2020-05-10 09:57:46 +02:00
|
|
|
|
|
|
|
echo "** backblaze b2 custom uploader **"
|
|
|
|
|
2020-05-10 19:58:59 +02:00
|
|
|
FILE_TO_UPLOAD=$1
|
|
|
|
MIME_TYPE=$2
|
|
|
|
B2_KEY_ID=$3
|
|
|
|
B2_KEY=$4
|
2020-05-10 10:36:39 +02:00
|
|
|
SHA1_OF_FILE=$(openssl dgst -sha1 $FILE_TO_UPLOAD | awk '{print $2;}')
|
2020-05-10 09:57:46 +02:00
|
|
|
|
2020-05-10 10:39:33 +02:00
|
|
|
b2_authorize_account=$(curl -s 'https://api.backblazeb2.com/b2api/v2/b2_authorize_account' -u "$B2_KEY_ID:$B2_KEY")
|
2020-05-10 19:36:31 +02:00
|
|
|
|
2020-05-10 11:24:42 +02:00
|
|
|
apiUrl=$(echo $b2_authorize_account | jq -r '.apiUrl')
|
|
|
|
B2_TOKEN=$(echo $b2_authorize_account | jq -r '.authorizationToken')
|
2020-05-10 22:32:25 +02:00
|
|
|
B2_BUCKET_ID=$(echo $b2_authorize_account | jq -r '.allowed.bucketId')
|
2020-05-10 09:57:46 +02:00
|
|
|
|
2020-05-10 20:11:23 +02:00
|
|
|
if [ -z "$b2_authorize_account" ]; then echo b2_authorize_account is empty; exit 1; fi
|
|
|
|
if [ -z "$apiUrl" ]; then echo apiUrl is empty; exit 1; fi
|
|
|
|
if [ -z "$B2_TOKEN" ]; then echo B2_TOKEN is empty; exit 1; fi
|
|
|
|
if [ -z "$B2_BUCKET_ID" ]; then echo B2_BUCKET_ID is empty; exit 1; fi
|
2020-05-10 19:58:59 +02:00
|
|
|
|
2020-05-10 19:36:31 +02:00
|
|
|
UPLOAD_URL=$(curl -s -H "Authorization:$B2_TOKEN" -d '{"bucketId":"$B2_BUCKET_ID"}' $apiUrl/b2api/v2/b2_get_upload_url | jq -r '.uploadUrl')
|
2020-05-10 09:57:46 +02:00
|
|
|
|
|
|
|
curl -s -H "Authorization:$B2_TOKEN" -H "X-Bz-File-Name:$FILE_TO_UPLOAD" -H "Content-Type:$MIME_TYPE" -H "X-Bz-Content-Sha1:$SHA1_OF_FILE" -H "X-Bz-Info-Author:${DRONE_COMMIT_AUTHOR}" --data-binary "@$FILE_TO_UPLOAD" $UPLOAD_URL
|
|
|
|
|
|
|
|
echo "** done **"
|