【问题标题】:Download pdf files from external url's - Heroku, NodeJS, Angular 7从外部 url 下载 pdf 文件 - Heroku、NodeJS、Angular 7
【发布时间】:2020-06-02 13:50:57
【问题描述】:

我正在尝试将多个 pdf 文件从外部源临时下载到我的 nodejs 服务器(在 Heroku 中)并将其上传到 AWS S3 存储桶。

我尝试了多种方法,所有这些方法在我的本地机器上都可以正常工作,但在 Heroku Dyno NodeJS Server 中却不行。我什至无法在 Heroku 中创建文件夹。我猜是因为权限有限。

在节点中

1) 使用 var download = require('download-file') (目前在下面的代码中使用)

2) axios

3) res.download()

下载文件代码

const downloadFiles = async (unique_files) =>  {

  for (let index = 0; index < unique_files.length; index++) {
    let file_ext = unique_files[index].substr(unique_files[index].length - 4);
      if(file_ext == ".pdf") {
        await downloadzz(unique_files[index])
      }
  }

}

function downloadzz(link) {
  download(link, function(err){ 
    if (err) throw err
    console.log("DOWNLOAD Complete");

  });
}

上传文件代码

const uploadFiles = async (unique_files) =>  {

  for (let index = 0; index < unique_files.length; index++) {
    let file_ext = unique_files[index].substr(unique_files[index].length - 4);
      if(file_ext == ".pdf") {
        await uploadzz(unique_files[index])
      }
  }
}

function uploadzz(link) {
    fs.readFile(require('path').resolve(__dirname+'/../external-pdfs/', link.slice(link.lastIndexOf('/') + 1)), function (err, data) {

    params = {Bucket: pdfBucket, Key: link.slice(link.lastIndexOf('/') + 1), Body: data, ACL: "public-read" };
    s3.putObject(params, function(err, data) {
        if (err) {
            console.log("Failed Upload", err);
        } else {
            console.log("Successfully uploaded data to bucket", data);
        }
    });

  });
}

我没有收到任何错误,但在 heroku 服务器上似乎不存在名称为 external-pdfs 的文件夹。

我愿意寻求更好的解决方案:例如,直接将文件从外部 url 上传到 s3... 如何从外部 url 读取文件并直接上传到 AWS S3 存储桶?

【问题讨论】:

    标签: node.js amazon-web-services heroku amazon-s3


    【解决方案1】:

    您可以使用axios。将responseType设置为stream,即可获取文件数据并作为正文传递。这是从 URL 获取 pdf 并将其信息直接上传到 S3 的示例代码:

    const AWS = require('aws-sdk');
    const axios = require('axios');
    
    AWS.config.loadFromPath('./config.json');
    const s3 = new AWS.S3({apiVersion: '2006-03-01'});
    const URL = "<YOUR_URL>";
    
    const uploadPdfToS3 = async () => {
        try{
            const {data, headers} = await axios.get(URL, {responseType: 'stream'});
            // Create params for putObject call
            const objectParams = {
                Bucket: "<YOUR_BUCKET>", 
                Key: "<YOUR_KEY>", 
                ContentLength: headers['content-length'],
                Body: data
            };
            // Create object upload promise
            await s3.putObject(objectParams).promise();
        } catch(err){
            console.log("ERROR --->" + err)
        }
    }
    

    【讨论】:

      【解决方案2】:

      在 Angular 中,我们可以使用 FileSaver 库来保存库中的 pdf 文件。

      找到下面的示例代码来做到这一点。 enter image description here

      【讨论】:

        猜你喜欢
        • 2023-02-17
        • 1970-01-01
        • 2018-10-06
        • 2020-11-04
        • 2023-03-22
        • 2018-05-10
        • 1970-01-01
        • 1970-01-01
        • 2019-12-16
        相关资源
        最近更新 更多