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