简体   繁体   English

节点get请求返回zip文件,需要将JSON导入MSSQL

[英]Node get request returns zip file need to import JSON into MSSQL

I am using node to make a get request for JSON. 我正在使用node发出JSON的get请求。 It is returning a zip file and all the data is just a bunch of numbers. 它返回一个zip文件,所有数据只是一堆数字。 I cant find anything online to show me how to extract the data from the zip file to actually read the JSON file. 我无法在网上找到任何东西来向我展示如何从zip文件中提取数据以实际读取JSON文件。 How do I open the zip at this point so I can put it into MSSQL Server? 此时如何打开zip以便将其放入MSSQL Server?

I get the request but not sure how to read it from here, it just says its an attachment. 我收到了请求,但不确定如何从这里阅读它,只是说它是附件。

"content-disposition":["attachment; filename=FileName.zip"]

This is the get request I am calling in node: 这是我在节点中调用的get请求:

require('dotenv').config();
const fetch = require('fetch-everywhere');
const base64 = require('base-64');
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;

const user = "Enter Username Here";
const pass = "Enter Password Here";
const headers = new Headers({
  "Authorization": `Basic ${base64.encode(`${user}:${pass}`)}`
});

fetch('Link', {
  method: 'GET',
  headers: headers,
})
  .then(function(response) {
    return response;
  })
  .then(function(myJson) {
    console.log(JSON.stringify(myJson));
  }).catch(error => {throw error});

So the return is a zip file. 因此,返回的是一个zip文件。 Where do I go from here if I need to dump it into MSSQL Server? 如果需要将其转储到MSSQL Server中,该从哪里去?

Any help would be greatly appreciate! 任何帮助将不胜感激!

You mentioned the file is very large (10GB, first, then edited to 100MB) in your edited comment. 您在编辑的注释中提到文件非常大(首先为10GB,然后再编辑为100MB)。 It would not be viable to store this file in MSSQL, as rdbms systems are not designed to store large files. 在MSSQL中存储此文件是不可行的,因为rdbms系统并非旨在存储大文件。 You may want to consider storing this object to Azure storage or AWS S3 for example. 例如,您可能要考虑将此对象存储到Azure存储或AWS S3。 Please read this if you need more information https://softwareengineering.stackexchange.com/questions/150669/is-it-a-bad-practice-to-store-large-files-10-mb-in-a-database 如果您需要更多信息,请阅读此内容https://softwareengineering.stackexchange.com/questions/150669/is-it-a-bad-practice-to-store-large-files-10-mb-in-a-database

I suspect nodejs may not be a good technology for this download / extraction either, and you may want to consider another technology which can benefit from higher resource consumption and multi threading. 我怀疑nodejs可能也不是这种下载/提取的好技术,您可能要考虑另一种可以从更高的资源消耗和多线程中受益的技术。 However, if you wish to do this in nodejs, it boils down to three steps 但是,如果您希望在nodejs中执行此操作,则可以归结为三个步骤

  • Download large file 下载大文件
  • Unzip large file 解压缩大文件
  • Store large file to somewhere (like azure or amazon storage) 将大文件存储到某处(例如天蓝色或亚马逊存储)

In order to download your file without running out of memory, you need to stream/pipe it, I don't know how fetch-everywhere will handle this, so I have provided an example of request streaming using request instead. 为了在不耗尽内存的情况下下载文件,您需要对它进行流式传输/管道传输,我不知道fetch-everywhere可以进行fetch-everywhere ,因此我提供了一个使用request流式传输request的示例。 I also picked node-stream-zip as it has a streaming api which will not load the entire set into memory. 我还选择了node-stream-zip因为它具有流式API,不会将整个集合加载到内存中。

I have left the "storage" part of the code empty, as I could not possibly know where you want to store it and MSSQL is not an option. 我将代码的“存储”部分留空,因为我可能不知道您要将其存储在哪里,而MSSQL则不是一种选择。

const fs = require('fs');
const request = require('request');
const progress = require('request-progress');
const StreamZip = require('node-stream-zip');

// download large file...
const url = 'example.com/path/to/large/file';
const zipPath = 'generateATmpFileName.zip';
const jsonPath = 'generateATmpFileName.json';
progress(request(url))
    .on('end', function () {
        // file has been completely downloaded, lets unzip it
        var zip = new StreamZip({  
            file: zipPath,  
            storeEntries: true    
        });
        zip.on('ready', function() {
            zip.stream('path-to-json-file-in-zip.json', function(error, zstream) {
                zstream.pipe(fs.createWriteStream(jsonPath));
                zstream.on('end', function() { 
                    zip.close();
                    // file has been completely extracted, do what you want with it
                });
            });
        });
    })
    .pipe(fs.createWriteStream(zipPath));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM