2017-06-14 103 views
-1

我對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 –

回答

0

因爲我無法弄清楚如何做到這一點的(節點)和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... 
}