2017-02-16 21 views
-1

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

这里是我的代码:

// 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" 

而且CloudWatch的:

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。

预先感谢您!

回答

0

过程完成请求

您收到此消息,因为你永远通知拉姆达该功能已完成之前退出。您需要在完成后调用context.succeed()函数,或者update your code使用更新的callback()方法,并在完成后调用该函数。