Home > database >  Download file from Google Drive and upload to S3 using NodeJS
Download file from Google Drive and upload to S3 using NodeJS

Time:08-17

I download my PDF file from Google drive according to the enter image description here

I also tried different response types from Google API such as blob. Any idea what I am missing? Thanks in advance!

CodePudding user response:

You need to export the file as PDF from google drive, use the following function, passing the id:

/**
 * Download a Document file in PDF format
 * @param{string} fileId file ID
 * @return{obj} file status
 * */
async function exportPdf(fileId) {
  const {GoogleAuth} = require('google-auth-library');
  const {google} = require('googleapis');

  // Get credentials and build service
  // TODO (developer) - Use appropriate auth mechanism for your app
  const auth = new GoogleAuth({scopes: 'https://www.googleapis.com/auth/drive'});
  const service = google.drive({version: 'v3', auth});

  try {
    const result = await service.files.export({
      fileId: fileId,
      mimeType: 'application/pdf',
    });
    console.log(result.status);
    return result;
  } catch (err) {
    console.log(err)
    throw err;
  }
}

CodePudding user response:

I managed to solve the issue by converting the stream to buffer and calling the pre-signed S3 URL without using Formdata:

streamToBuffer(stream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    stream
      .on('data', (chunk) => {
        chunks.push(chunk);
      })
      .on('end', () => {
        resolve(Buffer.concat(chunks));
      })
      .on('error', reject);
  });
}

async uploadFileToS3(fileStream, signedUrl) {
  const data = await this.streamToBuffer(fileStream);

  const response = await axios.put(signedUrl, data, {
    headers: {
      'Content-Type': 'application/octet-stream',
    },
  });

  return response;
}

const file = await this.driveClient.files.get(
  {
    fileId: id,
    alt: 'media',
  },
  {
    responseType: 'stream'
  },
);

const uploadedDocument = await this.uploadFileToS3(
  file.data,
  s3Url,
);

  • Related