简体   繁体   English

S3 putObject 没有使用 nodejs 将我的文件从 EFS 上传到 S3 Lambda

[英]S3 putObject isn't uploading my file to S3 from EFS using nodejs Lambda

I have a use case where I have to move a zip file from EFS to S3 using NodeJs Lambda function. I am ready data using fs.createReadStream.我有一个用例,我必须使用 NodeJs Lambda function 将 zip 文件从 EFS 移动到 S3。我使用 fs.createReadStream 准备好数据。 It is creating a zip file on EFS and when I try to move that file to S3 using putObjectCommand it doesn't upload that file to S3.它正在 EFS 上创建一个 zip 文件,当我尝试使用 putObjectCommand 将该文件移动到 S3 时,它不会将该文件上传到 S3。 The weird thing is that it doesn't even throw any error.奇怪的是它甚至没有抛出任何错误。 Here is my nodejs Lambda code:这是我的 nodejs Lambda 代码:

export const lambdaHandler = async (event, context) => {
    try {

        var efsDirectory = "/mnt/data/";
        //var zipFile = '/mnt/data/zipped.zip'

        fs.readdirSync(efsDirectory).forEach(async file => {
            var filePath = path.join(efsDirectory, file);
            console.log("My file is :", file);
            console.log("upper file path is :", filePath);

            var response = fs.statSync(filePath);
            console.log(response.birthtime)

            let fileBirthTime = response.birthtime;
            console.log(fileBirthTime);

            let currentTime = new Date();
            console.log("value of current time :", currentTime);


            let timeDifference = currentTime - fileBirthTime;
            console.log("Time difference is :", timeDifference);

            let timeDifferenceInMin = ((timeDifference / 1000) / 60);
            console.log(timeDifferenceInMin);
            if (timeDifferenceInMin >= 1) {
                console.log("File created time is greater than or euqal to 15 mins");


                var filePathWithouExt = path.parse(file).name;

                var zipFile = path.join(efsDirectory, filePathWithouExt);
                console.log("zip file path is this :", zipFile);
                var result = ZipLocal.sync.zip(efsDirectory).compress().save(`${zipFile}.zip`);
                console.log("files zipped");


                var fileStream = fs.createReadStream(`${zipFile}.zip`);
                fileStream.on('error', function (error) {
                    console.log(`error: ${error.message}`);
                })
                // console.log("EFS stream data :", fileStream)

                var s3Key = path.basename(`${zipFile}.zip`);
                console.log("s3key value is :", s3Key);

                let bucketName = "matomo-lambda-test-bucket";
                const params = {
                    Bucket: bucketName,
                    Key: `${s3Key}.zip`,
                    Body: fileStream
                };
                console.log("Params are :", params)
                await s3Client.send(
                    new PutObjectCommand(params)
                ).then(res=>{
                    console.log("res", res)
                }).catch(err=>{
                console.log("err",err)
                })
                // console.log("Upload Completed", s3Data);

                var bucketUrl = `https://${bucketName}.s3.us-west-2.amazonaws.com/${s3Key}`;
                console.log(bucketUrl);

                fs.unlinkSync(efsDirectory + file);
                console.log("deleted file :", file);

                fs.readdirSync(efsDirectory).forEach(file => {
                    console.log("Reading file :", file);
                });

                return {
                    'statusCode': 200,
                    'body': JSON.stringify({
                        message: 'hello world lambda',
                    })
                }

            }
            else {
                console.log("File created time is less than 15 min.");
            }



        });



    }
    catch (err) {
        console.log(err);
        return err;
    }
};

For each iteration it just zip the file but doesn't trigger s3 putobjectcommand.对于每次迭代,它只是 zip 文件,但不会触发 s3 putobject 命令。

You are mixing callbacks code with async/await code, which is very unmaintainable way of coding.您将回调代码与异步/等待代码混合在一起,这是一种非常难以维护的编码方式。

The main reason that putObject is not working is: putObject 不起作用的主要原因是:

console.log("Params are :", params)
            await s3Client.send(
                new PutObjectCommand(params)
            ).then(res=>{
                console.log("res", res)
            }).catch(err=>{
            console.log("err",err)
            })

Since this part, will never be called:由于这部分,永远不会被称为:

new PutObjectCommand(params)

Since the result of await s3Client.send is not a promise由于await s3Client.send的结果不是 promise

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

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