如何使用node.js中的单个文件(Amazon Webservices上的Lambda函数)解压缩.gz存档?

问题描述:

我对JavaScript,AWS-Lambda和node.js(基本上我是所有这些技术的新手)都很陌生,但我也有一些Java开发经验。如何使用node.js中的单个文件(Amazon Webservices上的Lambda函数)解压缩.gz存档?

我试图解压缩/解压缩作为Lambda函数在AWS Lambda中承载的node.js应用程序/函数中的.gz文件。但我不知道如何解压缩文件。这里是我的代码:

var async = require('async'); 
var JSZip = require('jszip'); 
var AWS = require('aws-sdk'); 

var s3 = new AWS.S3(); 

exports.handler = function(event, context, callback) { 
    // Read options from the event. 
    console.log("AWS Function Start!"); 

    var srcBucket = event.Records[0].s3.bucket.name; 
    // Object key may have spaces or unicode non-ASCII characters. 
    var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); 

    async.waterfall([ 
     function download(next) { 
      // Download the gz file from S3 into a buffer and pass the buffer to next function 
      s3.getObject({ 
        Bucket: srcBucket, 
        Key: srcKey 
       }, 
       next); 
      }, 

     function secondFunction(response, next) { 
      console.log("scondFunction") 

      var zip = new JSZip(response.body); 
      async.forEach(zip.files, function (zippedFile) { 

      var f = zippedFile; 
      console.log(f.name); 
      }); 

     }], function (err) { 
      if (err) { 
       console.error("error"); 
      } else { 
       console.log("success"); 
      } 

      callback(null, "message"); 
     } 
    ); 

}; 

因此,我首先从Amazon S3加载aws对象(.gz存档文件)。这工作正常,我有文件(作为字节流),我猜。所以我认为,我上传的Lambda包是可以的,因为我使用npm来安装所需的库。但是,当功能AWS LAMBDA被触发时,我得到了以下错误消息:

2017-06-11T15:55:18.612Z ef56fbfe-4ebd-11e7-ad06-05089152cc1f Error: The constructor with parameters has been removed in JSZip 3.0, please check the upgrade guide. 
at Object.JSZip (/var/task/node_modules/jszip/lib/index.js:14:15) 
at secondFunction (/var/task/AwsHtwFunction.js:28:14) 
at nextTask (/var/task/node_modules/async/dist/async.js:5273:14) 
at Response.next (/var/task/node_modules/async/dist/async.js:5280:9) 
at Response.<anonymous> (/var/task/node_modules/async/dist/async.js:906:16) 
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:360:18) 
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:105:20) 
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:77:10) 
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:673:14) 
at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10) 

我知道,这意味着什么,但我可以`吨弄清楚如何正确地做到这一点。它甚至是我正在使用的正确的库吗?

希望有人能帮助。如有需要,我会提供任何进一步的信息,这是解决我的问题所必需的。

非常感谢!

EDIT 2017年6月14日19:09:模块jzip内 内容的package.json文件:

{ 
    "_args": [ 
    [ 
     { 
     "raw": "jzip", 
     "scope": null, 
     "escapedName": "jzip", 
     "name": "jzip", 
     "rawSpec": "", 
     "spec": "latest", 
     "type": "tag" 
     }, 
     "C:\\Users\\User\\Desktop\\AWS\\test" 
    ] 
    ], 
    "_from": "[email protected]", 
    "_id": "[email protected]", 
    "_inCache": true, 
    "_location": "/jzip", 
    "_nodeVersion": "4.5.0", 
    "_npmOperationalInternal": { 
    "host": "packages-12-west.internal.npmjs.com", 
    "tmp": "tmp/jzip-1.0.0.tgz_1480154340888_0.22487609554082155" 
    }, 
    "_npmUser": { 
    "name": "clarketm", 
    "email": "[email protected]" 
    }, 
    "_npmVersion": "3.10.6", 
    "_phantomChildren": {}, 
    "_requested": { 
    "raw": "jzip", 
    "scope": null, 
    "escapedName": "jzip", 
    "name": "jzip", 
    "rawSpec": "", 
    "spec": "latest", 
    "type": "tag" 
    }, 
    "_requiredBy": [ 
    "#USER" 
    ], 
    "_resolved": "https://registry.npmjs.org/jzip/-/jzip-1.0.0.tgz", 
    "_shasum": "78e15fc5cfd3387315900473d7efe85bc52d6f8c", 
    "_shrinkwrap": null, 
    "_spec": "jzip", 
    "_where": "C:\\Users\\User\\Desktop\\AWS\\test", 
    "author": { 
    "name": "clarketm" 
    }, 
    "bugs": { 
    "url": "https://github.com/clarketm/jszip/issues" 
    }, 
    "dependencies": {}, 
    "description": "", 
    "devDependencies": {}, 
    "directories": {}, 
    "dist": { 
    "shasum": "78e15fc5cfd3387315900473d7efe85bc52d6f8c", 
    "tarball": "https://registry.npmjs.org/jzip/-/jzip-1.0.0.tgz" 
    }, 
    "gitHead": "ca81b063424ea31f19b531c7bc9136dc19a75ea1", 
    "homepage": "https://github.com/clarketm/jszip#readme", 
    "keywords": [ 
    "jszip" 
    ], 
    "license": "MIT", 
    "main": "jzip.js", 
    "maintainers": [ 
    { 
     "name": "clarketm", 
     "email": "[email protected]" 
    } 
    ], 
    "name": "jzip", 
    "optionalDependencies": {}, 
    "readme": "ERROR: No README data found!", 
    "repository": { 
    "type": "git", 
    "url": "git+https://github.com/clarketm/jszip.git" 
    }, 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "version": "1.0.0" 
} 
+0

你现在有什么版本的jszip? –

+0

感谢您的快速回复!根据我的package.json文件npm安装了jzip的1.0.0版本。 – Arraphi

+0

这似乎与aws-lambda非常相关。看起来您只需花时间查看jszip API并更改代码以正确使用它。该错误消息特别告诉你不要使用带参数的构造函数。你是从一个旧的例子或其他东西复制代码?查看官方API文档中的示例以了解如何使用此库应该是微不足道的:http://stuk.github.io/jszip/documentation/api_jszip/load_async.html –

因为我无法弄清楚如何做到这一点的(节点)和JS AWS Lambda也支持Java,我设法通过使用Java来实现它:

GZIPInputStream gzis = new GZIPInputStream(myInputStream); 
InputStreamReader reader_gz = new InputStreamReader(gzis); 
BufferedReader bufferedReader = new BufferedReader(reader_gz); 

while ((readedLine = bufferedReader.readLine()) != null) { 
    //It is a text file, so I can read it line per line 
    //do sth. with line... 
}