Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda