Skip to content

Instantly share code, notes, and snippets.

@ohtangza
Last active March 8, 2018 06:25
Show Gist options
  • Save ohtangza/e581bfe8543765382bf8ade5af4b17ac to your computer and use it in GitHub Desktop.
Save ohtangza/e581bfe8543765382bf8ade5af4b17ac to your computer and use it in GitHub Desktop.
AWS Polly via HTTP API
import axios from 'axios';
import aws4 from 'aws4';
// https://docs.aws.amazon.com/general/latest/gr/rande.html
const endPoint = 'https://polly.ap-northeast-2.amazonaws.com';
const data = {
OutputFormat: 'mp3',
SampleRate: '16000',
Text: 'hello',
TextType: 'text',
VoiceId: 'Joanna',
};
const opts = {
// method: 'POST',
host: 'polly.ap-northeast-2.amazonaws.com',
path: '/v1/speech',
body: JSON.stringify(data),
};
aws4.sign(opts, {
accessKeyId: '<accessKeyId>',
secretAccessKey: '<secretAccessKey>',
});
axios.post(
`${endPoint}/v1/speech`,
JSON.stringify(data),
{ headers: opts.headers },
)
.then((response) => {
console.log('success');
console.log(response.data);
})
.catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log();
console.log(error.response.status);
console.log();
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
import axios from 'axios';
import moment from 'moment';
import AWSSignature from 'react-native-aws-signature';
const awsSignature = new AWSSignature();
const credentials = {
AccessKeyId: 'AKIAIMZ6SRGIIBXOMLBA',
SecretKey: 'talpvO7j9BsWZpPsAQHpWxtMHPbE6eNADBwRYOXF',
};
const data = {
OutputFormat: 'mp3',
SampleRate: '16000',
Text: 'hello',
TextType: 'text',
VoiceId: 'Joanna',
};
const protocol = 'https';
const host = 'polly.ap-northeast-2.amazonaws.com';
const path = '/v1/speech';
// https://docs.aws.amazon.com/general/latest/gr/rande.html
const endPoint = `${protocol}://${host}${path}`;
const xAmzDate = `${moment().utc().format('YYYY-MM-DDTHH:mm:ss')}Z`;
// awsSignature._formatDateTime('2015-02-09T10:00:00Z');
console.log(xAmzDate);
const options = {
path,
method: 'post',
service: 'polly',
headers: {
'X-Amz-Date': xAmzDate,
host,
},
region: 'ap-northeast-2',
body: JSON.stringify(data),
credentials,
};
awsSignature.setParams(options);
// const signature = awsSignature.getSignature();
const authorization = awsSignature.getAuthorizationHeader();
axios.post(
endPoint,
JSON.stringify(data),
{
headers: {
'X-Amz-Date': xAmzDate,
...authorization,
},
},
)
.then((response) => {
console.log('success');
console.log(response.data);
})
.catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log();
console.log(error.response.status);
console.log();
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
/*
// https://docs.aws.amazon.com/general/latest/gr/rande.html
const endPoint = 'https://polly.ap-northeast-2.amazonaws.com';
const opts = {
// method: 'POST',
host: 'polly.ap-northeast-2.amazonaws.com',
path: '/v1/speech',
body: JSON.stringify(data),
};
aws4.sign(opts, {
accessKeyId: 'AKIAIMZ6SRGIIBXOMLBA',
secretAccessKey: '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',
});
axios.post(
`${endPoint}/v1/speech`,
JSON.stringify(data),
{ headers: opts.headers },
)
.then((response) => {
console.log('success');
console.log(response.data);
})
.catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log();
console.log(error.response.status);
console.log();
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
*/
/*
axios.post(
`${endPoint}/v1/speech`,
JSON.stringify(data),
{
headers: opts.headers,
transformRequest: [(data, headers) => {
console.log(data);
console.log(headers);
console.log();
delete headers.common;
delete headers.post['Content-Type'];
return data;
}],
},
)
.then((response) => {
console.log('success');
console.log(response.data);
})
.catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log();
console.log(error.response.status);
console.log();
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
*/
/*
axios.post(`${endPoint}/v1/speech`, data, config)
.then((response) => {
console.log(response.data);
})
.catch((error) => {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}
console.log(error.config);
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment