Search in sources :

Example 1 with Presigner

use of com.amazonaws.auth.Presigner in project aws-sdk-android by aws-amplify.

the class AmazonPollyPresigningClient method getPresignedSynthesizeSpeechUrl.

/**
 * <p>
 * Returns a pre-signed URL for accessing an Amazon Polly resource.
 * </p>
 * <p>
 * Pre-signed URLs allow clients to form a URL for an Amazon Polly
 * resource, and then sign it with the current AWS security credentials.
 * The pre-signed URL can be shared to other users, allowing access to
 * the resource without providing an account's AWS security credentials.
 * </p>
 * <p>
 * Pre-signed URLs are useful in many situations where AWS security
 * credentials aren't available from the client that needs to make the
 * actual request to Amazon Polly.
 * </p>
 * <p>
 * For example, a pre-signed URL to GET the synthesized speech audio stream
 * using the owner's AWS account can be generated and passed to a system
 * media player.
 * </p>
 *
 * @param synthesizeSpeechPresignRequest Object providing pre-signed synthesize speech
 *              request parameters.
 * @return Pre-signed URL of audio stream containing synthesized speech
 *              matching parameters provided in synthesizeSpeechPresignRequest
 *              that is possible to obtain using HTTP GET method.
 */
public URL getPresignedSynthesizeSpeechUrl(SynthesizeSpeechPresignRequest synthesizeSpeechPresignRequest) {
    Request<PresigningRequest> request = new SynthesizeSpeechPresignRequestMarshaller().marshall(synthesizeSpeechPresignRequest);
    request.setEndpoint(endpoint);
    request.setTimeOffset(timeOffset);
    if (synthesizeSpeechPresignRequest.getExpiration() == null) {
        synthesizeSpeechPresignRequest.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * DEFAULT_GET_REQUEST_EXPIRATION_MINUTES));
    }
    Signer signer = getSignerByURI(endpoint);
    if (!(signer instanceof Presigner)) {
        throw new AmazonClientException("Unsupported signer");
    }
    Presigner presigner = (Presigner) signer;
    Date expirationDate = synthesizeSpeechPresignRequest.getExpiration();
    AWSCredentials credentials = synthesizeSpeechPresignRequest.getRequestCredentials();
    if (credentials == null) {
        credentials = awsCredentialsProvider.getCredentials();
    }
    presigner.presignRequest(request, credentials, expirationDate);
    // Remove the leading slash (if any) in the resource-path
    return ServiceUtils.convertRequestToUrl(request, true);
}
Also used : Signer(com.amazonaws.auth.Signer) AmazonClientException(com.amazonaws.AmazonClientException) SynthesizeSpeechPresignRequestMarshaller(com.amazonaws.services.polly.model.transform.SynthesizeSpeechPresignRequestMarshaller) PresigningRequest(com.amazonaws.services.polly.internal.PresigningRequest) AWSCredentials(com.amazonaws.auth.AWSCredentials) Date(java.util.Date) Presigner(com.amazonaws.auth.Presigner) AmazonPollyCustomPresigner(com.amazonaws.services.polly.internal.AmazonPollyCustomPresigner)

Example 2 with Presigner

use of com.amazonaws.auth.Presigner in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method generatePresignedUrl.

/*
     * (non-Javadoc)
     * @see
     * com.amazonaws.services.s3.AmazonS3#generatePresignedUrl(com.amazonaws
     * .services.s3.model.GeneratePresignedUrlRequest)
     */
@Override
public URL generatePresignedUrl(GeneratePresignedUrlRequest generatePresignedUrlRequest) throws AmazonClientException {
    assertParameterNotNull(generatePresignedUrlRequest, "The request parameter must be specified when generating a pre-signed URL");
    final String bucketName = generatePresignedUrlRequest.getBucketName();
    final String key = generatePresignedUrlRequest.getKey();
    assertParameterNotNull(bucketName, "The bucket name parameter must be specified when generating a pre-signed URL");
    assertParameterNotNull(generatePresignedUrlRequest.getMethod(), "The HTTP method request parameter must be specified when generating a pre-signed URL");
    if (generatePresignedUrlRequest.getExpiration() == null) {
        generatePresignedUrlRequest.setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 15));
    }
    final HttpMethodName httpMethod = HttpMethodName.valueOf(generatePresignedUrlRequest.getMethod().toString());
    // If the key starts with a slash character itself, the following method
    // will actually add another slash before the resource path to prevent
    // the HttpClient mistakenly treating the slash as a path delimiter.
    // For presigned request, we need to remember to remove this extra slash
    // before generating the URL.
    final Request<GeneratePresignedUrlRequest> request = createRequest(bucketName, key, generatePresignedUrlRequest, httpMethod);
    addParameterIfNotNull(request, "versionId", generatePresignedUrlRequest.getVersionId());
    if (generatePresignedUrlRequest.isZeroByteContent()) {
        request.setContent(new ByteArrayInputStream(new byte[0]));
    }
    for (final Entry<String, String> entry : generatePresignedUrlRequest.getRequestParameters().entrySet()) {
        request.addParameter(entry.getKey(), entry.getValue());
    }
    if (generatePresignedUrlRequest.getContentType() != null) {
        request.addHeader(Headers.CONTENT_TYPE, generatePresignedUrlRequest.getContentType());
    }
    if (generatePresignedUrlRequest.getContentMd5() != null) {
        request.addHeader(Headers.CONTENT_MD5, generatePresignedUrlRequest.getContentMd5());
    }
    // SSE-C
    populateSSE_C(request, generatePresignedUrlRequest.getSSECustomerKey());
    // SSE
    addHeaderIfNotNull(request, Headers.SERVER_SIDE_ENCRYPTION, generatePresignedUrlRequest.getSSEAlgorithm());
    // SSE-KMS
    addHeaderIfNotNull(request, Headers.SERVER_SIDE_ENCRYPTION_KMS_KEY_ID, generatePresignedUrlRequest.getKmsCmkId());
    // Add custom query parameters
    final Map<String, String> customQueryParameters = generatePresignedUrlRequest.getCustomQueryParameters();
    if (customQueryParameters != null) {
        for (Map.Entry<String, String> e : customQueryParameters.entrySet()) {
            request.addParameter(e.getKey(), e.getValue());
        }
    }
    addResponseHeaderParameters(request, generatePresignedUrlRequest.getResponseHeaders());
    final Signer signer = createSigner(request, bucketName, key);
    if (signer instanceof Presigner) {
        // If we have a signer which knows how to presign requests,
        // delegate directly to it.
        ((Presigner) signer).presignRequest(request, awsCredentialsProvider.getCredentials(), generatePresignedUrlRequest.getExpiration());
    } else {
        // Otherwise use the default presigning method, which is hardcoded
        // to use QueryStringSigner.
        presignRequest(request, generatePresignedUrlRequest.getMethod(), bucketName, key, generatePresignedUrlRequest.getExpiration(), null);
    }
    // Remove the leading slash (if any) in the resource-path
    return ServiceUtils.convertRequestToUrl(request, true);
}
Also used : HttpMethodName(com.amazonaws.http.HttpMethodName) S3Signer(com.amazonaws.services.s3.internal.S3Signer) S3QueryStringSigner(com.amazonaws.services.s3.internal.S3QueryStringSigner) AWSS3V4Signer(com.amazonaws.services.s3.internal.AWSS3V4Signer) Signer(com.amazonaws.auth.Signer) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Date(java.util.Date) Presigner(com.amazonaws.auth.Presigner)

Aggregations

Presigner (com.amazonaws.auth.Presigner)2 Signer (com.amazonaws.auth.Signer)2 Date (java.util.Date)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 HttpMethodName (com.amazonaws.http.HttpMethodName)1 AmazonPollyCustomPresigner (com.amazonaws.services.polly.internal.AmazonPollyCustomPresigner)1 PresigningRequest (com.amazonaws.services.polly.internal.PresigningRequest)1 SynthesizeSpeechPresignRequestMarshaller (com.amazonaws.services.polly.model.transform.SynthesizeSpeechPresignRequestMarshaller)1 AWSS3V4Signer (com.amazonaws.services.s3.internal.AWSS3V4Signer)1 S3QueryStringSigner (com.amazonaws.services.s3.internal.S3QueryStringSigner)1 S3Signer (com.amazonaws.services.s3.internal.S3Signer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1