使用Node.js版本8.10在Lambda中加载S3文件
我一直在尝试(但失败)使用节点版本8.10中的S3.getObject从S3存储桶加载文件 我发现了一篇很好的帖子,上面有一个回复,几乎可以正常工作,但是语法在8.10中不太有效,无论我如何重新安排代码,我都无法让它正常工作使用Node.js版本8.10在Lambda中加载S3文件,node.js,amazon-s3,aws-lambda,Node.js,Amazon S3,Aws Lambda,我一直在尝试(但失败)使用节点版本8.10中的S3.getObject从S3存储桶加载文件 我发现了一篇很好的帖子,上面有一个回复,几乎可以正常工作,但是语法在8.10中不太有效,无论我如何重新安排代码,我都无法让它正常工作 var AWS = require('aws-sdk') var s3 = new AWS.S3(); var fileData = null; exports.handler = (event, context, callback) => { conso
var AWS = require('aws-sdk')
var s3 = new AWS.S3();
var fileData = null;
exports.handler = (event, context, callback) => {
console.log('I am in the main procedure');
var params = {
Bucket: "change_for_your_bucket",
Key: "change_to_your_json_file"
};
fetchDataFromS3(params);
console.log('I am in the main procedure, the function above should be waiting but it is not');
waitForFileLoadBeforeDoingSomething(event, context, callback);
const s = JSON.stringify(fileData.Body.toString('utf-8'));
console.log(`this is the file: ${s}`);
console.log('I have the file!(dg.2)');
};
function fetchDataFromS3(params)
{
console.log('-------------- fetchDataFromS3:Start -------------------------');
// gets the object from s3 => promise
const uploadPromise = s3.getObject(params).promise();
// returns the body of the s3 object
uploadPromise
.then(function(data) {
console.log("successfully downloaded data");
fileData = data.Body.toString();
})
.catch(function download(err) {console.log(err,err.stack); throw err;});
console.log('-------------- fetchDataFromS3:Done -------------------------');
}
function waitForFileLoadBeforeDoingSomething(event, context, callback){
if(!fileData){
console.log('No file available to me as yet, lets sleep for a bit');
setTimeout(function(){
waitForFileLoadBeforeDoingSomething(event, context, callback);
}, 300);
}
}
输出如下
Function Logs:
START RequestId: cb16f155-c0d7-11e8-ad01-f5991c5adaaf Version: $LATEST
2018-09-25T15:29:29.759Z cb16f155-c0d7-11e8-ad01-f5991c5adaaf I am in the main procedure
2018-09-25T15:29:29.759Z cb16f155-c0d7-11e8-ad01-f5991c5adaaf -------------- fetchDataFromS3:Start -------------------------
2018-09-25T15:29:29.811Z cb16f155-c0d7-11e8-ad01-f5991c5adaaf -------------- fetchDataFromS3:Done -------------------------
2018-09-25T15:29:29.811Z cb16f155-c0d7-11e8-ad01-f5991c5adaaf I am in the main procedure, the function above should be waiting but it is not
2018-09-25T15:29:29.811Z cb16f155-c0d7-11e8-ad01-f5991c5adaaf No file available to me as yet, lets sleep for a bit
2018-09-25T15:29:29.812Z cb16f155-c0d7-11e8-ad01-f5991c5adaaf TypeError: Cannot read property 'Body' of null
at exports.handler (/var/task/dg3.js:17:39)
您可以看到,我没有点击“成功下载数据”行,我无法确定我是否犯了错误,函数是否仍在异步运行,或者承诺的语法是否有误。首先,您必须更改入口点方法。正如您在尝试使用8.10节点运行时之前提到的,下面是代码的一部分:
exports.handler = (event, context, callback) => {}
function waitForFileLoadBeforeDoingSomething(event, context, callback){
if(!fileData){
console.log('No file available to me as yet, lets sleep for a bit');
setTimeout(function(){
waitForFileLoadBeforeDoingSomething(event, context, callback);
}, 300);
}
}
您必须更改为:
export async function <function_name>(event) {}
然后去掉var声明。不要弄乱范围。只需使用:
const AWS = require('aws-sdk');
下一步是创建一个S3实例:
const S3 = new AWS.S3({region: process.env.AWS_REGION, apiVersion: '2006-03-01'});
// with region of your AWS account and current API verstion;
为获取方法声明参数:
const params =
{
Bucket: 'STRING_VALUE', // a path to your Bucket
Key: 'STRING_VALUE' // a key (literally a path to your file)
}
参考:
您不必将事件字符串化,因为它已经字符串化了:
const s = JSON.stringify(fileData.Body.toString('utf-8'));
最后:
try
{
const result = await S3.getObject(params).promise();
// if successful then:
console.log(`Check the result: ${result}`);
}
catch (ex) // if an error occured
{
console.error(ex);
}
另外,确保运行时间为5分钟(仅在您可以调整后用于调试目的)并增加lambda的内存(也用于测试目的)。首先,您必须更改入口点方法。正如您在尝试使用8.10节点运行时之前提到的,下面是代码的一部分:
exports.handler = (event, context, callback) => {}
function waitForFileLoadBeforeDoingSomething(event, context, callback){
if(!fileData){
console.log('No file available to me as yet, lets sleep for a bit');
setTimeout(function(){
waitForFileLoadBeforeDoingSomething(event, context, callback);
}, 300);
}
}
您必须更改为:
export async function <function_name>(event) {}
然后去掉var声明。不要弄乱范围。只需使用:
const AWS = require('aws-sdk');
下一步是创建一个S3实例:
const S3 = new AWS.S3({region: process.env.AWS_REGION, apiVersion: '2006-03-01'});
// with region of your AWS account and current API verstion;
为获取方法声明参数:
const params =
{
Bucket: 'STRING_VALUE', // a path to your Bucket
Key: 'STRING_VALUE' // a key (literally a path to your file)
}
参考:
您不必将事件字符串化,因为它已经字符串化了:
const s = JSON.stringify(fileData.Body.toString('utf-8'));
最后:
try
{
const result = await S3.getObject(params).promise();
// if successful then:
console.log(`Check the result: ${result}`);
}
catch (ex) // if an error occured
{
console.error(ex);
}
另外,请确保运行时间为5分钟(只有在您可以调整后才用于调试目的),并增加lambda的内存(也用于测试目的)。感谢您的良好响应,如果真的有助于我的理解,我在理解之前又一次陷入了将S3 getobject调用分离为单独函数的困境,我不得不稍微更改事件处理程序声明以使其工作(我不知道为什么)。我将发布我创建的代码的更新版本。由于我无法在注释中发布长代码段,这里有一个指向代码工作版本的链接。感谢您的回复,如果真的有助于我的理解的话,在我弄明白之前,我又一次陷入了将S3 getobject调用分离为一个单独函数的困境,我不得不稍微更改事件处理程序声明以使其工作(我不知道为什么)。我将发布我创建的代码的更新版本。由于我无法在注释中发布长代码段,这里有一个指向代码工作版本的链接。