Last active
April 10, 2016 18:09
-
-
Save glena/63ede2a79c6d415e129ba98adfd2e031 to your computer and use it in GitHub Desktop.
webtask to upload things to s3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Promise = require('bluebird'); | |
var request = Promise.promisify(require("request")); | |
var AWS = require('aws-sdk'); | |
function slugify(text) | |
{ | |
return text.toString().toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/^-+/, '') // Trim - from start of text | |
.replace(/-+$/, ''); // Trim - from end of text | |
} | |
module.exports = function (context, cb) { | |
console.log('init'); | |
AWS.config.update({ | |
region: 'us-west-1', | |
accessKeyId: context.data.accessKeyId, | |
secretAccessKey: context.data.secretAccessKey | |
}); | |
var s3bucket = new AWS.S3({params: {Bucket: '...', Key: '...'}}); | |
request({ | |
method:'GET', | |
url:'https://'+context.data.domain+'/...', | |
headers:{ | |
... | |
} | |
}).then(function(response){ | |
return JSON.parse(response[1]).data; | |
}).filter(function(element){ | |
return element.visible && element.editable; | |
}).map(function(element){ | |
var new_el = { | |
required: element.required, | |
title: element.title, | |
type: element.type, | |
id: element.id, | |
slug: slugify(element.title) | |
}; | |
return new_el; | |
}).then(function(results){ | |
var params = { Body: JSON.stringify(results) }; | |
s3bucket.putObject(params, function (res) { | |
cb(null, 'DONE'); | |
}); | |
}).catch(function(e) { | |
cb(e); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment