2

Please can I get some help as to why my code is throwing a Pipe error, using a PassThrough: TypeError: dest.on is not a function

I originally thought it was because I was not returning the PassThrough, as outlined in the other post

Now I'm not so sure? Thanks in advance

const { google } = require('googleapis')
const auth =  require('./googleOAuth')
const aws = require('aws-sdk')
const fs = require('fs')
const stream = require('stream')

// AWS S3 bucket name to upload to
const awsBucketName = 'my-backup'

// get AWS keys stored in local file and pass through to AWS auth
const getAWSKeys = async () => {
    const awsKeys = await auth.read('./cred/awskeys.json').then(result => {return result})
    aws.config.update({
        accessKeyId: awsKeys.keys.aws_access_key_id,
        secretAccessKey: awsKeys.keys.aws_secret_access_key
      })
}

// upload a file to AWS S3 by passing the file stream from getGFileContent into the 'body' parameter of the upload
const s3Upload = async () => {
  await getAWSKeys()
  let pass = new stream.PassThrough()
  let params = {
      Bucket: awsBucketName, // bucket-name
      Key: 'filePath.jpg', // file will be saved as bucket-name/[uniquekey.csv]
      Body: pass  // file data passed through stream
  } 
  new aws.S3().upload(params).promise()
    .then(() => console.log(`Successfully uploaded data to bucket`))
    .catch( err => console.log(`Error, unable to upload to S3: ${err}`))
  return pass
}

// download gFile, non google docs files. Downloaded as a stream of data and pipped into the awsUpload function
const getGFileContent = async () => {
  const gKeys = await auth.get()
  const drive = google.drive({version: 'v3', auth: gKeys})
  let params = {fileId: '1bNr_ZM90fM0EnPcFPfdd2LnB7Z2Tts3LiQ', mimeType: "image/jpeg", alt: 'media'}

  return drive.files.get(params, {responseType: 'stream'})
  .then(res => {
    return new Promise((resolve, reject) => {
      res.data
        .on('end', () => {resolve()})
        .on('error', err => {reject(`Error downloading Google docs file: ${err}`)})
        .pipe(s3Upload())
        })
    })
}

getGFileContent()

0 Answers0