• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

300
Views
¿Cómo cargar un archivo de texto en Google Drive?

¿Cómo puedo cargar un archivo de texto en Google Drive usando CloudFunctions?

El archivo se creó en la carpeta local, pero no estoy seguro de cómo puedo pasar esto en el cuerpo de los metadatos.

He seguido el siguiente enfoque:

Paso 1: Escribir archivo

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

Paso 2: preparar los metadatos

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

Paso 3: Llame a la API de Google Drive para cargar el archivo

 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); });

Entonces todo el código de la función es:

 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); }); });

Básicamente, la aplicación móvil llamará a CloudFunction al pasar accessToken y parentFolderId y cloudFunction cargará el archivo después de alguna lógica comercial.

over 3 years ago · Santiago Trujillo
2 answers
Answer question

0

No puede pasar el archivo como metadatos. En su lugar, puede pasarlo como datos de formulario como este:

 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, });
over 3 years ago · Santiago Trujillo Report

0

Finalmente, se resolvió utilizando el siguiente enfoque:

 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); });

Este enlace ayudó a resolver este problema.

Publicado en nombre del autor de la pregunta

over 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error