AWS
lambda

[AWS][Lambda] S3に put object されたら自動的に Lambda で Expires ヘッダを追加する

More than 1 year has passed since last update.

元ネタは、以下の記事
Amazon S3の画像をAWS Cloudfrontで配信するキャッシュコントロールをAWS Lambdaで自動化する | Developers.IO

index.js
console.log('Loading event');
var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});

exports.handler = function(event, context) {
   console.log('Received event:');
   console.log(JSON.stringify(event, null, '  '));
   // Get the object from the event and show its content type
   var bucket = event.Records[0].s3.bucket.name;
   var key = event.Records[0].s3.object.key;
   s3.getObject({Bucket:bucket, Key:key},
      function(err,data) {
        if (err) {
           console.log('error getting object ' + key + ' from bucket ' + bucket + 
               '. Make sure they exist and your bucket is in the same region as this function.');
           context.done('error','error getting file'+err);
        }
        else {
            console.log('logging Cache-Control : ',data.CacheControl);
            if  (typeof data.CacheControl != 'undefined'){
                console.log('cache control was already exists.');
                context.done(null,'skip execution.');
            }else {
                var Expires = new Date();
                var ContentType = data.ContentType ? data.ContentType : 'application/octet-stream';
                var CacheControl = 1;

                // set expires date
                switch( true ){
                case /html/.test(ContentType):
                    CacheControl = 1; // 1day
                    break;
                case /^(image|video|audio)\//.test(ContentType):
                    CacheControl = 365; // 1year
                    break;
                case /(javascript|css|text)/.test(ContentType):
                    CacheControl = 30; // 1month
                    break;
                case /^application/.test(ContentType):
                    CacheControl = 30; // 1month
                    break;
                }
                Expires.setDate(Expires.getDate() + CacheControl);

                var params = {
                  Bucket: bucket, /* required */
                  CopySource: bucket + "/" + key,
                  Key: key, /* required */
                  ContentType: ContentType,
                  Expires:  Expires,
                  CacheControl: "max-age="+(86400 * CacheControl).toString(),
                  MetadataDirective: "REPLACE"
                };

                console.log('replace object.');
                console.log('bucket : ' + bucket);
                console.log('CopySource : ' + bucket + "/" + key);

                s3.copyObject(params, function(err2, data2){
                   if (err2){
                       context.done('error','error2 getting file'+err2);
                   }else{
                       console.log('replace done! Cache-Control : ',data2.CacheControl);

                   }
                   context.done(null,'object meta-data changed.');
               });
            }
        }
      }
   );
};