gpt4 book ai didi

node.js - 从 AWS lambda handler.js 调用文件?

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:31 24 4
gpt4 key购买 nike

我有node.js 文件(即abc.js),当我在node.js 编辑器中运行时,它将给出输出。我想在 AWS Lambda 中运行相同的文件。为此,我创建了一个 lambda 并将 abc.js 移动到那里。要运行,我似乎需要在 handler.js 中实现我的 abc.js 文件(即以 lambda 方式意味着回调等)。

有什么方法可以从 handler.js 触发 abc.js 而不是在 handler.js 中再次实现相同的功能?

检查了上述用例,但在谷歌上没有找到太多信息。

已更新

我的 abc.js 文件

var AWS = require('aws-sdk');
// Set the region
AWS.config.update({
region: "ap-south-1"
});

// Create S3 service object
s3 = new AWS.S3();
var params= {};
s3.listBuckets(params, bucketList);
function bucketList(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else
{
console.log(data)
}
}

我的 lambda 中的 handler.js 并根据我对您答案的解释对其进行修改。

exports.handler = async (event) => {
const abc = require('./abc.js');
// TODO implement
abc.bucketList();
};

这是我遇到的错误

Response:
{
"errorMessage": "abc.bucketList is not a function",
"errorType": "TypeError",
"stackTrace": [
"exports.handler (/var/task/index.js:5:5)"
]
}

感谢任何帮助。

最佳答案

需要您的文件

const abc = require('./abc.js');

并在处理函数中调用您的代码

abc.yourExportedMethod();

关于node.js - 从 AWS lambda handler.js 调用文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52318090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com