Interface PollyPresigner

  • All Superinterfaces:
    AutoCloseable, SdkAutoCloseable, SdkPresigner
    All Known Implementing Classes:
    DefaultPollyPresigner

    public interface PollyPresigner
    extends SdkPresigner
    Enables signing a PollyRequest so that it can be executed without requiring any additional authentication on the part of the caller.

    Signature Duration

    Pre-signed requests are only valid for a finite period of time, referred to as the signature duration. This signature duration is configured when the request is generated, and cannot be longer than 7 days. Attempting to generate a signature longer than 7 days in the future will fail at generation time. Attempting to use a pre-signed request after the signature duration has passed will result in an access denied response from the service.

    Example Usage

     
         // Create a PollyPresigner using the default region and credentials.
         // This is usually done at application startup, because creating a presigner can be expensive.
         PollyPresigner presigner = PollyPresigner.create();
    
         // Create a SynthesizeSpeechRequest to be pre-signed
         SynthesizeSpeechRequest synthesizeSpeechRequest =
                 SynthesizeSpeechRequest.builder()
                                        .text("Hello Polly!")
                                        .voiceId(VoiceId.SALLI)
                                        .outputFormat(OutputFormat.PCM)
                                        .build();
    
         // Create a SynthesizeSpeechRequest to specify the signature duration
         SynthesizeSpeechPresignRequest synthesizeSpeechPresignRequest =
             SynthesizeSpeechPresignRequest.builder()
                                    .signatureDuration(Duration.ofMinutes(10))
                                    .synthesizeSpeechRequest(synthesizeSpeechRequest)
                                    .build();
    
         // Generate the presigned request
         PresignedSynthesizeSpeechRequest presignedSynthesizeSpeechRequest =
             presigner.presignSynthesizeSpeech(SynthesizeSpeechPresignRequest);
    
         // Log the presigned URL, for example.
         System.out.println("Presigned URL: " + presignedSynthesizeSpeechRequest.url());
    
         // It is recommended to close the presigner when it is done being used, because some credential
         // providers (e.g. if your AWS profile is configured to assume an STS role) require system resources
         // that need to be freed. If you are using one presigner per application (as recommended), this
         // usually is not needed.
         presigner.close();
     
     

    Browser Compatibility

    Some pre-signed requests can be executed by a web browser. These "browser compatible" pre-signed requests do not require the customer to send anything other than a "host" header when performing an HTTP GET against the pre-signed URL.

    Whether a pre-signed request is "browser compatible" can be determined by checking the PresignedRequest.isBrowserExecutable() flag. It is recommended to always check this flag when the pre-signed request needs to be executed by a browser, because some request fields will result in the pre-signed request not being browser-compatible.

    Executing a Pre-Signed Request from Java code

    Browser-compatible requests (see above) can be executed using a web browser. All pre-signed requests can be executed from Java code. This documentation describes two methods for executing a pre-signed request: (1) using the JDK's URLConnection class, (2) using an SDK synchronous SdkHttpClient class.

    Using {code URLConnection}:

         // Create a pre-signed request using one of the "presign" methods on PollyPresigner
         PresignedRequest presignedRequest = ...;
    
         // Create a JDK HttpURLConnection for communicating with Polly
         HttpURLConnection connection = (HttpURLConnection) presignedRequest.url().openConnection();
    
         // Specify any headers that are needed by the service (not needed when isBrowserExecutable is true)
         presignedRequest.httpRequest().headers().forEach((header, values) -> {
             values.forEach(value -> {
                 connection.addRequestProperty(header, value);
             });
         });
    
         // Download the result of executing the request
         try (InputStream content = connection.getInputStream()) {
             System.out.println("Service returned response: ");
             IoUtils.copy(content, myFileOutputstream);
         }