【发布时间】:2018-07-24 18:18:25
【问题描述】:
我正在创建一个解决方案,其中文件和表单数据从 html-form 提交到 Api 网关,然后到 AWS lambda 进行处理,这些数据可以是“metapart/form-data”或“application/x-www” -form-urlencoded”,并且在我的 Api 网关二进制支持中已经配置。我用谷歌搜索了它,但找不到用于解析标头 Content-type 的 node.js 库。下面是我正在使用的代码:
enter code here
const AWS = require('aws-sdk');
var s3 = new AWS.S3({apiVersion: '2006-03-01'});
const querystring = require('querystring');
AWS.config.region = 'xxx';
// "exports.handler" must match the entrypoint defined in the lambda Config.
exports.handler = function (event, context, callback) {
var bodyBuffer = new Buffer (String(event ['body-json']),'base64');
const params1 = querystring.parse(event.body);
var param = {Bucket:'mybucket', Key:'abc.csv', Body: bodyBuffer};
console.log("s3");
s3.upload(param, function(err, bodyBuffer) {
if (err) {
console.log(err, err.stack); // an error occurred
}
else console.log(bodyBuffer); // successful response
console.log('actually done!');
context.done();
});
const my_field1 = params1['my-field1'];
const html = `<!DOCTYPE html><p>You said: ` + my_field1 + `</p>`;
if(my_field1==''){
callback (null, "textfield1Notfilled");
}
callback(null, html);
}
(现在这段代码将文件上传到 S3 存储桶并在 html 页面上显示表单数据) 请帮忙!!
【问题讨论】:
标签: node.js api lambda multipartform-data content-type