Search in sources :

Example 1 with S3QueryStringSigner

use of com.amazonaws.services.s3.internal.S3QueryStringSigner in project aws-sdk-android by aws-amplify.

the class AmazonS3Client method presignRequest.

/**
 * Pre-signs the specified request, using a signature query-string
 * parameter.
 *
 * @param request The request to sign.
 * @param methodName The HTTP method (GET, PUT, DELETE, HEAD) for the
 *            specified request.
 * @param bucketName The name of the bucket involved in the request. If the
 *            request is not an operation on a bucket this parameter should
 *            be null.
 * @param key The object key involved in the request. If the request is not
 *            an operation on an object, this parameter should be null.
 * @param expiration The time at which the signed request is no longer
 *            valid, and will stop working.
 * @param subResource The optional sub-resource being requested as part of
 *            the request (e.g. "location", "acl", "logging", or "torrent").
 */
protected <T> void presignRequest(Request<T> request, HttpMethod methodName, String bucketName, String key, Date expiration, String subResource) {
    // Run any additional request handlers if present
    beforeRequest(request);
    String resourcePath = "/" + ((bucketName != null) ? bucketName + "/" : "") + ((key != null) ? key : "") + ((subResource != null) ? "?" + subResource : "");
    // Make sure the resource-path for signing does not contain
    // any consecutive "/"s.
    // Note that we should also follow the same rule to escape
    // consecutive "/"s when generating the presigned URL.
    // See ServiceUtils#convertRequestToUrl(...)
    resourcePath = resourcePath.replaceAll("(?<=/)/", "%2F");
    AWSCredentials credentials = awsCredentialsProvider.getCredentials();
    final AmazonWebServiceRequest originalRequest = request.getOriginalRequest();
    if (originalRequest != null && originalRequest.getRequestCredentials() != null) {
        credentials = originalRequest.getRequestCredentials();
    }
    new S3QueryStringSigner(methodName.toString(), resourcePath, expiration).sign(request, credentials);
    // with the pre-signed URL when it's sent back to Amazon S3.
    if (request.getHeaders().containsKey(Headers.SECURITY_TOKEN)) {
        final String value = request.getHeaders().get(Headers.SECURITY_TOKEN);
        request.addParameter(Headers.SECURITY_TOKEN, value);
        request.getHeaders().remove(Headers.SECURITY_TOKEN);
    }
}
Also used : S3QueryStringSigner(com.amazonaws.services.s3.internal.S3QueryStringSigner) AWSCredentials(com.amazonaws.auth.AWSCredentials) AmazonWebServiceRequest(com.amazonaws.AmazonWebServiceRequest)

Example 2 with S3QueryStringSigner

use of com.amazonaws.services.s3.internal.S3QueryStringSigner in project aws-sdk-android by aws-amplify.

the class S3QueryStringSignerTest method testSign.

@Test
public void testSign() throws URISyntaxException {
    final S3QueryStringSigner signer = new S3QueryStringSigner("GET", "/test-bucket123456/TestFile.txt", new Date(1431377237312L));
    final GetObjectRequest gr = new GetObjectRequest("test-bucket123456", "TestFile.txt");
    final Request<?> req = new DefaultRequest(gr, Constants.S3_SERVICE_DISPLAY_NAME);
    req.setHttpMethod(HttpMethodName.GET);
    req.setResourcePath("TestFile.txt");
    req.setEndpoint(new URI("https://test-bucket123456.s3-us-west-2.amazonaws.com"));
    signer.sign(req, new BasicAWSCredentials("AKIAJD11BOGUS11ACCESS11KEYXMGOZQ", "LYd/ZD4F11BOGUS11SECRET11KEYOM1SiD6"));
    assertEquals("https://test-bucket123456.s3-us-west-2.amazonaws.com/TestFile.txt?AWSAccessKeyId=AKIAJD11BOGUS11ACCESS11KEYXMGOZQ&Expires=1431377237&Signature=vtILMLWYdEalygpKGFv%2FqvP5qp8%3D", ServiceUtils.convertRequestToUrl(req, true).toString());
}
Also used : DefaultRequest(com.amazonaws.DefaultRequest) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) URI(java.net.URI) Date(java.util.Date) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Test(org.junit.Test)

Aggregations

AmazonWebServiceRequest (com.amazonaws.AmazonWebServiceRequest)1 DefaultRequest (com.amazonaws.DefaultRequest)1 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 S3QueryStringSigner (com.amazonaws.services.s3.internal.S3QueryStringSigner)1 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)1 URI (java.net.URI)1 Date (java.util.Date)1 Test (org.junit.Test)1