文档 AWS SDK 示例 GitHub 存储库中还有更多 S AWS DK 示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 Amazon Polly 的示例 AWS CLI 使用 Bash 脚本
以下代码示例向您展示了如何在 Amazon Polly 中使用 with Bash 脚本来执行操作和实现常见场景。 AWS Command Line Interface
场景是向您演示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。
每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。
主题
场景
以下代码示例展示了如何:
清理资源
- AWS CLI 使用 Bash 脚本
-
注意
还有更多相关信息 GitHub。在 Sample developer tutorials
存储库中查找完整示例,了解如何进行设置和运行。 #!/bin/bash # Amazon Polly Getting Started Script # This script demonstrates how to use Amazon Polly with the AWS CLI set -euo pipefail # Set up logging LOG_FILE="polly-tutorial.log" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" WORK_DIR=$(mktemp -d) trap 'cleanup_temp' EXIT cleanup_temp() { rm -rf "$WORK_DIR" } echo "Starting Amazon Polly tutorial at $(date)" > "$LOG_FILE" # Function to log commands and their output log_cmd() { echo "Running: $1" | tee -a "$LOG_FILE" # Use bash array to safely handle arguments bash -c "$1" 2>&1 | tee -a "$LOG_FILE" || return $? } # Function to check for errors check_error() { if echo "$1" | grep -iq "error"; then echo "ERROR detected in output. Exiting script." | tee -a "$LOG_FILE" echo "$1" | tee -a "$LOG_FILE" return 1 fi return 0 } # Function to handle errors and cleanup handle_error() { local line_number=$1 echo "Error occurred at line $line_number. Attempting cleanup..." | tee -a "$LOG_FILE" cleanup exit 1 } # Function to clean up resources cleanup() { echo "" | tee -a "$LOG_FILE" echo "===========================================================" | tee -a "$LOG_FILE" echo "CLEANUP PROCESS" | tee -a "$LOG_FILE" echo "===========================================================" | tee -a "$LOG_FILE" # Delete lexicon if it exists if [[ -n "${LEXICON_NAME:-}" ]]; then echo "Deleting lexicon: $LEXICON_NAME" | tee -a "$LOG_FILE" if aws polly delete-lexicon --name "$LEXICON_NAME" 2>&1 | tee -a "$LOG_FILE"; then echo "Lexicon deleted successfully." | tee -a "$LOG_FILE" else echo "Warning: Failed to delete lexicon." | tee -a "$LOG_FILE" fi fi # Remove audio files for file in output.mp3 ssml-output.mp3 lexicon-output.mp3 example.pls; do if [[ -f "$file" ]]; then rm -f "$file" echo "Removed $file" | tee -a "$LOG_FILE" fi done echo "Cleanup complete." | tee -a "$LOG_FILE" } # Trap errors with line number trap 'handle_error ${LINENO}' ERR # Verify AWS CLI is available if ! command -v aws &> /dev/null; then echo "AWS CLI is not installed. Please install it first." | tee -a "$LOG_FILE" exit 1 fi # Verify AWS credentials are configured if ! aws sts get-caller-identity &> /dev/null; then echo "AWS credentials are not configured. Please configure them first." | tee -a "$LOG_FILE" exit 1 fi # Step 1: Verify Amazon Polly is available echo "Step 1: Verifying Amazon Polly availability" | tee -a "$LOG_FILE" if aws polly describe-voices --query 'Voices[0].Name' --output text &> /dev/null; then echo "Amazon Polly is available. Proceeding with tutorial." | tee -a "$LOG_FILE" else echo "Amazon Polly is not available in your AWS CLI installation or region." | tee -a "$LOG_FILE" echo "Please update your AWS CLI to the latest version or check your region." | tee -a "$LOG_FILE" exit 1 fi # Step 2: List available voices echo "" | tee -a "$LOG_FILE" echo "Step 2: Listing available voices" | tee -a "$LOG_FILE" log_cmd "aws polly describe-voices --language-code en-US --output text --query 'Voices[0:3].[Id, LanguageCode, Gender]'" || true # Step 3: Basic text-to-speech conversion echo "" | tee -a "$LOG_FILE" echo "Step 3: Converting text to speech" | tee -a "$LOG_FILE" OUTPUT_FILE="${WORK_DIR}/output.mp3" POLLY_TEXT="Hello, welcome to Amazon Polly. This is a sample text to speech conversion." log_cmd "aws polly synthesize-speech --output-format mp3 --voice-id Joanna --text '$POLLY_TEXT' '$OUTPUT_FILE'" || true if [[ -f "$OUTPUT_FILE" ]]; then echo "Successfully created output.mp3 file." | tee -a "$LOG_FILE" echo "You can play this file with your preferred audio player." | tee -a "$LOG_FILE" cp "$OUTPUT_FILE" output.mp3 else echo "Failed to create output.mp3 file." | tee -a "$LOG_FILE" exit 1 fi # Step 4: Using SSML for enhanced speech echo "" | tee -a "$LOG_FILE" echo "Step 4: Using SSML for enhanced speech" | tee -a "$LOG_FILE" SSML_OUTPUT="${WORK_DIR}/ssml-output.mp3" SSML_TEXT='<speak>Hello! <break time="1s"/> This is a sample of <emphasis>SSML enhanced speech</emphasis>.</speak>' log_cmd "aws polly synthesize-speech --output-format mp3 --voice-id Matthew --text-type ssml --text '$SSML_TEXT' '$SSML_OUTPUT'" || true if [[ -f "$SSML_OUTPUT" ]]; then echo "Successfully created ssml-output.mp3 file." | tee -a "$LOG_FILE" echo "You can play this file with your preferred audio player." | tee -a "$LOG_FILE" cp "$SSML_OUTPUT" ssml-output.mp3 else echo "Failed to create ssml-output.mp3 file." | tee -a "$LOG_FILE" exit 1 fi # Step 5: Working with lexicons echo "" | tee -a "$LOG_FILE" echo "Step 5: Working with lexicons" | tee -a "$LOG_FILE" # Generate a random identifier for the lexicon (max 20 chars, alphanumeric only) LEXICON_NAME="example$(openssl rand -hex 6 | cut -c 1-10)" echo "Using lexicon name: $LEXICON_NAME" | tee -a "$LOG_FILE" # Create a lexicon file echo "Creating lexicon file..." | tee -a "$LOG_FILE" LEXICON_FILE="${WORK_DIR}/example.pls" cat > "$LEXICON_FILE" << 'EOF' <?xml version="1.0" encoding="UTF-8"?> <lexicon version="1.0" xmlns="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd" alphabet="ipa" xml:lang="en-US"> <lexeme> <grapheme>AWS</grapheme> <alias>Amazon Web Services</alias> </lexeme> </lexicon> EOF # Upload the lexicon echo "Uploading lexicon..." | tee -a "$LOG_FILE" LEXICON_ARN=$(aws polly put-lexicon --name "$LEXICON_NAME" --content file://"$LEXICON_FILE" --query 'LexiconArn' --output text 2>&1) || true if [[ -n "$LEXICON_ARN" && "$LEXICON_ARN" != "" && ! "$LEXICON_ARN" =~ error ]]; then echo "Lexicon uploaded with ARN: $LEXICON_ARN" | tee -a "$LOG_FILE" aws polly tag-resource --resource-arn "$LEXICON_ARN" --tags Key=project,Value=doc-smith Key=tutorial,Value=amazon-polly-gs 2>&1 | tee -a "$LOG_FILE" || true else echo "Lexicon uploaded." | tee -a "$LOG_FILE" fi # List available lexicons echo "Listing available lexicons..." | tee -a "$LOG_FILE" log_cmd "aws polly list-lexicons --output text --query 'Lexicons[*].[Name]'" || true # Get details about the lexicon echo "Getting details about the lexicon..." | tee -a "$LOG_FILE" log_cmd "aws polly get-lexicon --name '$LEXICON_NAME' --output text --query 'Lexicon.Name'" || true # Use the lexicon when synthesizing speech echo "Using the lexicon for speech synthesis..." | tee -a "$LOG_FILE" LEXICON_OUTPUT="${WORK_DIR}/lexicon-output.mp3" LEXICON_TEXT="I work with AWS every day." log_cmd "aws polly synthesize-speech --output-format mp3 --voice-id Joanna --lexicon-names '$LEXICON_NAME' --text '$LEXICON_TEXT' '$LEXICON_OUTPUT'" || true if [[ -f "$LEXICON_OUTPUT" ]]; then echo "Successfully created lexicon-output.mp3 file." | tee -a "$LOG_FILE" echo "You can play this file with your preferred audio player." | tee -a "$LOG_FILE" cp "$LEXICON_OUTPUT" lexicon-output.mp3 else echo "Failed to create lexicon-output.mp3 file." | tee -a "$LOG_FILE" exit 1 fi # Summary of created resources echo "" | tee -a "$LOG_FILE" echo "===========================================================" | tee -a "$LOG_FILE" echo "TUTORIAL SUMMARY" | tee -a "$LOG_FILE" echo "===========================================================" | tee -a "$LOG_FILE" echo "Created resources:" | tee -a "$LOG_FILE" echo "1. Lexicon: $LEXICON_NAME" | tee -a "$LOG_FILE" echo "2. Audio files:" | tee -a "$LOG_FILE" echo " - output.mp3" | tee -a "$LOG_FILE" echo " - ssml-output.mp3" | tee -a "$LOG_FILE" echo " - lexicon-output.mp3" | tee -a "$LOG_FILE" echo "" | tee -a "$LOG_FILE" # Cleanup with auto-confirmation echo "" | tee -a "$LOG_FILE" echo "===========================================================" | tee -a "$LOG_FILE" echo "CLEANUP CONFIRMATION" | tee -a "$LOG_FILE" echo "===========================================================" | tee -a "$LOG_FILE" echo "Cleaning up all created resources..." | tee -a "$LOG_FILE" cleanup echo "" | tee -a "$LOG_FILE" echo "Tutorial completed successfully!" | tee -a "$LOG_FILE" echo "Log file: $LOG_FILE" | tee -a "$LOG_FILE"-
有关 API 详细信息,请参阅《AWS CLI 命令参考》中的以下主题。
-