AWS Lambda函数对外部API的POST请求,URI太长错误

时间:2015-09-21 15:33:55

标签: node.js http post amazon-web-services aws-lambda

使用AWS API Gateway和Lambda函数,我尝试发出一个POST请求,将JSON数据传递到外部API并返回一个二进制文档,我将存储到S3。

目前我能够发出请求并存储文档但是文档的内容(在我从二进制文件转换后)是HTML标记和414错误, 请求URI太大,而不是使用JSON数据的预期模板。

我猜测错误是返回的数据是在url字符串中传递而不是在post请求中传递,但是我无法找到问题,我们将不胜感激。

lambda函数:

var util = require('util');
var https = require('https');
var AWS = require('aws-sdk');

var s3 = new AWS.S3();

exports.handler = function(event, context) {
    console.log('Received event:', JSON.stringify(event, null, 2));

    var operation = event.operation;
    delete event.operation;

    var accessKey = event.accessKey;
    delete event.accessKey;

    var templateName = event.templateName;
    delete event.templateName;

    var outputName = event.outputName;
    delete event.outputName;

    var req = {
        "accessKey": accessKey,
        "templateName": templateName,
        "outputName": outputName,
        "data": event.data
    };

    var body = '';

    function doPost(data, callback) {
        // Build the post string from an object
        var post_data = JSON.stringify(data);

        // An object of options to indicate where to post to
        var post_options = {
            host: 'some.thing.com',
            port: '443',
            path: '/path/to/render',
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Content-Length': Buffer.byteLength(post_data)
            }
        };

        // Set up the request
        var post_req = https.request(post_options, function(res) {
            console.log('STATUS: ' + res.statusCode);
            console.log('HEADERS: ' + JSON.stringify(res.headers));

            res.on('data', function(chunk)  {
                console.log('data response:' + chunk);
                body += chunk;
            });

            res.on('error', function(e) {
                context.fail('error:' + e.message);
            });

            res.on('end', function() {
                console.log('End of request');
                callback();
            });
        });

        // post the data
        post_req.write(post_data);
        post_req.end();
        console.log('REQUEST INSPECT:' + util.inspect(post_req));
    }

    function upload() {
        var bucketName = 'bucketName';
        var keyName = 'docName.doc';
        var params = {Bucket: bucketName, Key: keyName, Body: body, ContentType: 'binary'};

        s3.putObject(params, function(err, data) {
            if (err) {
                console.log(err);
            }else {
                context.succeed('upload success');
            }
        });
     }

    switch (operation) {
        case 'create':
            // Make sure there's data before we post it
            if(req) {
                doPost(req, upload);
            }
            else {
                context.fail('No data to post');
            }
            break;
        default:
            context.fail(new Error('Unrecognized operation "' + operation + '"'));
    }
};

0 个答案:

没有答案