3

How can I upload text file on Google drive using CloudFunctions?

File has been created on local folder but not sure how can I pass this in metadata body.

I have followed below approach:

Step 1: Write file

// Write file
fs.writeFileSync("sample.txt", "Hello content!");

Step 2: Prepare metadata


    const metadata = {
      name: "sample.txt",
      parents: [parentFolder],
      mimeType: "text/plain",
      uploadType: "media",
    };

Step 3: Call Google drive API to upload file

    axios({
      method: "POST",
      url: "https://www.googleapis.com/drive/v3/files?supportsAllDrives=true",
      headers: {
        Authorization: `Bearer ${token}`,
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      data: metadata,
    })
      .then((res) => {
        response.status(200).send(res.data);
      })
      .catch((err) => {
        response.status(500).send(err.message);
      });

So whole function code is:

exports.onCreateFile = functions.https.onRequest((request, response) => {
  // <-- Some validation -->
    const parentFolder = request.query.parentFolder;
    if (!parentFolder) {
      response.status(400).send("Parent folder not found");
      return;
    }

    // Write file
    fs.writeFileSync("vault.txt", "Hello content!");

    const metadata = {
      name: "vault.txt",
      parents: [parentFolder],
      mimeType: "text/plain",
      uploadType: "media",
    };

    axios({
      method: "POST",
      url: "https://www.googleapis.com/drive/v3/files?supportsAllDrives=true",
      headers: {
        Authorization: `Bearer ${token}`,
        Accept: "application/json",
        "Content-Type": "application/json",
      },
      data: metadata,
    })
      .then((res) => {
        // console.log(res.data);
        response.status(200).send(res.data);
      })
      .catch((err) => {
        // console.log(err);
        response.status(500).send(err.message);
      });
});

Basically Mobile app will call CloudFunction by passing accessToken and parentFolderId and cloudFunction will upload file after some business logic.

Dharman
  • 30,962
  • 25
  • 85
  • 135

2 Answers2

2

You can not pass the file as metadata. Instead you can pass it as form data like this:

    const metadata = { name: "sample.txt", mimeType: "text/plain", parents: ["root"] };
    const fileContent = "Hello content!";
    const fileBlob = new Blob([fileContent], {type: "text/plain"});
    const form = new FormData();
    form.append("metadata", JSON.stringify(metadata), { type: "application/json" }));
    form.append("file", fileBlob);
    fetch("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsAllDrives=true", {
      method: "POST",
      headers: new Headers({ Authorization: `Bearer ${token}` }),
      body: form,
    });
Mario Varchmin
  • 3,704
  • 4
  • 18
  • 33
  • It returns 400 and now trying multipart upload but no luck. `const form = new FormData(); form.append("file", "Some dummy text"); axios({ method: "POST", url: "https://www.googleapis.com/drive/v3/files?uploadType=multipart&supportsAllDrives=true", headers: { Authorization: `Bearer ${token}`, Accept: "application/json", "Content-Type": `multipart/form-data; boundary=${form.getBoundary()} `, }, data: form, }) .then((res) => { response.status(200).send(res.data); }) ` – Ashish Narnoli Aug 21 '21 at 18:33
  • You didn't append the metadata to the form. Try to stay as close to the sample code I posted as you can. Because the code works. – Mario Varchmin Aug 21 '21 at 19:03
  • It is working but with two issues in my case and maybe you can help: 1: Don't have Blob in Node and I'm writing file with `fs.writeFileSync("/tmp/vault.txt", "Hello Temp content!");`. Appending metadata throws `ReferenceError: Blob is not defined` 2: Uploaded "Untitled" text file has `----------------------------866368930733703415619015 Content-Disposition: form-data; name="file"; filename="vault.txt" Content-Type: text/plain Hello Temp content! ----------------------------866368930733703415619015--` – Ashish Narnoli Aug 23 '21 at 10:38
  • 1
    I updated my answer. See if it is working. Unfortunately I can not test it. If it doesn't work, let me know. You can also try with `blob`: https://stackoverflow.com/a/58896208/7821823 – Mario Varchmin Aug 23 '21 at 12:03
  • Thanks. I am glad that it is resolved and I have updated my questions with update. [link](https://mryqu.github.io/post/upload_file_to_google_drive_using_postman_and_curl/) helped to fix this issue. Headers should be `{ Authorization: `Bearer ${token}`, "Content-Type": `multipart/related; boundary=${formData.getBoundary()}`, }` – Ashish Narnoli Aug 23 '21 at 14:03
1

Finally, it got resolved using the following approach:

fs.writeFileSync("/tmp/sample.txt", "Hello Temp content!");

    const metadata = {
      name: "sample.txt",
      mimeType: "text/plain",
      parents: [parentFolder],
    };

    const formData = new FormData();
    formData.append("metadata", JSON.stringify(metadata), {
      contentType: "application/json",
    });
    formData.append("file", fs.createReadStream("/tmp/sample.txt"));

    axios({
      method: "POST",
      url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&supportsAllDrives=true",
      headers: {
        Authorization: `Bearer ${token}`,
        "Content-Type": `multipart/related; boundary=${formData.getBoundary()}`,
      },
      data: formData,
    })
      .then((res) => {
        response.status(200).send(res.data);
      })
      .catch((err) => {
        response.status(500).send(err);
      });

This Link helped to resolve this issue.

Posted on behalf of the question asker

Dharman
  • 30,962
  • 25
  • 85
  • 135