mirror of
https://github.com/jambonz/speech-utils.git
synced 2026-01-25 02:08:26 +00:00
support AWS Polly RoleArn credential
This commit is contained in:
@@ -3,6 +3,7 @@ const fs = require('fs');
|
|||||||
const bent = require('bent');
|
const bent = require('bent');
|
||||||
const ttsGoogle = require('@google-cloud/text-to-speech');
|
const ttsGoogle = require('@google-cloud/text-to-speech');
|
||||||
const { PollyClient, SynthesizeSpeechCommand } = require('@aws-sdk/client-polly');
|
const { PollyClient, SynthesizeSpeechCommand } = require('@aws-sdk/client-polly');
|
||||||
|
const { STSClient, AssumeRoleCommand } = require('@aws-sdk/client-sts');
|
||||||
|
|
||||||
const sdk = require('microsoft-cognitiveservices-speech-sdk');
|
const sdk = require('microsoft-cognitiveservices-speech-sdk');
|
||||||
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
|
const TextToSpeechV1 = require('ibm-watson/text-to-speech/v1');
|
||||||
@@ -265,14 +266,30 @@ async function synthAudio(client, logger, stats, { account_sid,
|
|||||||
|
|
||||||
const synthPolly = async(logger, {credentials, stats, language, voice, engine, text}) => {
|
const synthPolly = async(logger, {credentials, stats, language, voice, engine, text}) => {
|
||||||
try {
|
try {
|
||||||
const {region, accessKeyId, secretAccessKey} = credentials;
|
const {region, accessKeyId, secretAccessKey, roleArn} = credentials;
|
||||||
const polly = new PollyClient({
|
let polly;
|
||||||
region,
|
if (accessKeyId && secretAccessKey) {
|
||||||
credentials: {
|
polly = new PollyClient({
|
||||||
accessKeyId,
|
region,
|
||||||
secretAccessKey
|
credentials: {
|
||||||
}
|
accessKeyId,
|
||||||
});
|
secretAccessKey
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} else if (roleArn) {
|
||||||
|
const stsClient = new STSClient({ region});
|
||||||
|
const roleToAssume = { RoleArn: 'myRoleArn', RoleSessionName: 'session1' };
|
||||||
|
const command = new AssumeRoleCommand(roleToAssume);
|
||||||
|
|
||||||
|
const response = await stsClient.send(command);
|
||||||
|
const assumedRoleCreds = response.Credentials;
|
||||||
|
polly = new PollyClient({
|
||||||
|
region,
|
||||||
|
credentials: assumedRoleCreds,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
throw new Error('Missing Polly credential');
|
||||||
|
}
|
||||||
const opts = {
|
const opts = {
|
||||||
Engine: engine,
|
Engine: engine,
|
||||||
OutputFormat: 'mp3',
|
OutputFormat: 'mp3',
|
||||||
|
|||||||
@@ -162,6 +162,33 @@ test('AWS speech synth tests', async(t) => {
|
|||||||
client.quit();
|
client.quit();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('AWS speech synth tests by RoleArn', async(t) => {
|
||||||
|
const fn = require('..');
|
||||||
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|
||||||
|
if (!process.env.AWS_ROLE_ARN || !process.env.AWS_REGION) {
|
||||||
|
t.pass('skipping AWS speech synth tests by RoleArn since AWS_ROLE_ARN or AWS_REGION not provided');
|
||||||
|
return t.end();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
let opts = await synthAudio(stats, {
|
||||||
|
vendor: 'aws',
|
||||||
|
credentials: {
|
||||||
|
roleArn: process.env.AWS_ROLE_ARN,
|
||||||
|
region: process.env.AWS_REGION,
|
||||||
|
},
|
||||||
|
language: 'en-US',
|
||||||
|
voice: 'Joey',
|
||||||
|
text: 'This is a test. This is only a test',
|
||||||
|
});
|
||||||
|
t.ok(!opts.servedFromCache, `successfully synthesized aws by roleArn audio to ${opts.filePath}`);
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
t.end(err);
|
||||||
|
}
|
||||||
|
client.quit();
|
||||||
|
});
|
||||||
|
|
||||||
test('Azure speech synth tests', async(t) => {
|
test('Azure speech synth tests', async(t) => {
|
||||||
const fn = require('..');
|
const fn = require('..');
|
||||||
const {synthAudio, client} = fn(opts, logger);
|
const {synthAudio, client} = fn(opts, logger);
|
||||||
|
|||||||
Reference in New Issue
Block a user