question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

drive.files.create creates a file with 0 bytes

See original GitHub issue

Environment details

  • OS: macOS 11.4
  • Node.js version: 14.16.1
  • npm version: 6.14.12
  • googleapis version: 78.0.0

Description

Files uploaded to Google Drive using a stream or buffer have length of zero bytes. No errors anywhere. Works only when body is a string, but how do you make a string from a binary file and how do you specify the charset for a text file?

Steps to reproduce

const { google } = require('googleapis');
const Readable = require('stream').Readable;

async function uploadToGoogle(googlePrivateKey) {
  const scopes = [
    'https://www.googleapis.com/auth/drive'
  ];

  const auth = new google.auth.JWT(
    ***********@**********.iam.gserviceaccount.com', null,
    googlePrivateKey, scopes
  );

  const drive = google.drive({ version: 'v3', auth });

  const buffer = Buffer.from('Test data 1', 'latin1');
  
  const driveFile = await drive.files.create({
    requestBody: {
      name: 'testfile.txt',
      mimeType: 'text/plain',
      parents: [
        '***************************************'
      ],
      supportsAllDrives: true
    },
    media: {
      mimeType: 'text/plain',
      body: new Readable(buffer)
    }
  });
}

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tapzcommented, Oct 19, 2022

@Rafahur03

const axios = require('axios');
const { v4: uuidv4 } = require('uuid');

const { google } = require('googleapis');

const drive = google.drive({ version: 'v3', auth });

const res = await drive.files.list({
  q: `'${rootFolderId}' in parents`,
  pageSize: '1000',
  supportsAllDrives: 'true',
});

const folderId = res.data.files.find(f => f.name === folderName)?.id;

const boundary = uuidv4();
const jsonBody = JSON.stringify({
  name: filename,
  parents: [folderId]
});

const bodyHead = 
  `--${boundary}\r\n` + 
  'content-type: application/json\r\n' + 
  '\r\n' +
  `${jsonBody}\r\n` + 
  `--${boundary}\r\n` + 
  `content-type: ${mimeType}\r\n` + 
  'content-transfer-encoding: binary\r\n' +
  `content-length: ${buffer.length}\r\n` +
  '\r\n';

const bodyTail = `\r\n--${boundary}--`;

const response = await axios.post(
  'https://www.googleapis.com/upload/drive/v3/files?fields=id&uploadType=multipart', 
  Buffer.concat([
    Buffer.from(bodyHead),
    buffer,
    Buffer.from(bodyTail)
  ]), {
  headers: {
    'content-type': `multipart/related; boundary=${boundary}`,
    Authorization: res.config.headers.Authorization,
    Accept: 'application/json'
  }
});
0reactions
OlivierChirouzecommented, Nov 21, 2022

@tapz, you saved my day!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Files Become 0 Bytes? Top 3 Ways to Restore Zero Byte Files
Solution 1. Fix and Recover Zero Byte Files in CMD · Open the Run dialog box by pressing Win and R keys together....
Read more >
Thousands of 0 byte files taking up all my drive space. Help?
Answer: Yes. I have quite an extensive database in Google Photos stored in High Quality (Unlimited until June of 2021). As far as...
Read more >
What are zero-byte files and how do I deal with them?
A zero -byte file is a file that does not contain any data. While most files contain several bytes, kilobytes (thousands of bytes)...
Read more >
Why is my uploaded file 0 bytes? | Media Temple Community
There are a few common causes for uploaded files to read as 0 bytes : The file was corrupted during transfer. This can...
Read more >
How to Recover Data from 0 Bytes Hard Drive and Fix the Issue
Malware – In this case, a 0 byte virus could have infected your computer, leading your files, folders and/or disk to read 0...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found