Closed
Description
On /samples/drive/export.js there is a mistake
I tried the function a few times and it downloaded 20kb less each time
So I discover that the function finish to run by await to res.data.on('end')
but the stream didn't finish to write the data to the fs yet
The correct function is:
async function runSample() {
// [START main_body]
const fileId = '1EkgdLY3T-_9hWml0VssdDWQZLEc8qqpMB77Nvsx6khA';
const destPath = path.join(os.tmpdir(), 'important.pdf');
const dest = fs.createWriteStream(destPath);
const res = await drive.files.export(
{fileId, mimeType: 'application/pdf'},
{responseType: 'stream'}
);
res.data.pipe(dest)
await new Promise((resolve, reject) => {
dest
.on('finish', () => {
console.log(`Done downloading document: ${destPath}.`);
resolve();
})
.on('error', err => {
console.error('Error downloading document.');
reject(err);
})
});
// [END main_body]
}