Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active Dec 10, 2021
Embed
What would you like to do?
Uploading Files of multipart/form-data to Google Drive using Drive API with Node.js

Uploading Files of multipart/form-data to Google Drive using Drive API with Node.js

These are the sample scripts for uploading files of multipart/form-data to Google Drive using Drive API with Node.js. In this case, googleapis for Node.js is not used.

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.

Sample script 1

This sample script uploads a file using the modules of fs and request. Before you use this script, please prepare your access token for uploading the file.

const fs = require("fs");
const request = require("request");

const filePath = "./sample.txt";
const accessToken = "###";

fs.readFile(filePath, function (err, content) {
  if (err) {
    console.error(err);
  }
  const metadata = {
    name: "sample.txt",
  };
  const boundary = "xxxxxxxxxx";
  let data = "--" + boundary + "\r\n";
  data += 'Content-Disposition: form-data; name="metadata"\r\n';
  data += "Content-Type: application/json; charset=UTF-8\r\n\r\n";
  data += JSON.stringify(metadata) + "\r\n";
  data += "--" + boundary + "\r\n";
  data += 'Content-Disposition: form-data; name="file"\r\n\r\n';
  const payload = Buffer.concat([
    Buffer.from(data, "utf8"),
    Buffer.from(content, "binary"),
    Buffer.from("\r\n--" + boundary + "--\r\n", "utf8"),
  ]);
  request(
    {
      method: "POST",
      url:
        "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
      headers: {
        Authorization: "Bearer " + accessToken,
        "Content-Type": "multipart/form-data; boundary=" + boundary,
      },
      body: payload,
    },
    (err, res, body) => {
      if (err) {
        console.log(body);
        return;
      }
      console.log(body);
    }
  );
});

Sample script 2

This sample script uploads a file using the modules of fs, form-data and node-fetch. Before you use this script, please prepare your access token for uploading the file.

const fs = require("fs");
const FormData = require("form-data");
const fetch = require("node-fetch");

const filePath = "./sample.txt";
const accessToken = "###";

token = req.body.token;
var formData = new FormData();
var fileMetadata = {
  name: "sample.txt",
};
formData.append("metadata", JSON.stringify(fileMetadata), {
  contentType: "application/json",
});
formData.append("data", fs.createReadStream(filePath), {
  filename: "sample.txt",
  contentType: "text/plain",
});
fetch("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", {
  method: "POST",
  body: formData,
  headers: { Authorization: "Bearer " + accessToken },
})
  .then((res) => res.json())
  .then(console.log);

Result

When above scripts are run, the following result is retrieved from both scripts.

{
  "kind": "drive#file",
  "id": "###",
  "name": "sample.txt",
  "mimeType": "text/plain"
}

Reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment