| 序号 | 更新时间 | 备注 |
|---|---|---|
| 1 | 2025.04.01 | 初始化整理笔记 |
| 2 | 2025.04.02 | 补充SDK描述示例和lambda的方式构建 |
| 3 | 2025.04.09 | 补充api gateway的详细描述及 安全策略设置 |
文章目录
一、前言
本文根据AWS的
TTS功能构建试验文档,包含java实践引入文档 - Amazon Polly
基于官方提供文档和合作伙伴提供的讯息,主要有两种模式搭建
1、利用环境安装
2、利用环境的文件
二、本地环境配置操作
列出配置
aws configure list
设置配置
然后填入已经准备好的讯息
AWS Access Key ID: 您的访问密钥 ID。AWS Secret Access Key: 您的密钥。- 默认区域(如:
us-west-1)。- 输出格式(如:
json)。
aws configure
三、基础内容
1、查看支持的语言及语音(不要盲目相信官方文档描述)
言语列表参考地址: https://docs.aws.amazon.com/polly/latest/dg/available-voices.html
# 这里是获取标准的语言
DescribeVoicesRequest describeVoiceRequest = DescribeVoicesRequest.builder()
.engine("standard")
.build();
DescribeVoicesResponse describeVoicesResult = polly.describeVoices(describeVoiceRequest);
2、引入依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.29.45</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>polly</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.soundlibs</groupId>
<artifactId>jlayer</artifactId>
<version>1.0.1.4</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sso</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>ssooidc</artifactId>
</dependency>
</dependencies>
3、統一註冊事件
大陆及港澳台
REGION = "ap-southeast-1"构建
PollyClient
1、利用本地环境模式
正如【二】中所示,在运行环境中配置
aws configure即可。执行sdk过程中不设置 profileFile 和 credentialsProvider 即可
PollyClient polly = PollyClient.builder()
.region(Region.of(REGION))
.build();
2、引入本地文件的模式
由
资源目录中设置配置文件,示例配置文件credentials[default] aws_access_key_id = EXAMPLEACCESSKEY1234 aws_secret_access_key = EXAMPLESECRETKEY1234567890abcdefg [my-profile] aws_access_key_id = EXAMPLEACCESSKEY12345 aws_secret_access_key = EXAMPLESECRETKEY1234567890abcdefgh
// 从 resources 中获取凭证文件的输入流,假设文件路径为 "/aws/credentials"
InputStream credentialsStream = PollyDemo.class
.getResourceAsStream("/aws/credentials");
if (credentialsStream == null) {
throw new RuntimeException("未能在 resources 中找到 '/aws/credentials' 文件");
}
// 将资源文件复制到临时文件中
Path tempCredentialsFile = Files.createTempFile("aws-credentials", ".tmp");
Files.copy(credentialsStream, tempCredentialsFile, StandardCopyOption.REPLACE_EXISTING);
credentialsStream.close();
// 使用临时文件构造 ProfileFile 对象
ProfileFile profileFile = ProfileFile.builder()
.content(tempCredentialsFile)
.type(ProfileFile.Type.CREDENTIALS)
.build();
DefaultCredentialsProvider build = DefaultCredentialsProvider.builder().profileFile(profileFile)
.profileName("my-profile").build();
PollyClient polly = PollyClient.builder()
.credentialsProvider(build)
.region(Region.AP_SOUTHEAST_1)
.build();
3、直接赋值的模式
直接明文配置内容
// 创建 PollyClient
PollyClient polly = PollyClient.builder()
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(ACCESS_KEY, SECRET_KEY)
))
.region(Region.of(REGION))
.build();
四、TTS 方式
这里采用
mode作为参数值 ,可参考software.amazon.awssdk.services.polly.model.Engine其实靠谱实际用的就是
standard和neural
- standard (标准)
- neural (神经)
- long-form (长格式语音)
- generative(生成式语音)
DescribeVoicesRequest describeVoiceRequest = DescribeVoicesRequest.builder()
.engine(mode)
.build();
获取语音实体,这里示例获取Joanna
DescribeVoicesResponse describeVoicesResult = polly.describeVoices(describeVoiceRequest)<

最低0.47元/天 解锁文章
1627

被折叠的 条评论
为什么被折叠?



