使用 Amazon S3 触发器创建缩略图

发布于 2022年 01月 12日 09:17

使用 Amazon S3 触发器创建缩略图

环境

centos (注意,必须是Linux环境)

node12.x

安装教程

curl -sL https://rpm.nodesource.com/setup_12.x | sudo bash -

yum install -y nodejs yum install -y nodejs

​ 参考文档:https://blog.csdn.net/Z_Z_W_/article/details/104988833

教程

​ \1. 建立一个名为lambda-s3文件夹

\2. 建立index.js文件,保存该内容如下,由代码可以看出,桶都是我们手动建立的,并不是代码帮我们建的

// dependencies const AWS = require('aws-sdk'); const util = require('util'); const sharp = require('sharp'); // get reference to S3 client const s3 = new AWS.S3(); exports.handler = async (event, context, callback) => {   // Read options from the event parameter.  console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));  const srcBucket = event.Records[0].s3.bucket.name;  // Object key may have spaces or unicode non-ASCII characters.  const srcKey    = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));  const dstBucket = srcBucket + "-resized";  const dstKey    = "resized-" + srcKey;   // Infer the image type from the file suffix.  const typeMatch = srcKey.match(/\.([^.]*)$/);  if (!typeMatch) {      console.log("Could not determine the image type.");      return;  }   // Check that the image type is supported    const imageType = typeMatch[1].toLowerCase();  if (imageType != "jpg" && imageType != "png") {      console.log(`Unsupported image type: ${imageType}`);      return;  }   // 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(error);      return;  }     // set thumbnail width. Resize will set the height automatically to maintain aspect ratio.  const width  = 200;   // Use the sharp module to resize the image and save in a buffer.  try {       var buffer = await sharp(origimage.Body).resize(width).toBuffer();            } catch (error) {      console.log(error);      return;  }    // Upload the thumbnail image to the destination bucket  try {      const destparams = {          Bucket: dstBucket,          Key: dstKey,          Body: buffer,          ContentType: "image"      };       const putResult = await s3.putObject(destparams).promise();         } catch (error) {      console.log(error);      return;  }         console.log('Successfully resized ' + srcBucket + '/' + srcKey +      ' and uploaded to ' + dstBucket + '/' + dstKey);  };

\3. 使用非root用户在lambda-s3目录下安装sharp包,npm run sharp。注意:安装sharp包对版本严格限制

参考文档:https://sharp.pixelplumbing.com/install

安装好之后如下结构

​ \4. 在lambda下面打包成zip,将该zip文件download下来,命令sz,然后把这个zip文件上传至aws lambda服务的 lambda函数

​ zip -r function.zip . ,该压缩文件我会放在最下面,负责人只需要知道原理就行,如果遇到这个需求,将该压缩文件下载下来就行,免去以上所有步骤

​ \5. 首先我们需要在s3建立一个源公开桶名为 meross-userfiles-mobilehub-1868304105,然后建立一个缩略图桶 meross-userfiles-mobilehub-1868304105-resized。key的路径:如果你的key,都以public开头,那么对应缩略图桶的key路径是以resized-public开头,详细请看以上js代码

​ \6. 在按照https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/with-s3-tutorial.html 文档设置好权限之后,我们往 源桶的对应路径下面上传一个jpg文件,

​ 7.查看目标桶

​ 我们发现目标桶的缩略图自动创建成功

压缩文件下载:

https://note.youdao.com/ynoteshare/index.html?id=0f06dd6b5d62d75782c55b59f45a0d98&type=note&_time=1639794879376

参考文档

https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/with-s3-tutorial.html

https://blog.csdn.net/Z_Z_W_/article/details/104988833

https://sharp.pixelplumbing.com/install

推荐文章