Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active January 5, 2025 06:09
Show Gist options
  • Save tanaikech/ae451679e8220f3b2d48edb3f8c1a8d3 to your computer and use it in GitHub Desktop.
Save tanaikech/ae451679e8220f3b2d48edb3f8c1a8d3 to your computer and use it in GitHub Desktop.

Revisions

  1. tanaikech revised this gist Jul 30, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion submit.md
    Original 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).
    In this sample, a PNG file is uploaded with the resumable upload using a single chunk.

    ## Sample script

  2. tanaikech revised this gist Jul 30, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion submit.md
    Original 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.
    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

  3. tanaikech created this gist Mar 5, 2020.
    60 changes: 60 additions & 0 deletions submit.md
    Original 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)