Amazon Lambda add expires headers to S3 storage uploads automatically
Permalink: http://www.ramonfincken.com/permalink/topic436.html
Rating : 3 / 5Rate this article (5 = best) 1 2 3 4 5
Tags:
amazon ,
auto ,
automatic ,
expire ,
expires ,
file ,
files ,
header ,
headers ,
lambda ,
s3 ,
storage ,
upload ,
uploads
View previous topic
::
View next topic
Author
Message
ramon fincken Site's programmerGet a free globally recognized avatar It's free! Joined: 03 Aug 2007 Posts: 403 Location: A'dam/Diemen, The Netherlands
Posted: Sat Nov 12, 2016 12:46 pm Post subject: Amazon Lambda add expires headers to S3 storage uploads automatically
Handy if you are using cloudfront, which is unable (sadly) to add extra headers based on S3 source files.
Runtime
Node.js 4.3
Handler
index.handler
Rest is up to you
Memory
128MB
Timeout
5 sec
VPC
No
Triggers
No -> I have set this up in the bucket config -> Events -> add notification for "Object created"
Code
Code: 'use strict';
// CONFIGURATION //////////////////////////////////////////////
var CacheControlHeader = 'public, max-age=31536000';
var lineBreak = 'nn-----------------------------------------nn';
///////////////////////////////////////////////////////////////
console.log('Loading function...');
let aws = require('aws-sdk');
let s3 = new aws.S3({ apiVersion: '2006-03-01' });
exports.handler = (event, context, callback) => {
const bucket = event.Records[0].s3.bucket.name;
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
var params = { Bucket: bucket, Key: key };
s3.getObject(params, (err, data) => {
if (err) {
console.log(err);
var message = lineBreak+'Error encountered!nnFailed to get object: s3://'+bucket+'/'+key +'.nnMake sure it is in the same region as this function!'+lineBreak;
console.log(message);
} else {
const mimeHeader = data.ContentType;
if (data.CacheControl != CacheControlHeader) {
var params = { Bucket: bucket, Key: key, CopySource: encodeURIComponent(bucket+'/'+key), ContentType: data.ContentType, CacheControl: CacheControlHeader, 'Metadata':{}, MetadataDirective: 'REPLACE' };
s3.copyObject(params, (err, data) => {
if (err) {
console.log(err);
message = lineBreak+'Error encountered!nnFailed to get object: s3://'+bucket+'/'+key +'.nnMake sure it is in the same region as this function!'+lineBreak;
console.log(message);
} else {
message = lineBreak+'Metadata updated successfully!nnOBJECT: s3://'+bucket+'/'+key+'nCONTENT-TYPE: '+mimeHeader+'nCACHE-CONTROL: '+CacheControlHeader+''+lineBreak;
console.log(message);
}
});
} else {
message = lineBreak+'Metadata already updated!nnOBJECT: s3://'+bucket+'/'+key+'nCONTENT-TYPE: '+mimeHeader+'nCACHE-CONTROL: '+CacheControlHeader+''+lineBreak;
console.log(message);
}
}
});
};
Back to top
Google adsense Advertisement
Posted: Sat Nov 12, 2016 12:46 pm Post subject: Amazon Lambda add expires headers to S3 storage uploads automatically
Advertisement
Back to top
GravityForms Advertisement
Posted: Sat Nov 12, 2016 12:46 pm Post subject: Amazon Lambda add expires headers to S3 storage uploads automatically
Advertisement
Back to top
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum