【问题标题】:How to write a simple lambda function to upload a pdf in Amazon S3?如何编写一个简单的 lambda 函数在 Amazon S3 中上传 pdf?
【发布时间】:2017-02-16 13:39:41
【问题描述】:

我是 node.js 的新开发人员,我想编写一个小 lambda 函数(在 node.js 中),将 pdf 文件(从硬盘驱动器)上传到 Amazon S3 存储桶。

这是我的代码:

// dependencies
var util = require('util');
var fs = require('fs');

exports.handler = function (req, res) {
    var file = req.files.file;
    fs.readFile("‪C:\\Users\\zack\\Downloads\\test.pdf", function (err, data) {
        if (err) throw err; // Something went wrong!
        var s3bucket = new AWS.S3({params: {Bucket: 'myBucket3'}});
        s3bucket.createBucket(function () {
            var params = {
                Key: file.originalFilename, //file.name doesn't exist as a property
                Body: data
            };
            s3bucket.upload(params, function (err, data) {
                // Whether there is an error or not, delete the temp file
                fs.unlink("‪C:\\Users\\zack\\Downloads\\test.pdf", function (err) {
                    if (err) {
                        console.error(err);
                    }
                    console.log('Temp File Delete');
                });

                console.log("PRINT FILE:", file);
                if (err) {
                    console.log('ERROR MSG: ', err);
                    res.status(500).send(err);
                } else {
                    console.log('Successfully uploaded data');
                    res.status(200).end();
                }
            });
        });
    });
};

lambda 函数向我显示此错误消息:

 "errorMessage": "RequestId: 05024c81-f44b-11e6-a45e-57b13036ad96 Process exited before completing request"

还有云观察:

START RequestId: e66a7653-f44b-11e6-8b1a-c511605a533b Version: $LATEST
2017-02-16T13:29:11.133Z    e66a7653-f44b-11e6-8b1a-c511605a533b    TypeError: Cannot read property 'file' of undefined
    at exports.handler (/var/task/invoices3/invoices3.js:17:25)
END RequestId: e66a7653-f44b-11e6-8b1a-c511605a533b
REPORT RequestId: e66a7653-f44b-11e6-8b1a-c511605a533b  Duration: 27.82 ms  Billed Duration: 100 ms     Memory Size: 1024 MB    Max Memory Used: 23 MB  
RequestId: e66a7653-f44b-11e6-8b1a-c511605a533b Process exited before completing request

你能告诉我问题出在哪里吗?我一点也不擅长 node.js。

提前谢谢你!

【问题讨论】:

    标签: node.js amazon-web-services amazon-s3 lambda


    【解决方案1】:

    在完成请求之前进程退出

    您收到此消息是因为您从未通知 Lambda 该函数已完成。完成后,您需要调用 context.succeed() 函数,或者 update your code 使用较新的 callback() 方法,并在完成后调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 2018-01-08
      • 2020-04-02
      • 1970-01-01
      • 2012-12-28
      • 1970-01-01
      相关资源
      最近更新 更多