【问题标题】:Uploading PDF Content Into An S3 Bucket将 PDF 内容上传到 S3 存储桶
【发布时间】:2018-08-31 04:33:11
【问题描述】:

我正在尝试从远程位置下载包含数据的 PDF 内容,并将内容作为 pdf 文件上传到 S3。我在 AWS lambda 的上下文中使用 NodeJS。 s3.putObject参数函数解析成功,一个pdf文件按预期保存到S3桶中,但是查看文档是空白的,提示可能所有数据都没有传给s3.putObject。

这是我的代码。

const request = require('request')
const viewUrl = "https://link_to_downloadable_pdf/"
    
const options = {
  url: viewUrl,
  headers: {
    'Content-Type': 'application/pdf'
  }
};

request(options, function(err, res, body){
  if(err){return console.log(err)}
  const base64data = new Buffer(body, 'binary');
  const params = {
    Bucket: "myS3bucket",
    Key: "my-pdf.pdf",
    ContentType: "application/pdf",
    Body: base64data,
    ACL: 'public-read'
  };  
  
  s3.putObject(params, function(err, data) {
      if (err) {
        console.log(err);
      } else {
        callback(null, JSON.stringify(data))
      }  
  })

当我在 Postman 中测试 URL 时,它会返回包含数据的 PDF。知道为什么 NodeJS 代码可能不会做同样的事情吗?

【问题讨论】:

  • 我怀疑您需要将 PDF 响应流式传输到 putObject(),或者将整个文件下载到本地 PDF,然后从那里上传(但流式传输会更好)。
  • 问题解决了吗?

标签: node.js amazon-web-services pdf amazon-s3 aws-lambda


【解决方案1】:

你能试试这个代码吗? :)

    import AWS from 'aws-sdk'
    const request = require('request')
    const S3 = new AWS.S3()

    var promise = new Promise((resolve, reject) => {
        return request({ url : 'https://link_to_downloadable_pdf/', encoding : null }, 
        function(err, res, body){

            if(err)
                return reject({ status:500,error:err })

            return resolve({ status:200, body: body})        
        })
    })

    promise.then((pdf) => {

        if(pdf.status == 200)
        {
            console.log('uploading file..')

            s3.putObject({
                Bucket: process.env.bucket,
                Body: pdf.body,
                Key: 'my-pdf.pdf',
                ACL:'public-read'
            }, (err,data) => {
                if(err)
                    console.log(err)
                else
                    console.log('uploaded')
            })
        }
    })

我会注意任何事情。希望能帮到你

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 2020-06-16
    • 2018-12-30
    • 2017-04-19
    • 1970-01-01
    • 2018-08-13
    • 2021-10-07
    • 1970-01-01
    • 2018-04-25
    相关资源
    最近更新 更多