如何写入AWS Lambda实例的文件系统? [英] How do you write to the file system of an aws lambda instance?

查看:187
本文介绍了如何写入AWS Lambda实例的文件系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试写入aws lambda实例的文件系统失败. 文档说,标准的lambda实例具有512mb的可用空间在/tmp/.但是,在本地计算机上运行的以下代码在lambda实例上根本无法运行:

I am unsuccessfully trying to write to the file system of an aws lambda instance. The docs say that 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:

  var fs = require('fs');
  fs.writeFile("/tmp/test.txt", "testing", function(err) {
      if(err) {
          return console.log(err);
      }
      console.log("The file was saved!");
  });

永远不会在lambda实例上调用匿名回调函数中的代码.有人成功做到了吗?非常感谢您的帮助.

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.

这可能是相关的问题. s3代码与我尝试使用fs回调函数进行处理之间是否可能发生某种冲突?下面的代码是当前正在运行的代码.

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.

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");
                }
            });
        }
    });
};

推荐答案

所以答案就在context.fail()context.succeed()函数中.在aws和lambda领域是一个全新的人,我不知道调用这些方法中的任何一个

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.succeed()方法表示成功执行并返回 一个字符串.

The context.succeed() method signals successful execution and returns a string.

通过消除这些并且仅在我运行了所有想要的代码后才调用它们,一切正常.

By eliminating these and only calling them after I had run all the code that I wanted, everything worked well.

这篇关于如何写入AWS Lambda实例的文件系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆