简体   繁体   English

使用aws lambda将批处理数据从aws sqs存储到aws s3

[英]store batch data from aws sqs to aws s3 using aws lambda

I'm trying to get polled messages (iot data) in aws sqs from aws lambda (nodejs runtime) and trying to store it in aws s3 bucket.我正在尝试从 aws lambda(nodejs 运行时)获取 aws sqs 中的轮询消息(iot 数据),并尝试将其存储在 aws s3 存储桶中。 But I'm getting error in storing it.但是我在存储它时遇到错误。

Here is my payload:-这是我的有效载荷:-

{ "device_name": "xxx", "temp":23.44, "humidity":33.44, "timestamp": epoch timestamp }

Here is my code:-这是我的代码:-


import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3";
export const putInS3 = async (device_name,year,month,day) => {
        let bucketName = "my-bucket-name";
        const client = new S3Client({Bucket:bucketName,key:`${device_name}/${year}/${month}/${day}/`});
        try {
            const putObj = new PutObjectCommand(client);
            const sendRequest = await client.send(putObj);
            console.log(sendRequest);
        } catch (err) {
            if(err) console.log(err);
        }
}
export const handler = async(event) => {
    // TODO implement
    // console.log(s3Bucket);
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    event.Records.forEach(record => {
    let { body } = record;
    body = JSON.parse(body);
    let device_name = body.device_name;
    putInS3(device_name,year,month,day);
    console.log(body + "    ");
    });
};

The above code gives following error:上面的代码给出了以下错误:

Error: No value provided for input HTTP label: Bucket.

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks谢谢

You are providing an incorrect value to PutObjectCommand.您向 PutObjectCommand 提供的值不正确。 You are passing the client but you should be passing parameters.您正在传递客户端,但您应该传递参数。

This should work:这应该工作:

const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');

const s3Client = new S3Client({'region': 'us-east-1'});

export const putInS3 = async (device_name,year,month,day, body) => {
        let bucketName = "my-bucket-name";
        const parameters = {
         'Bucket': bucketName,
         'Key': `${device_name}/${year}/${month}/${day}/`,
         'Body': body
        }
        try {
            const putObj = new PutObjectCommand(parameters);
            const sendRequest = await s3Client.send(putObj);
            console.log(sendRequest);
        } catch (err) {
            if(err) console.log(err);
        }
}

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

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