【问题标题】:Lambda function doesn't execute till the endLambda 函数直到最后才执行
【发布时间】:2020-07-18 18:10:37
【问题描述】:

我正在调用一个 lambda 函数来压缩上传到存储桶的图像,然后将压缩后的图像写入另一个存储桶

代码执行良好,它获取图像,但由于某种原因,它无法将压缩图像写入新存储桶

这是我的代码:


//Import compress module
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const sharp = require('sharp');

const AWS = require('aws-sdk')

const s3 = new AWS.S3()
exports.handler = async (event) =>{
    // TODO implement
    
    const srcBucket = event.Records[0].s3.bucket.name;
    
    const srcKey    = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
    const dstBucket = "safarni";
    const dstKey    =  srcKey;
    
    console.log(srcKey, dstBucket, dstKey)
     // Download the image from the S3 source bucket. 
    try {
        const params = {
            Bucket: srcBucket,
            Key: srcKey
        };
        var origimage = await s3.getObject(params).promise();

    } catch (error) {

        console.log('here');
        return;
    }  
    
    const jpgBuffer = await sharp(origimage.Body).jpeg().toBuffer()
    
    //Compressing the photo
    const compressedjpgBuffer = await imagemin.buffer(jpgBuffer, {
            plugins: [imageminMozjpeg({ quality: 85 })]
    })
       
    console.log(compressedjpgBuffer)    
    // Upload the thumbnail image to the destination bucket
    try {
        const destparams = {
            Bucket: dstBucket,
            Key: dstKey,
            Body: compressedjpgBuffer,    
        };

        const putResult = await s3.putObject(destparams).promise(); 
        
    } catch (error) {
        console.log(error);
        return;
    } 
        
    console.log('Successfully resized ' + srcBucket + '/' + srcKey +
        ' and uploaded to ' + dstBucket + '/' + dstKey); 
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

这是日志的图像

【问题讨论】:

  • 你的函数超时了吗,增加超时是否允许它完成?

标签: node.js amazon-web-services amazon-s3 aws-lambda


【解决方案1】:

您提供的此日志中的功能正在超时。执行一个(或两个以下选项)

  • 将超时时间增加到超过 3 秒(这是默认值)。可以是increased upto 15 minutes,您应该将超时设置为与执行操作所需的预期时间相匹配。
  • 增加(内存/CPU 分配)。

通过单击基本设置部分上方的Edit,可以在控制台中更改其中任何一个,然后修改为适当的值,如下所示。

一旦超时,功能将无法继续。

【讨论】:

    【解决方案2】:

    尝试像这样更改您的代码:

    const imagemin = require('imagemin');
    const imageminMozjpeg = require('imagemin-mozjpeg');
    const sharp = require('sharp');
    const AWS = require("aws-sdk");
    
    const s3 = new AWS.S3();
    
    exports.handler = async (event) => {
      const s3MetaData = event.Records[0].s3;
      const srcBucket = s3MetaData.bucket.name;
      const srcKey = s3MetaData.object.key;
    
      const dstBucket = "safarni";
      const dstKey = `compressed_${srcKey}`;
    
      try {
        const srcParams = {
          Bucket: srcBucket,
          Key: srcKey
        };
    
        const origImage = await s3.getObject(srcParams).promise();
    
        const jpgBuffer = await sharp(origImage.Body).jpeg().toBuffer();
    
        const compressedJPGBuffer = await imagemin.buffer(jpgBuffer, 
          { plugins: [imageminMozjpeg({ quality: 85 })] });
    
        const destParams = {
          Bucket: dstBucket,
          Key: dstKey,
          Body: compressedJPGBuffer
        }
    
        const newImage = await s3.putObject(destParams).promise();
    
        return {
          statusCode: 200,
          body: "Lambda successfully executed"
        }
    
      } catch (err) {
        console.error(`The following error occurred: ${err.message}`);
        throw err;
      }
    }
    

    确保您已应用以下政策:

    - Action:
      - s3:putObject
      - s3:getObject
    Effect: Allow
    Resource:
      Fn::Join:
        - ""
        - - "arn:aws:s3:::"
          - Ref: YourBucket
            - /*
    

    并且还增加超时,因为显然它在 3 秒后超时。

    【讨论】:

    • 我增加了超时,它解决了这个问题,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多