Javascript 您如何写入 aws lambda 实例的文件系统?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35006874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you write to the file system of an aws lambda instance?
提问by Rymnel
I am unsuccessfully trying to write to the file system of an aws lambda instance. The docs saythat a standard lambda instance has 512mb of space available at /tmp/
. However the following code that runs on my local machine isn't working at all on the lambda instance:
我尝试写入 aws lambda 实例的文件系统失败。 文档说标准的 lambda 实例在/tmp/
. 但是,在我的本地机器上运行的以下代码在 lambda 实例上根本不起作用:
var fs = require('fs');
fs.writeFile("/tmp/test.txt", "testing", function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
The code in the anonymous callback function is never getting called on the lambda instance. Anyone had any success doing this? Thanks so much for your help.
匿名回调函数中的代码永远不会在 lambda 实例上被调用。任何人有任何成功做到这一点?非常感谢你的帮助。
It's possible that this is a related question. Is it possible that there is some kind of conflict going on between the s3 code and what I'm trying to do with the fs callback function? The code below is what's currently being run.
这可能是一个相关的问题。s3 代码和我试图用 fs 回调函数做的事情之间是否可能存在某种冲突?下面的代码是当前正在运行的代码。
console.log('Loading function');
var aws = require('aws-sdk');
var s3 = new aws.S3({ apiVersion: '2006-03-01' });
var fs = require('fs');
exports.handler = function(event, context) {
//console.log('Received event:', JSON.stringify(event, null, 2));
// Get the object from the event and show its content type
var bucket = event.Records[0].s3.bucket.name;
var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
var params = {
Bucket: bucket,
Key: key
};
s3.getObject(params, function(err, data) {
if (err) {
console.log(err);
var message = "Error getting object " + key + " from bucket " + bucket +
". Make sure they exist and your bucket is in the same region as this function.";
console.log(message);
context.fail(message);
} else {
//console.log("DATA: " + data.Body.toString());
fs.writeFile("/tmp/test.csv", "testing", function (err) {
if(err) {
context.failed("writeToTmp Failed " + err);
} else {
context.succeed("writeFile succeeded");
}
});
}
});
};
采纳答案by Rymnel
So the answer lies in the context.fail()
or context.succeed()
functions. Being completely new to the world of aws and lambda I was ignorant to the fact that calling any of these methods stops execution of the lambda instance.
所以答案在于context.fail()
orcontext.succeed()
函数。作为 aws 和 lambda 世界的新手,我不知道调用这些方法中的任何一个都会停止执行 lambda 实例。
According to the docs:
根据文档:
The context.succeed() method signals successful execution and returns a string.
context.succeed() 方法表示成功执行并返回一个字符串。
By eliminating these and only calling them after I had run all the code that I wanted, everything worked well.
通过消除这些并仅在我运行了我想要的所有代码后才调用它们,一切都运行良好。
回答by James
Modifying your code into the Lambda template worked for me. I think you need to assign a function to exports.handler
and call the appropriate context.succeed()
or context.fail()
method. Otherwise, you just get generic errors.
将您的代码修改为 Lambda 模板对我有用。我认为您需要分配一个函数exports.handler
并调用适当的context.succeed()
orcontext.fail()
方法。否则,您只会收到一般错误。
var fs = require("fs");
exports.handler = function(event, context) {
fs.writeFile("/tmp/test.txt", "testing", function (err) {
if (err) {
context.fail("writeFile failed: " + err);
} else {
context.succeed("writeFile succeeded");
}
});
};