【问题标题】:How to parse the Content-type of the data being uploaded through aws Api gateway in aws Lambda (node.js)?如何在aws Lambda(node.js)中解析通过aws Api网关上传的数据的Content-type?
【发布时间】: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


    【解决方案1】:

    您可以尝试使用event.headers 获取所有请求标头,然后使用event.headers['Content-Type']。

    【讨论】:

      猜你喜欢
      • 2018-08-28
      • 1970-01-01
      • 2021-09-06
      • 2018-01-15
      • 2021-03-16
      • 2014-03-20
      相关资源
      最近更新 更多