support AWS Polly RoleArn credential

This commit is contained in:
Quan HL
2024-04-19 15:30:22 +07:00
parent ba61f20334
commit 08a56d1a40
2 changed files with 52 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ const fs = require('fs');
const bent = require('bent');
const ttsGoogle = require('@google-cloud/text-to-speech');
const { PollyClient, SynthesizeSpeechCommand } = require('@aws-sdk/client-polly');
const { STSClient, AssumeRoleCommand } = require('@aws-sdk/client-sts');
const sdk = require('microsoft-cognitiveservices-speech-sdk');
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}) => {
try {
const {region, accessKeyId, secretAccessKey} = credentials;
const polly = new PollyClient({
region,
credentials: {
accessKeyId,
secretAccessKey
}
});
const {region, accessKeyId, secretAccessKey, roleArn} = credentials;
let polly;
if (accessKeyId && secretAccessKey) {
polly = new PollyClient({
region,
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 = {
Engine: engine,
OutputFormat: 'mp3',

View File

@@ -162,6 +162,33 @@ test('AWS speech synth tests', async(t) => {
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) => {
const fn = require('..');
const {synthAudio, client} = fn(opts, logger);