简体   繁体   English

使用node.js将JSON从AWS Lambda保存到AWS S3

[英]Save a JSON from AWS Lambda to AWS S3 with node.js

I am trying to save a JSON File from AWS Lambda to S3. 我试图将JSON文件从AWS Lambda保存到S3。 (to be more precise: I want to create a new file 'supertest.json' containing the 'data' inside the S3 bucket 'gpiocontroll-XYZ' ) (更精确地说:我想在S3存储桶'gpiocontroll-XYZ'创建一个包含“数据”的新文件'supertest.json' 'gpiocontroll-XYZ'

The Lambda function looks like this: Lambda函数如下所示:

'use strict'

const aws = require('aws-sdk');
const s3 = new aws.S3();

//const fs = require('fs');

function saveJSONtoS3(data){
    console.log('SAVEJSON', data);

    var params = {
        Bucket: 'gpiocontroll-XYZ', // your bucket name,
        Key: 'test.txt', // path to the object you're looking for
        Body: data
    }

    s3.putObject(params, function(err, data) {
        // Handle any error and exit
        if (err)
        console.log('ERROR', err);
        else {
            console.log('UPLOADED SUCCESS');
        }
        console.log('INSIDE FUNCTION');
    });

    console.log('END')
}

module.exports = {
    saveJSONtoS3 : saveJSONtoS3
}

The log on Lambda looks like: Lambda上的日志如下所示:

2017-12-27T20:04:29.382Z    255d436d-eb41-11e7-b237-1190c4f33d2d    SAVEJSON {"table":[{"pin":"1","state":"aus"}]}
2017-12-27T20:04:29.402Z    255d436d-eb41-11e7-b237-1190c4f33d2d    END
END RequestId: 255d436d-eb41-11e7-b237-1190c4f33d2d
REPORT RequestId: 255d436d-eb41-11e7-b237-1190c4f33d2d  Duration: 362.29 ms Billed Duration: 400 ms     Memory Size: 128 MB Max Memory Used: 43 MB  

So it seems like everything is fine but the s3.putObject function just don't get triggered. 所以看起来一切都很好,但是s3.putObject函数只是不会被触发。 Lambda and S3 are both in the same region. Lambda和S3都在同一区域。 The S3 is public with an IAM user. S3对IAM用户公开。 Do I need to log in in the Lambda function somehow? 我是否需要以某种方式登录Lambda函数?

Thanks a lot! 非常感谢!

As @dashmug said, your example is not a Lambda function. 正如@dashmug所说,您的示例不是 Lambda函数。

You must have exports.handler in your file somewhere unless specified in the function configuration. 除非在功能配置中指定,否则文件中的某个位置必须具有exports.handler

All Lambda functions start by calling exports.handler with ( event, context, callback ) parameters. 所有Lambda函数exports.handler带有( event, context, callback )参数的exports.handler开头。 These include the data of the event or action, some additional context , and a success/fail callback . 其中包括event或操作的数据,一些其他context以及成功/失败callback

Here is what you are looking for: 这是您要寻找的:

Update: changed S3.putObject Promise wrapped function to S3.putObject().promise() per @dashmug's recommendation. 更新:根据@dashmug的建议,将S3.putObject Promise包装函数更改为S3.putObject().promise()

Requires AWS SDK for JavaScript (v2.3.0 - March 31 2016 or later) 需要适用于JavaScript的AWS开发工具包(v2.3.0-2016年3月31日或更高版本)

'use strict';

const
    AWS = require( 'aws-sdk' ),
    S3  = new AWS.S3();

exports.handler = ( event, context, callback ) => {
    console.log( `FUNCTION STARTED: ${new Date()}` );

    S3.putObject( {
         Bucket: 'gpiocontroll-XYZ',
         Key: 'test.txt',
         Body: 'stuff'
    } )
         .promise()
         .then( () => console.log( 'UPLOAD SUCCESS' ) )
         .then( () => callback( null, 'MISSION SUCCESS' ) )
         .catch( e => {
            console.error( 'ERROR', e );
            callback( e );
         } );
};

Note: you must give the Lambda function IAM permissions to the S3 Bucket you are trying to access. 注意:必须为Lambda函数IAM授予您尝试访问的S3存储桶的权限。 In the case above, your IAM role should look something like this: 在上述情况下,您的IAM角色应如下所示:

{
    "Effect": "Allow",
    "Action": [ "s3:PutObject" ],
    "Resource": [
        "arn:aws:s3:::gpiocontroll-XYZ/*"
    ]
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM