How to get files from google drive in nodejs (typescript)

54 Views Asked by At

I am trying to download html, css, js, etc files from google drive using its api and I am getting error regarding saving file to local storage.

Typescript code:

const destinationFile = fs.createWriteStream(path.join(filePath, file.data.name || ''));
const downloadStream = await drive.files.get({ fileId, alt: 'media' });

downloadStream.data.pipe(destinationFile)
    .on('finish', () => console.log("File downloaded:", file.data.name))
    .on('error', (err: Error) => console.error("Error downloading file:", file.data.name, err));

Error: Property 'pipe' does not exist on type 'GaxiosResponse<Schema$File>'.ts(2339)

path.join(filePath, file.data.name || '') looks like this: dist\output\xj1dh\src\setupTests.js

donwloadStream:

  config: {
    ...
  },
  data: Blob {
    [Symbol(type)]: 'application/javascript',
    [Symbol(buffer)]: <Buffer 2f 2f 20 6a 65 73 74 2d 64 6f 6d 20 61 64 64 73 20 63 75 73 74 6f 6d 20 6a 65 73 74 20 6d 61 74 63 68 65 72 73 20 66 6f 72 20 61 73 73 65 72 74 69 6e ... 196 more bytes> 
  },
  headers: {
    ...
  },
  status: 200,
  statusText: 'OK',
  request: {
    ...
  }
}```


I used chatGPT, it says `downloadStream.data` is buffer not stream and hence not readable.
0

There are 0 best solutions below