You are viewing documentation for version 2 of the AWS SDK for Ruby. Version 3 documentation can be found here.

Class: Aws::Polly::Client

Inherits:
Seahorse::Client::Base show all
Defined in:
(unknown)

Overview

An API client for Amazon Polly. To construct a client, you need to configure a :region and :credentials.

polly = Aws::Polly::Client.new(
  region: region_name,
  credentials: credentials,
  # ...
)

See #initialize for a full list of supported configuration options.

Region

You can configure a default region in the following locations:

  • ENV['AWS_REGION']
  • Aws.config[:region]

Go here for a list of supported regions.

Credentials

Default credentials are loaded automatically from the following locations:

  • ENV['AWS_ACCESS_KEY_ID'] and ENV['AWS_SECRET_ACCESS_KEY']
  • Aws.config[:credentials]
  • The shared credentials ini file at ~/.aws/credentials (more information)
  • From an instance profile when running on EC2

You can also construct a credentials object from one of the following classes:

Alternatively, you configure credentials with :access_key_id and :secret_access_key:

# load credentials from disk
creds = YAML.load(File.read('/path/to/secrets'))

Aws::Polly::Client.new(
  access_key_id: creds['access_key_id'],
  secret_access_key: creds['secret_access_key']
)

Always load your credentials from outside your application. Avoid configuring credentials statically and never commit them to source control.

Instance Attribute Summary

Attributes inherited from Seahorse::Client::Base

#config, #handlers

Constructor collapse

API Operations collapse

Instance Method Summary collapse

Methods inherited from Seahorse::Client::Base

add_plugin, api, #build_request, clear_plugins, define, new, #operation, #operation_names, plugins, remove_plugin, set_api, set_plugins

Methods included from Seahorse::Client::HandlerBuilder

#handle, #handle_request, #handle_response

Constructor Details

#initialize(options = {}) ⇒ Aws::Polly::Client

Constructs an API client.

Options Hash (options):

  • :access_key_id (String)

    Used to set credentials statically. See Aws::Plugins::RequestSigner for more details.

  • :convert_params (Boolean) — default: true

    When true, an attempt is made to coerce request parameters into the required types. See Aws::Plugins::ParamConverter for more details.

  • :credentials (required, Credentials)

    Your AWS credentials. The following locations will be searched in order for credentials:

    • :access_key_id, :secret_access_key, and :session_token options
    • ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']
    • HOME/.aws/credentials shared credentials file
    • EC2 instance profile credentials See Aws::Plugins::RequestSigner for more details.
  • :endpoint (String)

    A default endpoint is constructed from the :region. See Aws::Plugins::RegionalEndpoint for more details.

  • :http_continue_timeout (Float) — default: 1

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :http_idle_timeout (Integer) — default: 5

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :http_open_timeout (Integer) — default: 15

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :http_proxy (String)

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :http_read_timeout (Integer) — default: 60

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :http_wire_trace (Boolean) — default: false

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :log_level (Symbol) — default: :info

    The log level to send messages to the logger at. See Aws::Plugins::Logging for more details.

  • :log_formatter (Logging::LogFormatter)

    The log formatter. Defaults to Seahorse::Client::Logging::Formatter.default. See Aws::Plugins::Logging for more details.

  • :logger (Logger) — default: nil

    The Logger instance to send log messages to. If this option is not set, logging will be disabled. See Aws::Plugins::Logging for more details.

  • :profile (String)

    Used when loading credentials from the shared credentials file at HOME/.aws/credentials. When not specified, 'default' is used. See Aws::Plugins::RequestSigner for more details.

  • :raise_response_errors (Boolean) — default: true

    When true, response errors are raised. See Seahorse::Client::Plugins::RaiseResponseErrors for more details.

  • :region (required, String)

    The AWS region to connect to. The region is used to construct the client endpoint. Defaults to ENV['AWS_REGION']. Also checks AMAZON_REGION and AWS_DEFAULT_REGION. See Aws::Plugins::RegionalEndpoint for more details.

  • :retry_limit (Integer) — default: 3

    The maximum number of times to retry failed requests. Only ~ 500 level server errors and certain ~ 400 level client errors are retried. Generally, these are throttling errors, data checksum errors, networking errors, timeout errors and auth errors from expired credentials. See Aws::Plugins::RetryErrors for more details.

  • :secret_access_key (String)

    Used to set credentials statically. See Aws::Plugins::RequestSigner for more details.

  • :session_token (String)

    Used to set credentials statically. See Aws::Plugins::RequestSigner for more details.

  • :ssl_ca_bundle (String)

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :ssl_ca_directory (String)

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :ssl_ca_store (String)

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :ssl_verify_peer (Boolean) — default: true

    See Seahorse::Client::Plugins::NetHttp for more details.

  • :stub_responses (Boolean) — default: false

    Causes the client to return stubbed responses. By default fake responses are generated and returned. You can specify the response data to return or errors to raise by calling ClientStubs#stub_responses. See ClientStubs for more information.

    Please note When response stubbing is enabled, no HTTP requests are made, and retries are disabled. See Aws::Plugins::StubResponses for more details.

  • :validate_params (Boolean) — default: true

    When true, request parameters are validated before sending the request. See Aws::Plugins::ParamValidator for more details.

Instance Method Details

#delete_lexicon(options = {}) ⇒ Struct

Deletes the specified pronunciation lexicon stored in an AWS Region. A lexicon which has been deleted is not available for speech synthesis, nor is it possible to retrieve it using either the GetLexicon or ListLexicon APIs.

For more information, see Managing Lexicons.

Examples:

Example: To delete a lexicon


# Deletes a specified pronunciation lexicon stored in an AWS Region.

resp = client.delete_lexicon({
  name: "example", 
})

# resp.to_h outputs the following:
{
}

Request syntax with placeholder values


resp = client.delete_lexicon({
  name: "LexiconName", # required
})

Options Hash (options):

  • :name (required, String)

    The name of the lexicon to delete. Must be an existing lexicon in the region.

Returns:

  • (Struct)

    Returns an empty response.

See Also:

#describe_voices(options = {}) ⇒ Types::DescribeVoicesOutput

Returns the list of voices that are available for use when requesting speech synthesis. Each voice speaks a specified language, is either male or female, and is identified by an ID, which is the ASCII version of the voice name.

When synthesizing speech ( SynthesizeSpeech ), you provide the voice ID for the voice you want from the list of voices returned by DescribeVoices.

For example, you want your news reader application to read news in a specific language, but giving a user the option to choose the voice. Using the DescribeVoices operation you can provide the user with a list of available voices to select from.

You can optionally specify a language code to filter the available voices. For example, if you specify en-US, the operation returns a list of all available US English voices.

This operation requires permissions to perform the polly:DescribeVoices action.

Examples:

Example: To describe available voices


# Returns the list of voices that are available for use when requesting speech synthesis. Displayed languages are those within the specified language code. If no language code is specified, voices for all available languages are displayed.

resp = client.describe_voices({
  language_code: "en-GB", 
})

# resp.to_h outputs the following:
{
  voices: [
    {
      gender: "Female", 
      id: "Emma", 
      language_code: "en-GB", 
      language_name: "British English", 
      name: "Emma", 
    }, 
    {
      gender: "Male", 
      id: "Brian", 
      language_code: "en-GB", 
      language_name: "British English", 
      name: "Brian", 
    }, 
    {
      gender: "Female", 
      id: "Amy", 
      language_code: "en-GB", 
      language_name: "British English", 
      name: "Amy", 
    }, 
  ], 
}

Request syntax with placeholder values


resp = client.describe_voices({
  language_code: "cy-GB", # accepts cy-GB, da-DK, de-DE, en-AU, en-GB, en-GB-WLS, en-IN, en-US, es-ES, es-US, fr-CA, fr-FR, is-IS, it-IT, ja-JP, nb-NO, nl-NL, pl-PL, pt-BR, pt-PT, ro-RO, ru-RU, sv-SE, tr-TR
  next_token: "NextToken",
})

Response structure


resp.voices #=> Array
resp.voices[0].gender #=> String, one of "Female", "Male"
resp.voices[0].id #=> String, one of "Geraint", "Gwyneth", "Mads", "Naja", "Hans", "Marlene", "Nicole", "Russell", "Amy", "Brian", "Emma", "Raveena", "Ivy", "Joanna", "Joey", "Justin", "Kendra", "Kimberly", "Matthew", "Salli", "Conchita", "Enrique", "Miguel", "Penelope", "Chantal", "Celine", "Mathieu", "Dora", "Karl", "Carla", "Giorgio", "Mizuki", "Liv", "Lotte", "Ruben", "Ewa", "Jacek", "Jan", "Maja", "Ricardo", "Vitoria", "Cristiano", "Ines", "Carmen", "Maxim", "Tatyana", "Astrid", "Filiz", "Vicki", "Takumi"
resp.voices[0].language_code #=> String, one of "cy-GB", "da-DK", "de-DE", "en-AU", "en-GB", "en-GB-WLS", "en-IN", "en-US", "es-ES", "es-US", "fr-CA", "fr-FR", "is-IS", "it-IT", "ja-JP", "nb-NO", "nl-NL", "pl-PL", "pt-BR", "pt-PT", "ro-RO", "ru-RU", "sv-SE", "tr-TR"
resp.voices[0].language_name #=> String
resp.voices[0].name #=> String
resp.next_token #=> String

Options Hash (options):

  • :language_code (String)

    The language identification tag (ISO 639 code for the language name-ISO 3166 country code) for filtering the list of voices returned. If you don\'t specify this optional parameter, all available voices are returned.

  • :next_token (String)

    An opaque pagination token returned from the previous DescribeVoices operation. If present, this indicates where to continue the listing.

Returns:

See Also:

#get_lexicon(options = {}) ⇒ Types::GetLexiconOutput

Returns the content of the specified pronunciation lexicon stored in an AWS Region. For more information, see Managing Lexicons.

Examples:

Example: To retrieve a lexicon


# Returns the content of the specified pronunciation lexicon stored in an AWS Region.

resp = client.get_lexicon({
  name: "", 
})

# resp.to_h outputs the following:
{
  lexicon: {
    content: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<lexicon version=\"1.0\" \r\n      xmlns=\"http://www.w3.org/2005/01/pronunciation-lexicon\"\r\n      xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n      xsi:schemaLocation=\"http://www.w3.org/2005/01/pronunciation-lexicon \r\n        http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd\"\r\n      alphabet=\"ipa\" \r\n      xml:lang=\"en-US\">\r\n  <lexeme>\r\n    <grapheme>W3C</grapheme>\r\n    <alias>World Wide Web Consortium</alias>\r\n  </lexeme>\r\n</lexicon>", 
    name: "example", 
  }, 
  lexicon_attributes: {
    alphabet: "ipa", 
    language_code: "en-US", 
    last_modified: Time.parse(1478542980.117), 
    lexemes_count: 1, 
    lexicon_arn: "arn:aws:polly:us-east-1:123456789012:lexicon/example", 
    size: 503, 
  }, 
}

Request syntax with placeholder values


resp = client.get_lexicon({
  name: "LexiconName", # required
})

Response structure


resp.lexicon.content #=> String
resp.lexicon.name #=> String
resp.lexicon_attributes.alphabet #=> String
resp.lexicon_attributes.language_code #=> String, one of "cy-GB", "da-DK", "de-DE", "en-AU", "en-GB", "en-GB-WLS", "en-IN", "en-US", "es-ES", "es-US", "fr-CA", "fr-FR", "is-IS", "it-IT", "ja-JP", "nb-NO", "nl-NL", "pl-PL", "pt-BR", "pt-PT", "ro-RO", "ru-RU", "sv-SE", "tr-TR"
resp.lexicon_attributes.last_modified #=> Time
resp.lexicon_attributes.lexicon_arn #=> String
resp.lexicon_attributes.lexemes_count #=> Integer
resp.lexicon_attributes.size #=> Integer

Options Hash (options):

  • :name (required, String)

    Name of the lexicon.

Returns:

See Also:

#list_lexicons(options = {}) ⇒ Types::ListLexiconsOutput

Returns a list of pronunciation lexicons stored in an AWS Region. For more information, see Managing Lexicons.

Examples:

Example: To list all lexicons in a region


# Returns a list of pronunciation lexicons stored in an AWS Region.

resp = client.list_lexicons({
})

# resp.to_h outputs the following:
{
  lexicons: [
    {
      attributes: {
        alphabet: "ipa", 
        language_code: "en-US", 
        last_modified: Time.parse(1478542980.117), 
        lexemes_count: 1, 
        lexicon_arn: "arn:aws:polly:us-east-1:123456789012:lexicon/example", 
        size: 503, 
      }, 
      name: "example", 
    }, 
  ], 
}

Request syntax with placeholder values


resp = client.list_lexicons({
  next_token: "NextToken",
})

Response structure


resp.lexicons #=> Array
resp.lexicons[0].name #=> String
resp.lexicons[0].attributes.alphabet #=> String
resp.lexicons[0].attributes.language_code #=> String, one of "cy-GB", "da-DK", "de-DE", "en-AU", "en-GB", "en-GB-WLS", "en-IN", "en-US", "es-ES", "es-US", "fr-CA", "fr-FR", "is-IS", "it-IT", "ja-JP", "nb-NO", "nl-NL", "pl-PL", "pt-BR", "pt-PT", "ro-RO", "ru-RU", "sv-SE", "tr-TR"
resp.lexicons[0].attributes.last_modified #=> Time
resp.lexicons[0].attributes.lexicon_arn #=> String
resp.lexicons[0].attributes.lexemes_count #=> Integer
resp.lexicons[0].attributes.size #=> Integer
resp.next_token #=> String

Options Hash (options):

  • :next_token (String)

    An opaque pagination token returned from previous ListLexicons operation. If present, indicates where to continue the list of lexicons.

Returns:

See Also:

#put_lexicon(options = {}) ⇒ Struct

Stores a pronunciation lexicon in an AWS Region. If a lexicon with the same name already exists in the region, it is overwritten by the new lexicon. Lexicon operations have eventual consistency, therefore, it might take some time before the lexicon is available to the SynthesizeSpeech operation.

For more information, see Managing Lexicons.

Examples:

Example: To save a lexicon


# Stores a pronunciation lexicon in an AWS Region.

resp = client.put_lexicon({
  content: "file://example.pls", 
  name: "W3C", 
})

# resp.to_h outputs the following:
{
}

Request syntax with placeholder values


resp = client.put_lexicon({
  name: "LexiconName", # required
  content: "LexiconContent", # required
})

Options Hash (options):

  • :name (required, String)

    Name of the lexicon. The name must follow the regular express format [0-9A-Za-z]`1,20`. That is, the name is a case-sensitive alphanumeric string up to 20 characters long.

  • :content (required, String)

    Content of the PLS lexicon as string data.

Returns:

  • (Struct)

    Returns an empty response.

See Also:

#synthesize_speech(options = {}) ⇒ Types::SynthesizeSpeechOutput

Synthesizes UTF-8 input, plain text or SSML, to a stream of bytes. SSML input must be valid, well-formed SSML. Some alphabets might not be available with all the voices (for example, Cyrillic might not be read at all by English voices) unless phoneme mapping is used. For more information, see How it Works.

Examples:

Example: To synthesize speech


# Synthesizes plain text or SSML into a file of human-like speech.

resp = client.synthesize_speech({
  lexicon_names: [
    "example", 
  ], 
  output_format: "mp3", 
  sample_rate: "8000", 
  text: "All Gaul is divided into three parts", 
  text_type: "text", 
  voice_id: "Joanna", 
})

# resp.to_h outputs the following:
{
  audio_stream: "TEXT", 
  content_type: "audio/mpeg", 
  request_characters: 37, 
}

Request syntax with placeholder values


resp = client.synthesize_speech({
  response_target: "/path/to/file", # where to write response data, file path, or IO object
  lexicon_names: ["LexiconName"],
  output_format: "json", # required, accepts json, mp3, ogg_vorbis, pcm
  sample_rate: "SampleRate",
  speech_mark_types: ["sentence"], # accepts sentence, ssml, viseme, word
  text: "Text", # required
  text_type: "ssml", # accepts ssml, text
  voice_id: "Geraint", # required, accepts Geraint, Gwyneth, Mads, Naja, Hans, Marlene, Nicole, Russell, Amy, Brian, Emma, Raveena, Ivy, Joanna, Joey, Justin, Kendra, Kimberly, Matthew, Salli, Conchita, Enrique, Miguel, Penelope, Chantal, Celine, Mathieu, Dora, Karl, Carla, Giorgio, Mizuki, Liv, Lotte, Ruben, Ewa, Jacek, Jan, Maja, Ricardo, Vitoria, Cristiano, Ines, Carmen, Maxim, Tatyana, Astrid, Filiz, Vicki, Takumi
})

Response structure


resp.audio_stream #=> IO
resp.content_type #=> String
resp.request_characters #=> Integer

Options Hash (options):

  • :response_target (IO, String)

    Specifies where to stream response data. You can provide the path where a file will be created on disk, or you can provide an IO object. If omitted, the response data will be loaded into memory and written to a StringIO object.

  • :lexicon_names (Array<String>)

    List of one or more pronunciation lexicon names you want the service to apply during synthesis. Lexicons are applied only if the language of the lexicon is the same as the language of the voice. For information about storing lexicons, see PutLexicon.

  • :output_format (required, String)

    The format in which the returned output will be encoded. For audio stream, this will be mp3, ogg_vorbis, or pcm. For speech marks, this will be json.

  • :sample_rate (String)

    The audio frequency specified in Hz.

    The valid values for mp3 and ogg_vorbis are \"8000\", \"16000\", and \"22050\". The default value is \"22050\".

    Valid values for pcm are \"8000\" and \"16000\" The default value is \"16000\".

  • :speech_mark_types (Array<String>)

    The type of speech marks returned for the input text.

  • :text (required, String)

    Input text to synthesize. If you specify ssml as the TextType, follow the SSML format for the input text.

  • :text_type (String)

    Specifies whether the input text is plain text or SSML. The default value is plain text. For more information, see Using SSML.

  • :voice_id (required, String)

    Voice ID to use for the synthesis. You can get a list of available voice IDs by calling the DescribeVoices operation.

Returns:

See Also:

#wait_until(waiter_name, params = {}) {|waiter| ... } ⇒ Boolean

Waiters polls an API operation until a resource enters a desired state.

Basic Usage

Waiters will poll until they are succesful, they fail by entering a terminal state, or until a maximum number of attempts are made.

# polls in a loop, sleeping between attempts client.waiter_until(waiter_name, params)

Configuration

You can configure the maximum number of polling attempts, and the delay (in seconds) between each polling attempt. You configure waiters by passing a block to #wait_until:

# poll for ~25 seconds
client.wait_until(...) do |w|
  w.max_attempts = 5
  w.delay = 5
end

Callbacks

You can be notified before each polling attempt and before each delay. If you throw :success or :failure from these callbacks, it will terminate the waiter.

started_at = Time.now
client.wait_until(...) do |w|

  # disable max attempts
  w.max_attempts = nil

  # poll for 1 hour, instead of a number of attempts
  w.before_wait do |attempts, response|
    throw :failure if Time.now - started_at > 3600
  end

end

Handling Errors

When a waiter is successful, it returns true. When a waiter fails, it raises an error. All errors raised extend from Waiters::Errors::WaiterFailed.

begin
  client.wait_until(...)
rescue Aws::Waiters::Errors::WaiterFailed
  # resource did not enter the desired state in time
end

Parameters:

  • waiter_name (Symbol)

    The name of the waiter. See #waiter_names for a full list of supported waiters.

  • params (Hash) (defaults to: {})

    Additional request parameters. See the #waiter_names for a list of supported waiters and what request they call. The called request determines the list of accepted parameters.

Yield Parameters:

Returns:

  • (Boolean)

    Returns true if the waiter was successful.

Raises:

  • (Errors::FailureStateError)

    Raised when the waiter terminates because the waiter has entered a state that it will not transition out of, preventing success.

  • (Errors::TooManyAttemptsError)

    Raised when the configured maximum number of attempts have been made, and the waiter is not yet successful.

  • (Errors::UnexpectedError)

    Raised when an error is encounted while polling for a resource that is not expected.

  • (Errors::NoSuchWaiterError)

    Raised when you request to wait for an unknown state.

#waiter_namesArray<Symbol>

Returns the list of supported waiters. The following table lists the supported waiters and the client method they call:

Waiter NameClient MethodDefault Delay:Default Max Attempts:

Returns:

  • (Array<Symbol>)

    the list of supported waiters.