Skip to content

Instantly share code, notes, and snippets.

@qrush
Forked from marianmoldovan/index.js
Created May 30, 2018 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qrush/91f4ab87ae3b5b20a387629f6ece01d6 to your computer and use it in GitHub Desktop.
Save qrush/91f4ab87ae3b5b20a387629f6ece01d6 to your computer and use it in GitHub Desktop.
AWS Polly Lambda API
'use strict';
const aws = require('aws-sdk');
const s3 = new aws.S3({ apiVersion: '2006-03-01' });
const polly = new aws.Polly({region: 'eu-west-1'});
const md5 = require('md5');
const request = require('request');
var params = {
OutputFormat: 'mp3',
Text: 'Hola',
VoiceId: 'Conchita',
TextType: 'text'
};
var exportBucket = 'beeva-radical-lab/audios';
exports.handler = function(event, context, callback) {
if(event.text)
params.Text = event.text;
if(event.voice)
params.VoiceId = event.voice;
const fileName = md5(params.VoiceId + params.Text);
// Cache with S3 and checking cache availability with making a HTTP HEAD request (way faster than using S3 API)
const potentialURL = 'https://s3-eu-west-1.amazonaws.com/' + exportBucket + '/' + fileName + '.mp3';
request.head(potentialURL, function (error, response, body) {
if(response.statusCode === 200)
callback(null, {'voice': potentialURL})
else {
polly.synthesizeSpeech(params, function(err, data) {
if (err) callback(err);
else {
var s3Params = {ACL: 'public-read', Bucket: exportBucket, Key: fileName + '.mp3', ContentType: 'audio/mpeg', Body: data.AudioStream};
s3.upload(s3Params, function(err, data) {
if (err) callback(err);
else callback(null, {'voice': data.Location});
});
}
});
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment