Ever wanted to upload larges files to Google Drive using CURL from your server? But you are lazy to create a new Google App or hesitated to use third party tools? Then this post is for you!
Here we are going to utilize Google OAuth 2.0 Playground application. Let's get started.
Go to https://developers.google.com/oauthplayground/
Then expand Drive API v3 and select https://www.googleapis.com/auth/drive.file
This permission is sufficient for the file uploads. And then Click on Authorize APIs.
Then you will get Authorization code which will be filled automatically as shown below. Click on Exchange authorization code for tokens to get the access token. After that, Refresh token and Access token textboxes will be populated automatically. Tick the check box Auto-Refresh the token before it expires, if you wanted to refresh the access token after it expires. Then click on Step 3
Note: Access token expires after 1 hour. Refresh token expires after 24 hours. If you tick the check box Auto-Refresh the token before it expires then it will refresh the Access Token only, Not Refresh Token. If the Refresh token expired, then you have to start again from Step 1.
In the Step 3, Change the HTTP method to POST. Input the Request URI as https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&supportsAllDrives=true
The query parameter supportsAllDrives=true will be helpful when you are going to upload file to someone else's drive. For example, shared folder. But it won't hurt even if you leave it there and uploading file to your own drive.
It is for Resumable file upload, which is best for large file uploads and the unreliable network connections. More info: https://developers.google.com/drive/api/guides/manage-uploads
and Make sure, Content-Type is application/json
Then Click on Enter request body and enter the following metadata properties.
{
"name": "myfile.zip",
"parents": [
"1BvVXVd-O99thVSuQeycODONsfMRFqox0"
]
}
name - Name of the file you wanted to upload.parents - ID of the Google Drive folder. (Use this if you wish to upload it to a folder, if not specified it will be uploaded to your account's default location)
More info about Properties: https://developers.google.com/drive/api/v3/reference/files
You can get the Folder ID from the URL like below:
As per the above Image, URL is https://drive.google.com/drive/u/1/folders/1BvVXVd-O99thVSuQeycODONsfMRFqox0 , So ID of that Tutorial folder is 1BvVXVd-O99thVSuQeycODONsfMRFqox0
Once you entered the request body, click on Close.
Then, Click on Send the request. You would get the Resumable session URL in the Response location header as shown below. Copy the URL.
Please note that this URL Expires in 7 Days, therefore you have to finish uploading within 7 days.
Once you copied that URL, go to the server where you want to transfer the file to google drive, and use the following curl command(You don't have to specify access token) and replace the URL with your Resumable session URL.
curl -X PUT 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&supportsAllDrives=true&upload_id=ADPycdtpXu71Qra-KqkwyYt12oCKrPT-8KOxw949jBQbt5AmMpBJ-uSgv-Dn3C8aYJnyGsYVg4pAFi1TNI4LxrYFUKaUWQ' -T path-to-your-file.zip
If you want to show progress bar, then add --progress-bar parameter and need to output it to a file, you can output it cat to display it in the console. You can use something like this:
curl --progress-bar -X PUT 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable&supportsAllDrives=true&upload_id=ADPycdtpXu71Qra-KqkwyYt12oCKrPT-8KOxw949jBQbt5AmMpBJ-uSgv-Dn3C8aYJnyGsYVg4pAFi1TNI4LxrYFUKaUWQ' -T path-to-your-file.zip | cat
If your upload got interrupted then don't worry, you may need to add Content-Range header with appropriate values. Follow the steps mentioned here:
Caution!
You need to be careful when sharing Resumable Session URL, Because it allows anyone to upload file to the target folder.
That's All. Please let me know in the comments, if you get any issues. Thank you!
2 Comments
Thankyou very much..... It really workssssss =))))))
ReplyDeleteAnd, it's fast, dont need to create someapp just to get some client secret bla bla bla, that wastes much of time
Thank you ^_^
Delete