使用 Amazon S3 触发器创建缩略图
使用 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建立一个源公开桶名为 *4105,然后建立一个缩略图桶 *4105-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://docs.aws.amazon.com/zh_cn/lambda/latest/dg/with-s3-tutorial.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)