简体   繁体   English

如何使用 node.js 使用 aws Lambda 发布文件

[英]How to post file using aws Lambda using node.js

I need to read s3 file and send it to thirdparty api using Node.js V12 Labmda.我需要读取 s3 文件并使用 Node.js V12 Labmda 将其发送到第三方 api。

I am able to read the s3 file from bucket.我能够从存储桶中读取 s3 文件。 But I have problem uploading that file to thirdparty url using https request or post method.但是我在使用 https 请求或 post 方法将该文件上传到第三方 url 时遇到问题。 I tried several methods but couldn't succeeded.我尝试了几种方法,但都没有成功。

Here is my lambda sample code.这是我的 lambda 示例代码。 Can you please review and guide me how can I resolve this issue.您能否查看并指导我如何解决此问题。

const AWS = require('aws-sdk');
const http = require('https');
const querystring = require('querystring');

exports.handler = async function(event, context, callback) {
    const params = {
        Bucket: "bucket",
        Key: "filename"
    };
    var content = await s3.getObject(params).promise();
    await uploadFile("url", callback, content);
};

const uploadFile = async (url, callback, content) => {
    return new Promise((resolve, reject) => {

        var requestData = {};
        requestData['file'] = content;

        const postData = querystring.stringify(requestData);

        const options = {
            hostname: 'xxx.xxx',
            path: url,
            method: 'POST',
            headers: {
                'Content-Type': "multipart/form-data",
                'Content-Length': Buffer.byteLength(postData)
            }
        };

        const req = http.request(options, (res) => {

            res.on('data', (chunk) => {
                //do something
            });
            res.on('end', () => {
                // do something
            });
        })
        req.write(postData);
        req.end();

    });
}

I can see an example with requests module, maybe it could help:我可以看到一个带有requests模块的示例,也许它可以帮助:

const AWS = require('aws-sdk');
const http = require('https');
const querystring = require('querystring');
let request = require('request')

exports.handler = async function(event, context, callback) {
    const params = {
        Bucket: "bucket",
        Key: "filename"
    };
    let readStream = s3.getObject(params).createReadStream()
    await uploadFile("url", callback, readStream);
};

const uploadFile = async (url, callback, readStream) => {
    return new Promise((resolve, reject) => {

        var requestData = {};

        const postData = querystring.stringify(requestData);
        
        let formData = {
          applicationType: 'my_app_type',
          applicationName: 'my_app_name',
          upload: {
            value: readStream,
            options: {
              filename: 'my_file_name.zip', // adjust accordingly
              contentType: 'application/zip' // adjust accordingly
              knownLength: Buffer.byteLength(postData) // this is important
            }
          }
        }
        
        request.post({
          url: 'your_url',
          formData: formData
        }, function (error, response, body) {
          if (error) throw error
          console.log(body)
        });

    });
}

So, I kinda merged your snippet with this example, where the OP tries to PUT the content instead of POST ing it.所以,我还挺合并的片段这个例子中,在OP试图PUT的内容,而不是POST荷兰国际集团它。

Please note , however, that the code above is untested.但是请注意,上面的代码未经测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM