diff options
Diffstat (limited to 'index.js')
-rw-r--r-- | index.js | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/index.js b/index.js new file mode 100644 index 0000000..4f5c9cb --- /dev/null +++ b/index.js @@ -0,0 +1,26 @@ +'use strict'; + +const fetch = require('node-fetch'); +const AWS = require('aws-sdk'); // eslint-disable-line import/no-extraneous-dependencies + +const s3 = new AWS.S3(); + +module.exports.save = (event, context, callback) => { + fetch(event.image_url) + .then((response) => { + if (response.ok) { + return response; + } + return Promise.reject(new Error( + `Failed to fetch ${response.url}: ${response.status} ${response.statusText}`)); + }) + .then(response => response.buffer()) + .then(buffer => ( + s3.putObject({ + Bucket: process.env.BUCKET, + Key: event.key, + Body: buffer, + }).promise() + )) + .then(v => callback(null, v), callback); +}; |