飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

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

时间:2021-12-19  作者:agopher  

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

环境

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

node12.x

安装教程

curl -sL https://域名/setup_12.x | sudo bash -

yum install -y nodejs yum install -y nodejs

​ 参考文档:https://域名/Z_Z_W_/article/details/104988833

教程

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

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

// dependencies const AWS = require(\'aws-sdk\'); const util = require(\'util\'); const sharp = require(\'sharp\'); // get reference to S3 client const s3 = new 域名(); 域名ler = async (event, context, callback) => {   // Read options from the event parameter.  域名("Reading options from event:\n", 域名ect(event, {depth: 5}));  const srcBucket = 域名rds[0].域名;  // Object key may have spaces or unicode non-ASCII characters.  const srcKey    = decodeURIComponent(域名rds[0].域名.replace(/\+/g, " "));  const dstBucket = srcBucket + "-resized";  const dstKey    = "resized-" + srcKey;   // Infer the image type from the file suffix.  const typeMatch = 域名h(/\.([^.]*)$/);  if (!typeMatch) {      域名("Could not determine the image type.");      return;  }   // Check that the image type is supported    const imageType = typeMatch[1].toLowerCase();  if (imageType != "jpg" && imageType != "png") {      域名(`Unsupported image type: ${imageType}`);      return;  }   // Download the image from the S3 source bucket.    try {      const params = {          Bucket: srcBucket,          Key: srcKey      };      var origimage = await 域名bject(params).promise();   } catch (error) {      域名(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(域名).resize(width).toBuffer();            } catch (error) {      域名(error);      return;  }    // Upload the thumbnail image to the destination bucket  try {      const destparams = {          Bucket: dstBucket,          Key: dstKey,          Body: buffer,          ContentType: "image"      };       const putResult = await 域名bject(destparams).promise();         } catch (error) {      域名(error);      return;  }         域名(\'Successfully resized \' + srcBucket + \'/\' + srcKey +      \' and uploaded to \' + dstBucket + \'/\' + dstKey);  };

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

参考文档:https://域名/install

安装好之后如下结构

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

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

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

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

​ 7.查看目标桶

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

压缩文件下载:

https://域名/ynoteshare/域名?id=0f06dd6b5d62d75782c55b59f45a0d98&type=note&_time=1639794879376

参考文档

https://域名域名/zh_cn/lambda/latest/dg/with-s3-域名

https://域名/Z_Z_W_/article/details/104988833

https://域名/install

标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。