0

I am using the Dropbox HTTP API, the AWS SDK for Node, and request-promise-native to download a PDF file from a Dropbox folder and immediately upload it to S3. This code is running in AWS Lambda:

const rp = require('request-promise-native')

const getOptions = {
  url: 'https://content.dropboxapi.com/2/files/download',
  method: 'GET',
  headers: {
    'Authorization': `Bearer ${settings.DROPBOX_TOKEN}`,
    'Dropbox-API-Arg': JSON.stringify({ path: event.path })
  },
  gzip: true,
  resolveWithFullResponse: true
}

rp(getOptions)
  .on('error', callback)
  .then((response) => {

    const putOptions = {
      ACL: 'public-read',
      Bucket: 'mybucket',
      Key: common.dropboxToS3Path(event.path),
      Body: new Buffer(response.body),
      ContentDisposition: `inline; filename=\"${path.basename(event.path)}\"`,
      ContentEncoding: 'utf-8',
      ContentLength: response.headers['content-length'],
      ContentType: 'application/pdf'
    }
    s3.putObject(putOptions, function (err, data) {
      callback(err, data)
    })

  })
  .catch((err) => {
    callback(err)
  })

The Dropbox response's headers have the expected Content-Length and a Content-Disposition set to "attachment," which I am changing in the S3 put operation.

The resulting file on S3 is almost twice as large and cannot be opened by any PDF readers.

What am I missing?

Seth
  • 6,514
  • 5
  • 49
  • 58
  • `ContentEncoding: 'utf-8'` appears wrong: pdfs are not textual but binary in nature, so treating them as UTF8 encoded text can damage them. – mkl Aug 20 '16 at 19:27
  • @mkl You're right, but removing that line didn't solve the problem. – Seth Aug 20 '16 at 20:17
  • Figured it out -- found a duplicate question. I have to tell request-response that the content is binary by setting `encoding: null` on the `getOptions` in my sample. – Seth Aug 20 '16 at 20:23
  • Wow, that's weird. Binary should have been the default. – mkl Aug 20 '16 at 23:08

0 Answers0