####
#### x.sh [FILE] [FOLDER_ID] [FILE_ID]
#### https://labbots.com/google-drive-upload-bash-script/
#### https://developers.google.com/oauthplayground/
####
CLIENT_SERCRET=`cat CLIENT_SERCRET`
REFRESH_TOKEN=`cat REFRESH_TOKEN`
CLIENT_ID=`cat CLIENT_ID`
REFRESH_RESPONSE=`curl --silent \
https://www.googleapis.com/oauth2/v4/token \
--data-raw "client_secret=$CLIENT_SERCRET&grant_type=refresh_token&refresh_token=$REFRESH_TOKEN&client_id=$CLIENT_ID"`
echo "refresh response: $REFRESH_RESPONSE"
# 获取 token
ACCESS_TOKEN=`grep -o "\"access_token\"\s*:\s*\".*\"" <<<"$REFRESH_RESPONSE" | sed -n -e 's/"//gp' | awk -F': ' '{print $2}'`
echo "Token: $ACCESS_TOKEN"
# FILE=/Users/wangjie/Downloads/wangjiegulu_com_nginx_0515170316.zip
FILE="$1"
#curl -X POST -d '{ "title" : "@$FILE" }' -H "Authorization: Bearer $ACCESS_TOKEN" -H "Content-Type: application/json; charset=UTF-8" https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable -v -#
FOLDER_ID="$2"
FILE_ID="$3"
#ACCESS_TOKEN="$3"
MIME_TYPE=`file --brief --mime-type "$FILE"`
SLUG=`basename "$FILE"`
#FILE_SIZE=`stat -c%s "$FILE"`
FILE_SIZE=`wc -c < $FILE | awk '{print $1}'`
postData="{\"mimeType\": \"$MIME_TYPE\",\"title\": \"$SLUG\",\"parents\": [{\"id\": \"$FOLDER_ID\"}]}"
postDataSize=$(echo $postData | wc -c)
# Curl command to initiate resumable upload session and grab the location URL
echo "FILE_ID: $FILE_ID"
echo "Generating upload link for file $FILE ..."
if [ "$FILE_ID" = "" ]
then
uploadlink=`curl --silent \
-X POST \
-H "Host: www.googleapis.com" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json; charset=UTF-8" \
-H "X-Upload-Content-Type: $MIME_TYPE" \
-H "X-Upload-Content-Length: $FILE_SIZE" \
-d "$postData" \
"https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable" \
-v -# \
--dump-header - | sed -ne s/"Location: "//p | tr -d '\r\n'`
else
echo "FILE_ID not empty: ${FILE_ID}"
uploadlink=`curl --silent \
-X PUT \
-H "Host: www.googleapis.com" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json; charset=UTF-8" \
-H "X-Upload-Content-Type: $MIME_TYPE" \
-H "X-Upload-Content-Length: $FILE_SIZE" \
-d "$postData" \
"https://www.googleapis.com/upload/drive/v2/files/${FILE_ID}?uploadType=resumable" \
-v -# \
--dump-header - | sed -ne s/"Location: "//p | tr -d '\r\n'`
fi
echo "uploadlink: $uploadlink"
# Curl command to push the file to google drive.
# If the file size is large then the content can be split to chunks and uploaded.
# In that case content range needs to be specified.
echo "Uploading file $FILE to google drive..."
curl \
-X PUT \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: $MIME_TYPE" \
-H "Content-Length: $FILE_SIZE" \
-H "Slug: $SLUG" \
--data-binary "@$FILE" \
--output ./output \
"$uploadlink" \
$curl_args \
-v -#
Last active
May 31, 2019 07:13
-
-
Save wangjiegulu/77a6a347cf4d6e5f378f667b0ed5d3d1 to your computer and use it in GitHub Desktop.
Upload file to google drive via shell command.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment