【问题标题】:Downloading a lot of images with NodeJS使用 NodeJS 下载大量图像
【发布时间】:2021-01-19 16:18:44
【问题描述】:

我有大量的 URL (8000+) 我想从中下载图像。 我创建了一个脚本来下载文件,只要我将限制设置为大约 100。 如果我尝试下载更多内容,我会收到类似的错误

  • (节点:6740)UnhandledPromiseRejectionWarning:错误:读取 ECONNRESET
  • (节点:3808)UnhandledPromiseRejectionWarning:错误:连接 ETIMEDOUT
  • (节点:7052)UnhandledPromiseRejectionWarning:错误:客户端网络套接字在建立安全 TLS 连接之前已断开

这是用 URL 读取我的 CSV 的代码:

const fotoDownload = require('./async-foto');
const csv = require('csv-parser');
const fs = require('fs');
const results = [];

fs.createReadStream('\\\\hk-nas02\\import\\Partij\\Files\\partij.csv')
    .pipe(csv( { separator: ';'}))
    .on('data', (data) => results.push(data))
    .on('end', () => {
        console.log(results.length)

        let NoPartijen = results.length;
                
        for(i = 0;i < 50; i++){
            //console.log(results[i]);
            itemno = i
            path = '..\\files\\images\\'+results[i]['partij.VPARTIJNR']+'_'+results[i]['partij.PARTIJNR']+'_H.jpg';
            console.log(path)
            
            fotoDownload.fotoDownload(results[i]['partij.EXFOTOURL'], path, itemno)
            
        }

        console.log('Test');
    });

并调用以下代码下载:

const util = require('util')
const fs = require('fs')
const axios = require("axios").default;


module.exports = {
    fotoDownload: async (url, path, itemno) => {
        try {
            const response = await axios({
                method: "GET",
                url: url,
                responseType: "stream",})
            
            await response.data.pipe(fs.createWriteStream(path));
            console.log('Start foto download' + itemno);
            return;
            } catch (err) {
                throw new Error(err)
            }
        
    }
}

我假设我需要将数据切成块或其他东西,但我在这里有点迷失了。有人能把我引向正确的方向吗?

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    您需要设置下载图像的超时时间并添加连接保持活动

    类似的东西

    axios.defaults.timeout = 30000; //or whateve your desired timeout
    axios.defaults.httpsAgent = new https.Agent({ keepAlive: true });
    module.exports = {
        fotoDownload: async (url, path, itemno) => {
            try {
                const response = await axios({
                    method: "GET",
                    url: url,
                    responseType: "stream",})
                
                await response.data.pipe(fs.createWriteStream(path));
                console.log('Start foto download' + itemno);
                return;
                } catch (err) {
                    throw new Error(err)
                }
            
        }
    }
    

    注意:请确保您已安装 https 运行npm i http or npm i https

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-20
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      相关资源
      最近更新 更多