mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Lambda docs (#264)
* Add aws.md and s3 example Signed-off-by: Seif Lotfy <seif.lotfy@gmail.com> * minor fix
This commit is contained in:
committed by
C Cirello
parent
0cc946d937
commit
ba65220127
5
examples/s3/Dockerfile
Normal file
5
examples/s3/Dockerfile
Normal file
@@ -0,0 +1,5 @@
|
||||
FROM iron/lambda-nodejs
|
||||
|
||||
ADD example.js ./example.js
|
||||
|
||||
CMD ["example.run"]
|
||||
7
examples/s3/Makefile
Normal file
7
examples/s3/Makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
IMAGE=iron/lambda-node-aws-example
|
||||
|
||||
create: Dockerfile
|
||||
docker build -t $(IMAGE) .
|
||||
|
||||
test:
|
||||
docker run --rm -it -e PAYLOAD_FILE=/mnt/example-payload.json -e AWS_ACCESS_KEY_ID=change-here -e AWS_SECRET_ACCESS_KEY=change-here -v `pwd`:/mnt $(IMAGE)
|
||||
2
examples/s3/README.md
Normal file
2
examples/s3/README.md
Normal file
@@ -0,0 +1,2 @@
|
||||
Example on how to use AWS S3 in a lambda function.
|
||||
|
||||
5
examples/s3/example-payload.json
Normal file
5
examples/s3/example-payload.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"bucket": "iron-lambda-demo-images",
|
||||
"srcKey": "waterfall.jpg",
|
||||
"dstKey": "waterfall-1024.jpg"
|
||||
}
|
||||
70
examples/s3/example.js
Normal file
70
examples/s3/example.js
Normal file
@@ -0,0 +1,70 @@
|
||||
var im = require('imagemagick');
|
||||
var fs = require('fs');
|
||||
var AWS = require('aws-sdk');
|
||||
|
||||
// cb(err, resized) is called with true if resized.
|
||||
function resizeIfRequired(err, features, fileSrc, fileDst, cb) {
|
||||
if (err) {
|
||||
cb(err, false);
|
||||
return;
|
||||
}
|
||||
|
||||
var targetWidth = 1024;
|
||||
if (features.width > targetWidth)
|
||||
{
|
||||
im.resize({
|
||||
srcPath : fileSrc,
|
||||
dstPath : fileDst,
|
||||
width : targetWidth,
|
||||
format: 'jpg'
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
cb(err, false);
|
||||
} else {
|
||||
cb(null, true);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
cb(null, false);
|
||||
}
|
||||
}
|
||||
|
||||
exports.run = function(event, context) {
|
||||
var bucketName = event['bucket']
|
||||
var srcImageKey = event['srcKey']
|
||||
var dstImageKey = event['dstKey']
|
||||
|
||||
var s3 = new AWS.S3();
|
||||
|
||||
s3.getObject({
|
||||
Bucket: bucketName,
|
||||
Key: srcImageKey
|
||||
}, function (err, data) {
|
||||
|
||||
if (err) throw err;
|
||||
|
||||
var fileSrc = '/tmp/image-src.dat';
|
||||
var fileDst = '/tmp/image-dst.dat'
|
||||
fs.writeFileSync(fileSrc, data.Body)
|
||||
|
||||
im.identify(fileSrc, function(err, features) {
|
||||
resizeIfRequired(err, features, fileSrc, fileDst, function(err, resized) {
|
||||
if (err) throw err;
|
||||
if (resized) {
|
||||
s3.putObject({
|
||||
Bucket:bucketName,
|
||||
Key: dstImageKey,
|
||||
Body: fs.createReadStream(fileDst),
|
||||
ContentType: 'image/jpeg',
|
||||
ACL: 'public-read',
|
||||
}, function (err, data) {
|
||||
if (err) throw err;
|
||||
context.succeed("Image updated");
|
||||
});
|
||||
} else {
|
||||
context.succeed("Image not updated");
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user