Last active
January 5, 2025 06:09
-
-
Save tanaikech/ae451679e8220f3b2d48edb3f8c1a8d3 to your computer and use it in GitHub Desktop.
Revisions
-
tanaikech revised this gist
Jul 30, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ This is a simple sample script for achieving the resumable upload to Google Drive using Node.js. In order to achieve the resumable upload, at first, it is required to retrieve the location, which is the endpoint of upload. The location is included in the response headers. After the location was retrieved, the file can be uploaded to the location URL. In this sample, a PNG file is uploaded with the resumable upload using a single chunk. ## Sample script -
tanaikech revised this gist
Jul 30, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ This is a simple sample script for achieving the resumable upload to Google Drive using Node.js. In order to achieve the resumable upload, at first, it is required to retrieve the location, which is the endpoint of upload. The location is included in the response headers. After the location was retrieved, the file can be uploaded to the location URL. In these sample script, the maximum file size is 5 MB. Please be careful this. When you want to upload the files more than 5 MB, please check [this report](https://gist.github.com/tanaikech/ae451679e8220f3b2d48edb3f8c1a8d3). ## Sample script -
tanaikech created this gist
Mar 5, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ # Simple Script of Resumable Upload with Google Drive API for Node.js This is a simple sample script for achieving the resumable upload to Google Drive using Node.js. In order to achieve the resumable upload, at first, it is required to retrieve the location, which is the endpoint of upload. The location is included in the response headers. After the location was retrieved, the file can be uploaded to the location URL. In this sample, a PNG file is uploaded with the resumable upload using a single chunk. ## Sample script Before you use this, please set the variables. ```javascript const fs = require("fs"); const request = require("request"); const accessToken = "###"; // Please set the access token. const filename = "./sample.png"; // Please set the filename with the path. const fileSize = fs.statSync(filename).size; // 1. Retrieve session for resumable upload. request( { method: "POST", url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable", headers: { Authorization: `Bearer ${accessToken}`, "Content-Type": "application/json" }, body: JSON.stringify({ name: "sample.png", mimeType: "image/png" }) }, (err, res) => { if (err) { console.log(err); return; } // 2. Upload the file. request( { method: "PUT", url: res.headers.location, headers: { "Content-Range": `bytes 0-${fileSize - 1}/${fileSize}` }, body: fs.readFileSync(filename) }, (err, res, body) => { if (err) { console.log(err); return; } console.log(body); } ); } ); ``` ## Reference - [Perform a resumable upload](https://developers.google.com/drive/api/v3/manage-uploads#resumable)