node.js - Jimp 没有从 aws 的 lambda 触发器中的 url 读取

标签 node.js amazon-web-services jimp

从 aws cloudwatch 登录

20:42:36
START RequestId: 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b Version: $LATEST
20:42:39
END RequestId: 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b
20:42:39
REPORT RequestId: 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b Duration: 3003.18 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 128 MB Init Duration: 703.55 ms
REPORT RequestId: 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b  Duration: 3003.18 ms    Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 128 MB Init Duration: 703.55 ms    
20:42:39
2020-01-06T15:12:39.657Z 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b Task timed out after 3.00 seconds
2020-01-06T15:12:39.657Z 3b39ddb6-f2f5-4e11-a3d6-59f47f16240b Task timed out after 3.00 seconds

Node.js AWS Lambda 代码

const aws = require('aws-sdk');
const Jimp = require("jimp");
const uuid = require("uuid/v4");
const s3 = new aws.S3();
//lambda trigger handler for triggering event after object being uploaded into bucket
exports.handler = async (event, context) => {
  const key = event.Records[0].s3.object.key; // Uploaded object key
  const sanitizedKey = key.replace(/\+/g, ' ');
  const keyWithoutExtension = sanitizedKey.replace(/.[^.]+$/, '');
  const objectKey = keyWithoutExtension+'_mb.';

//read object using jimp to resize it accordingly
  const image = await Jimp.read(prefix+key)
                      .then((image) => {
                        console.log( "Before resizing" , image)
                        return image
                          .resize(256, 256) // resize
                          .quality(90) // set JPEG quality
                      })
                     .then((image) => {
                      return uploadToS3(image, objectKey+image.getExtension(), image.getExtension());
                      })
                      .catch(err => {
                        throw err;
                      })
                      .finally(() => {
                        console.info("Function ran successfully")
                      })
  console.log(image);
  return image
}
//upload file to s3 after resizing
async function uploadToS3(data, key, ContentType) {
  console.log("Inside uploadToS3: ", data, key, ContentType)
  const resp = await s3
    .putObject({
      Bucket: Bucket,
      Key: key,
      Body: data,
      ContentType: ContentType
    })}
  console.log("Response from S3: ", resp);
  return resp
}

最佳答案

除了 uploadToS3 方法中的一个小变化之外,一切看起来都很好。默认情况下适用于回调模式,除非您最后执行 .promise() 。查看更新后的方法

//upload file to s3 after resizing
async function uploadToS3(data, key, ContentType) {
  console.log("Inside uploadToS3: ", data, key, ContentType)
  const resp = await s3
    .putObject({
      Bucket: Bucket,
      Key: key,
      Body: data,
      ContentType: ContentType
    }).promise();
  console.log("Response from S3: ", resp);
  return resp
}

除了默认的 3 秒之外,还值得将 lambda 超时增加到其他值,以排除操作完成之前超时的情况。

希望这有帮助

关于node.js - Jimp 没有从 aws 的 lambda 触发器中的 url 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59624032/

相关文章:

javascript - 将一张图像叠加在另一张图像上

node.js - Jimp 及其缓冲区数据无法按预期使用 puppeteer 生成 PDF

node.js - 错误: Identifier 'Reader' has already been declared while bundling fstream with rollup.js

javascript - 如何在初始页面加载之前加载 CSS 数据主题以防止主题之间闪烁?

amazon-web-services - 多云kubernetes联合的方法

java - 导入 com.amazonaws.services.dynamodbv2.document.DynamoDB;无法解析导入的文档部分

amazon-web-services - AWS : Invalid storage size for engine name postgres

javascript - Node.js & Socket.io 添加用户名

node.js - Firebase 函数无法部署 : SyntaxError: Unexpected token function