简体   繁体   English

如何从 Lambda function 访问 S3 object?

[英]How do I access an S3 object from a Lambda function?

I am attempting to access a text file stored in an AWS S3 bucket.我正在尝试访问存储在 AWS S3 存储桶中的文本文件。 At present, it contains only the word "Test".目前,它只包含“测试”一词。

At first, I thought I was having problems with fs.readfile, but now I've discovered that the problem is more fundamental.起初,我以为我的 fs.readfile 有问题,但现在我发现问题更根本。 I cannot access the file at all.我根本无法访问该文件。 AWS from Node.js does not appear to be able to see the file at all.来自 Node.js 的 AWS 似乎根本无法看到该文件。

I am using the following Lambda function:我正在使用以下 Lambda function:

const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });

    exports.handler = async (event, context, callback) => { 
    // Get the object from the event and show its content type
    const bucket = event.Records[0].s3.bucket.name;
    const key = event.Records[0].s3.object.key; // decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' '));
    console.log('Bucket = ' + bucket);
    console.log('key = ' + key);

    var params = {Bucket: bucket, Key: key};
    console.log('Checking file existence');
    console.log(params);
    console.log('Calling s3.getObject');
    s3.getObject(params, function(err, data) { 
        console.log('S3.getObject called');
        console.log('err = ' + err);
        if (err) {
            console.log(err, err.stack); // an error occurred
            callback(err);
        } else {
            console.log(data);           // successful response
            callback(null, null);
        }
        console.log('Leaving s3.getObject');
    });
};

The testbed function contains the following code:测试台 function 包含以下代码:

{
  "Records": [
    {
      "eventVersion": "2.0",
      "eventSource": "aws:s3",
      "awsRegion": "us-east-1",
      "eventTime": "1970-01-01T00:00:00.000Z",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "EXAMPLE"
      },
      "requestParameters": {
        "sourceIPAddress": "127.0.0.1"
      },
      "responseElements": {
        "x-amz-request-id": "EXAMPLE123456789",
        "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"
      },
      "s3": {
        "s3SchemaVersion": "1.0",
        "configurationId": "testConfigRule",
        "bucket": {
          "name": "wgtiplists",
          "ownerIdentity": {
            "principalId": "EXAMPLE"
          },
          "arn": "arn:aws:s3:::wgtiplists"
        },
        "object": {
          "key": "tiplist.txt",
          "size": 1024,
          "eTag": "0123456789abcdef0123456789abcdef",
          "sequencer": "0A1B2C3D4E5F678901"
        }
      }
    }
  ]
}

And the results look like this (I've trimmed off the timestamp and Request ID texts):结果看起来像这样(我已经剪掉了时间戳和请求 ID 文本):

INFO    Bucket = s3://wgtiplists
INFO    key = tiplist.txt
INFO    Checking file existence
INFO    { Bucket: 's3://wgtiplists', Key: 'tiplist.txt' }
INFO    Calling s3.getObject

From this, I conclude that the S3 function is not being called, although I might be mistaken.由此,我得出结论,没有调用 S3 function,尽管我可能弄错了。

What am I doing wrong?我究竟做错了什么?

I believe you need parenthesis here:我相信你在这里需要括号:

const s3 = new aws.S3();

You should not be adding s3:// to the bucket name, just do this:您不应该将s3://添加到存储桶名称中,只需执行以下操作:

const bucket = event.Records[0].s3.bucket.name;

Look for the Cloudwatch logs.查找 Cloudwatch 日志。 You might get some insight from there.你可能会从那里得到一些见解。 Probably, as @Marck B mentioned, should be given bucket name is wrong可能正如@Marck B 提到的那样,应该给出存储桶名称是错误的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Node.js或Lambda函数在s3中为存储桶创建事件 - How do I create events for the buckets in s3 using nodejs or from lambda function 如何从Node中的Lambda函数将本地图像保存到S3 - How do I save a local image to S3 from my Lambda function in Node 访问被拒绝错误 500 从 Lambda function 中删除 S3 存储桶中的 Object 使用 NodeJS 和 Z48C061E29CEAED2 - Access Denied Error 500 Deleting Object in S3 Bucket from Lambda function using NodeJS and Postman 如何从 node.js 中的 s3 获取 HEAD 对象? (在 aws lambda 中运行) - How do you get the HEAD object from an s3 in node.js? (running in an aws lambda) 如何使用 Node 从 SAM Lambda function 在 S3 中写入 json 文件 - How can I write a json file in S3 from a SAM Lambda function with Node 如何将 .wav 从 lambda 函数中的 URL 保存到 s3 存储桶 - How to save .wav to s3 bucket from URL in lambda function 如何从Lambda函数解析AWS S3文件 - How to parse an AWS S3 file from a Lambda function 当S3存储桶中的getObject时,对AWS Lambda函数的访问被拒绝 - Access denied on aws lambda function when getObject from S3 bucket 从AWS Lambda函数中的S3获取对象并发送到Api Gateway - Get object from S3 in AWS Lambda function and send to Api Gateway 无法从 Lambda 中删除 S3 Object - Nodejs - Unable to delete S3 Object from Lambda - Nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM