Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to upload a file into a specific location in the Google Drive using the JS API?

I'm using googleapis to upload different files to the Google Drive. The scenario is:

  1. User provides a document and it's information through my REST API (I'm using NodeJS).
  2. The REST API creates the directory that will contain the document, if it's not already exist.
  3. The REST API uploads the document to that directory.

The structure of the drive is:

/root/documents/$type/$new_document

where $type is one of the user's provided fields and the $new_document is the document that was provided by the user.

The way I connect:

oauth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });
drive_instance = google.drive({
  version: 'v3',
  auth: oauth2Client,
});

I figured how to upload the document to root folder of the Google Drive:

}
try {
    const response = await drive.files.create({
      requestBody: {
        name: file.name,
        mimeType: file.mimetype,
      },
      media: {
        mimeType: file.mimetype,
        body: file.data,
      },
    });

    console.log(response.data);
} catch (error) {
    console.log(error.message);
}

What I'm struggling is:

  1. How to create the directory /root/documents/$type if it's not already existing?
  2. How to upload the $new_document to /root/documents/$type?

For the second question, I know that the docs provide an option of parents[] that will contain all the folder IDs. but then, how can I get the folder ID of /root/documents/$type? Is there someway to combine the steps (like maybe mkdir -p for the directories or creating the directory will return the ID of the directory).

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!