2017-02-16 19 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()方法,並在完成後調用該函數。