我想发送压缩数据(gzip)到一些URL,这将触发(代理) lambda函数,这将解压缩数据。
λ函数(NodeJS 8):
let zlib = require('zlib');
exports.handler = async (event) => {
let decompressedData = zlib.gunzipSync(event['body'])
return {
"statusCode": 200,
"body": decompressedData.toString()
};
};
我使用curl命令触发它(通过API网关),对于一些我用gzip压缩example.gz
的文件:
curl -X POST --data-binary @example.gz https://URL...
结果,我得到了:
{"message": "Internal server error"}
错误是(Cloudwatch中的日志):
"errorMessage": "incorrect header check",
"errorType": "Error",
"stackTrace": [
"Gunzip.zlibOnError (zlib.js:153:15)",
"Gunzip._processChunk (zlib.js:411:30)",
"zlibBufferSync (zlib.js:144:38)",
"Object.gunzipSync (zlib.js:590:14)",
"exports.handler (/var/task/test_index.js:5:33)"
]
当我查看event['body']
本身时,我看到的数据与我在example.gz
中看到的完全相同。也许我需要一些特殊的标题?我只想按原样传递数据。
发布于 2019-04-12 15:28:32
正如Michael - sqlbot所说,默认情况下,API Gateway不能将二进制数据传递给Lambda函数。
适合我的方法:我在curl命令中添加了头Content-Type: application/octet-stream
,在Binary Media Types
上的API网关设置中添加了application/octet-stream
。
这样,数据在base64中传递,然后我只是将base64中的日期转换为缓冲区:
let data = Buffer.from(event['body'], "base64")
然后再解压。
有关更多信息,请访问read here
发布于 2019-04-10 12:09:09
1/首先,您需要正确地构建gzip,确保gzip文件头文件不存在:curl command a gzipped POST body to an apache server
错误的方式:
echo '{ "mydummy" : "json" }' > body
gzip body
hexdump -C body.gz
00000000 1f 8b 08 08 20 08 30 59 00 03 62 6f 64 79 00 ab |.... .0Y..body..|
00000010 56 50 ca ad 4c 29 cd cd ad 54 52 b0 52 50 ca 2a |VP..L)...TR.RP.*|
00000020 ce cf 53 52 a8 e5 02 00 a6 6a 24 99 17 00 00 00 |..SR.....j$.....|
00000030
好方法:
echo '{ "mydummy" : "json" }' | gzip > body.gz
hexdump -C body.gz
00000000 1f 8b 08 00 08 0a 30 59 00 03 ab 56 50 ca ad 4c |......0Y...VP..L|
00000010 29 cd cd ad 54 52 b0 52 50 ca 2a ce cf 53 52 a8 |)...TR.RP.*..SR.|
00000020 e5 02 00 a6 6a 24 99 17 00 00 00 |....j$.....|
0000002b
2/在curl中,不要忘记用以下命令指定内容编码
-H "Content-Encoding: gzip"
3/另外,如果你使用express+compress,你不需要调用zlib
curl -X POST "http://example.org/api/a" -H "Content-Encoding: gzip" -H "Content-Type: application/json" --data-binary @body.gz
router.post("/api/a", function(req, res){
console.log(req.body); // { mydummy: 'json' }
});
https://stackoverflow.com/questions/55611758
复制