Created
January 3, 2018 22:12
-
-
Save dave-malone/566a47997b9abe49a2da33fc133714cd to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'use strict' | |
console.log('Loading function') | |
const aws = require('aws-sdk') | |
const s3 = new aws.S3({ apiVersion: '2006-03-01' }) | |
const elastictranscoder = new aws.ElasticTranscoder() | |
const config = { | |
pipeline_id: process.env.ELASTIC_TRANSCODER_PIPELINE_ID, | |
output_folder: process.env.S3_OUTPUT_FOLDER, | |
output_key_prefix: process.env.ELASTIC_TRANSCODER_OUTPUT_KEY_PREFIX, | |
output_preset_id: process.env.ELASTIC_TRANSCODER_OUTPUT_PRESET_ID | |
} | |
const submitTranscodingJob = (key) => { | |
console.log(`submitting transcoding job for key ${key}`) | |
var params = { | |
PipelineId: config.pipeline_id, | |
OutputKeyPrefix: config.output_folder, | |
Input: { | |
Key: key | |
}, | |
Outputs: [{ | |
Key: `${config.output_key_prefix}-${key}`, | |
PresetId: config.output_preset_id, | |
ThumbnailPattern: `thumbnails/${key}-{resolution}-{count}`, | |
}] | |
} | |
elastictranscoder.createJob(params, function(err, data) { | |
if (err){ | |
console.log(`An error occurred creating the elastic transcoder job: ${err}`, err.stack); // an error occurred | |
return | |
} | |
console.log(`elastic transcoder job successfully submitted:\n${JSON.stringify(data, null, 2)}`) | |
}) | |
} | |
exports.handler = (event, context, callback) => { | |
console.log('Received event:', JSON.stringify(event, null, 2)) | |
const key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, ' ')) | |
if(key.includes("/") || key.toLowerCase().endsWith(".mp4") !== true){ | |
console.log(`skipping ${key} as it either contained "/" or did not end in ".mp4"`) | |
return | |
} | |
submitTranscodingJob(key) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment