3

I wrote the following code to upload a file to my Google Drive account. It works just fine. Then i tried to add some upload progress tracker but i ran into some issues. It works but the progress goes almost instantly to 100%, way before the upload is completed.

export async function uploadFile(auth) {
  const fileMetadata = {
    name: FILENAME,
  };
  const fileWrite = fs.createReadStream(path.join(homedir(), 'Pictures', FILENAME));
  const fileSize = fs.statSync(path.join(homedir(), 'Pictures', FILENAME)).size;
  const media = {
    mimeType: FILETYPE,
    body: fileWrite,
  };
  const drive = google.drive({ version: 'v3', auth });
  const res = await drive.files.create({
    fileMetadata,
    media,
    fields: 'id',
  }, {
    onUploadProgress: (evt) => {
      const progress = (evt.bytesRead / fileSize) * 100;
      console.log(progress);
    },
  });
  console.log(res.data);
}

It uploads the file i want, it shows the progress and it returns the file id. The problem is that the upload progress is wrong. I tried to upload a 100mb file and, with a 20mbps upload connection, it only took 1.8 seconds for the progress to be completed. The promise file upload resolved after 14 seconds

tehhowch
  • 9,221
  • 4
  • 23
  • 40
Pier Brown
  • 31
  • 1
  • I suspect it's a confusion over onUploadProgress. I can't find any documentation for that callback, but I guess it's only appropriate for resumable uploads. – pinoyyid Aug 10 '18 at 21:19
  • Possibly related ([`onUploadProgress`](https://google.github.io/google-api-nodejs-client/interfaces/_lib_api_.methodoptions.html#onuploadprogress) is an Axios thing): https://stackoverflow.com/questions/44893946/axios-upload-progress-for-multiple-file-uploads https://github.com/axios/axios/pull/423 – tehhowch Aug 12 '18 at 19:56
  • Also see https://github.com/axios/axios/issues/639 – tehhowch Aug 12 '18 at 20:06
  • did you find any solution ? – AHméd Net Aug 28 '18 at 14:22

0 Answers0