zoukankan      html  css  js  c++  java
  • AWS的lambda和S3之间如何连携

      今天正好遇到了这个问题,就在官方文档里查询,然后根据他的说明整理了一下大致的流程,详细的请参考AWS国际版的官方文档,这里只是作者的一个简单的流程展示。

      

    Lambda和S3连接

    1.在S3的同一区域当中创建2个桶

    命名规则:source

              Sourceresized

    2.往source的S3桶中上传一张命名为HappyFace.jpg的文件

    3.准备一台安装好node.js的服务器

    ①创建一个文件夹

    mkdir examplefolder

    ②在这个文件夹路径下创建一个子文件夹

    cd examplefolder

    mkdir node_modules

    ③安装插件,在当前路径下

    npm install async gm

    ④安装完毕,创建一个文件index.js

    touch index.js

    ⑤打开index.js文本进行编辑,编辑内容如下

    vi index.js

    按“i”进行编辑

    // dependencies

    var async = require('async');

    var AWS = require('aws-sdk');

    var gm = require('gm')

                .subClass({ imageMagick: true }); // Enable ImageMagick integration.

    var util = require('util');

    // constants

    var MAX_WIDTH  = 100;

    var MAX_HEIGHT = 100;

    // get reference to S3 client

    var s3 = new AWS.S3();

    exports.handler = function(event, context, callback) {

        // Read options from the event.

        console.log("Reading options from event: ", util.inspect(event, {depth: 5}));

        var srcBucket = event.Records[0].s3.bucket.name;

        // Object key may have spaces or unicode non-ASCII characters.

        var srcKey    =

        decodeURIComponent(event.Records[0].s3.object.key.replace(/+/g, " ")); 

        var dstBucket = srcBucket + "resized";

        var dstKey    = "resized-" + srcKey;

        // Sanity check: validate that source and destination are different buckets.

        if (srcBucket == dstBucket) {

            callback("Source and destination buckets are the same.");

            return;

        }

        // Infer the image type.

        var typeMatch = srcKey.match(/.([^.]*)$/);

        if (!typeMatch) {

            callback("Could not determine the image type.");

            return;

        }

        var imageType = typeMatch[1];

        if (imageType != "jpg" && imageType != "png") {

            callback('Unsupported image type: ${imageType}');

            return;

        }

        // Download the image from S3, transform, and upload to a different S3 bucket.

        async.waterfall([

            function download(next) {

                // Download the image from S3 into a buffer.

                s3.getObject({

                        Bucket: srcBucket,

                        Key: srcKey

                    },

                    next);

                },

            function transform(response, next) {

                gm(response.Body).size(function(err, size) {

                    // Infer the scaling factor to avoid stretching the image unnaturally.

                    var scalingFactor = Math.min(

                        MAX_WIDTH / size.width,

                        MAX_HEIGHT / size.height

                    );

                    var width  = scalingFactor * size.width;

                    var height = scalingFactor * size.height;

                    // Transform the image buffer in memory.

                    this.resize(width, height)

                        .toBuffer(imageType, function(err, buffer) {

                            if (err) {

                                next(err);

                            } else {

                                next(null, response.ContentType, buffer);

                            }

                        });

                });

            },

            function upload(contentType, data, next) {

                // Stream the transformed image to a different S3 bucket.

                s3.putObject({

                        Bucket: dstBucket,

                        Key: dstKey,

                        Body: data,

                        ContentType: contentType

                    },

                    next);

                }

            ], function (err) {

                if (err) {

                    console.error(

                        'Unable to resize ' + srcBucket + '/' + srcKey +

                        ' and upload to ' + dstBucket + '/' + dstKey +

                        ' due to an error: ' + err

                    );

                } else {

                    console.log(

                        'Successfully resized ' + srcBucket + '/' + srcKey +

                        ' and uploaded to ' + dstBucket + '/' + dstKey

                    );

                }

                callback(null, "message");

            }

        );

    };

    编辑完毕按“Esc”退出,然后按“:wq”→enter保存退出

    ⑥当前路径下应有如下内容

    index.js

    /node_modules/gm

    /node_modules/async

    对如下内容进行打包

    命令:

    tar -czvf  ./*  ./CreateThumbnail.zip

    然后把包导出来

    ⑦创建IAM角色

    ⑧在lambda上上传压缩包

    ⑨在lambda上进行S3的调用测试

    代码如下

       "Records":[ 

          { 

             "eventVersion":"2.0",

             "eventSource":"aws:s3",

             "awsRegion":"us-west-2",

             "eventTime":"1970-01-01T00:00:00.000Z",

             "eventName":"ObjectCreated:Put",

             "userIdentity":{ 

                "principalId":"AIDAJDPLRKLG7UEXAMPLE"

             },

             "requestParameters":{ 

                "sourceIPAddress":"127.0.0.1"

             },

             "responseElements":{ 

                "x-amz-request-id":"C3D13FE58DE4C810",

                "x-amz-id-2":"FMyUVURIY8/IgAtTv8xRjskZQpcIZ9KG4V5Wp6S7S/JRWeUWerMUE5JgHvANOjpD"

             },

             "s3":{ 

                "s3SchemaVersion":"1.0",

                "configurationId":"testConfigRule",

                "bucket":{ 

                   "name":"sourcebucket",

                   "ownerIdentity":{ 

                      "principalId":"A3NL1KOZZKExample"

                   },

                   "arn":"arn:aws:s3:::sourcebucket"

                },

                "object":{ 

                   "key":"HappyFace.jpg", #S3source 桶里面放的文件名

                   "size":1024,

                   "eTag":"d41d8cd98f00b204e9800998ecf8427e",

                   "versionId":"096fKKXTRTtl3on89fVO.nfljtsv6qko"

                }

             }

          }

       ]

    }

    ⑩在S3那个sourceresized的S3桶里查看是否有文件生成

    生成为成功,没有内容再调查问题原因。

  • 相关阅读:
    WebSocket理解与使用
    JS面向对象与prototype,__proto__,constructor
    JS值传递与引用传递
    vue报错:./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!
    对于图片视频编辑类产品的思考
    时尚衣裙外贸独立站搭建(一)
    2021年Wordpress手把手教你做个独立站——部署篇
    一次备案失败所引发的思考
    2021年Wordpress博客装修美化(二)
    做什么,怎么做?(20210615)
  • 原文地址:https://www.cnblogs.com/cnqfz/p/8603144.html
Copyright © 2011-2022 走看看