Java Code Examples for software.amazon.awssdk.services.s3.presigner.S3Presigner#presignGetObject()

The following examples show how to use software.amazon.awssdk.services.s3.presigner.S3Presigner#presignGetObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3Manager.java    From joyqueue with Apache License 2.0 9 votes vote down vote up
private String getS3Url(String objectKey) {
    AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKey, secretKey));
    S3Presigner preSigner = S3Presigner.builder()
            .credentialsProvider(credentialsProvider)
            .endpointOverride(URI.create(endpoint))
            .region(clientRegion).build();
    GetObjectRequest getObjectRequest = GetObjectRequest.builder()
            .bucket(bucketName)
            .key(objectKey)
            .build();
    GetObjectPresignRequest getObjectPresignRequest = GetObjectPresignRequest.builder()
            .getObjectRequest(getObjectRequest).signatureDuration(Duration.ofDays(7)).build();
    PresignedGetObjectRequest presignedGetObjectRequest = preSigner.presignGetObject(getObjectPresignRequest);
    String url = presignedGetObjectRequest.url().toString();
    preSigner.close();
    return url;
}
 
Example 2
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void getObject_S3ConfigurationCanBeOverriddenToLeverageTransferAcceleration() {
    S3Presigner presigner = presignerBuilder().serviceConfiguration(S3Configuration.builder()
                .accelerateModeEnabled(true)
                .build())
            .build();

    PresignedGetObjectRequest presignedRequest =
            presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                    .getObjectRequest(go -> go.bucket("foo34343434")
                            .key("bar")));


    System.out.println(presignedRequest.url());

    assertThat(presignedRequest.httpRequest().host()).contains(".s3-accelerate.");
}
 
Example 3
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Dualstack also supports path style endpoints just like the normal endpoints.
 */
@Test
public void dualstackAndPathStyleEnabled_UsesPathStyleAddressingWithDualstackEndpoint() throws Exception {
    S3Presigner presigner = presignerBuilder().serviceConfiguration(S3Configuration.builder()
            .dualstackEnabled(true)
            .pathStyleAccessEnabled(true)
            .build())
            .build();

    PresignedGetObjectRequest presignedRequest =
            presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                    .getObjectRequest(go -> go.bucket(BUCKET)
                            .key("bar")));

    assertThat(presignedRequest.httpRequest().host()).isEqualTo("s3.dualstack.us-west-2.amazonaws.com");
    assertThat(presignedRequest.url().toString()).startsWith(String.format("https://s3.dualstack.us-west-2.amazonaws.com/%s/%s?", BUCKET, "bar"));
}
 
Example 4
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * When dualstack and accelerate are both enabled there is a special, global dualstack endpoint we must use.
 */
@Test
public void dualstackAndAccelerateEnabled_UsesDualstackAccelerateEndpoint() throws Exception {
    S3Presigner presigner = presignerBuilder().serviceConfiguration(S3Configuration.builder()
            .dualstackEnabled(true)
            .accelerateModeEnabled(true)
            .build())
            .build();

    PresignedGetObjectRequest presignedRequest =
            presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                    .getObjectRequest(go -> go.bucket(BUCKET)
                            .key("bar")));

    assertThat(presignedRequest.httpRequest().host()).isEqualTo(String.format("%s.s3-accelerate.dualstack.amazonaws.com", BUCKET));
}
 
Example 5
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void accessPointArn_differentRegion_useArnRegionTrue() throws Exception {
    String customEndpoint = "https://foobar-12345678910.s3-accesspoint.us-west-2.amazonaws.com";
    String accessPointArn = "arn:aws:s3:us-west-2:12345678910:accesspoint:foobar";

    S3Presigner presigner = presignerBuilder().serviceConfiguration(S3Configuration.builder()
            .useArnRegionEnabled(true)
            .build())
            .build();

    PresignedGetObjectRequest presignedRequest =
            presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                    .getObjectRequest(go -> go.bucket(accessPointArn)
                            .key("bar")));

    assertThat(presignedRequest.url().toString()).startsWith(customEndpoint);
}
 
Example 6
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getObject_EndpointOverrideIsIncludedInPresignedUrl() {
    S3Presigner presigner = presignerBuilder().endpointOverride(URI.create("http://foo.com")).build();
    PresignedGetObjectRequest presigned =
        presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .getObjectRequest(go -> go.bucket("foo34343434")
                                                                   .key("bar")));

    assertThat(presigned.url().toString()).startsWith("http://foo.com/foo34343434/bar?");
    assertThat(presigned.isBrowserExecutable()).isTrue();
    assertThat(presigned.signedHeaders().get("host")).containsExactly("foo.com");
    assertThat(presigned.signedPayload()).isEmpty();
}
 
Example 7
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getObject_CredentialsCanBeOverriddenAtTheRequestLevel() {
    AwsCredentials clientCredentials = AwsBasicCredentials.create("a", "a");
    AwsCredentials requestCredentials = AwsBasicCredentials.create("b", "b");

    S3Presigner presigner = presignerBuilder().credentialsProvider(() -> clientCredentials).build();


    AwsRequestOverrideConfiguration overrideConfiguration =
        AwsRequestOverrideConfiguration.builder()
                                       .credentialsProvider(() -> requestCredentials)
                                       .build();

    PresignedGetObjectRequest presignedWithClientCredentials =
        presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .getObjectRequest(go -> go.bucket("foo34343434")
                                                                   .key("bar")));

    PresignedGetObjectRequest presignedWithRequestCredentials =
        presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .getObjectRequest(go -> go.bucket("foo34343434")
                                                                   .key("bar")
                                                                   .overrideConfiguration(overrideConfiguration)));

    System.out.println(presignedWithClientCredentials.url());

    assertThat(presignedWithClientCredentials.httpRequest().rawQueryParameters().get("X-Amz-Credential").get(0))
        .startsWith("a");
    assertThat(presignedWithRequestCredentials.httpRequest().rawQueryParameters().get("X-Amz-Credential").get(0))
        .startsWith("b");
}
 
Example 8
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void accelerateEnabled_UsesVirtualAddressingWithAccelerateEndpoint() {
    S3Presigner presigner = presignerBuilder().serviceConfiguration(S3Configuration.builder()
            .accelerateModeEnabled(true)
            .build())
            .build();

    PresignedGetObjectRequest presignedRequest =
            presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                    .getObjectRequest(go -> go.bucket(BUCKET)
                            .key("bar")));

    assertThat(presignedRequest.httpRequest().host()).isEqualTo(String.format("%s.s3-accelerate.amazonaws.com", BUCKET));
}
 
Example 9
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Dualstack uses regional endpoints that support virtual addressing.
 */
@Test
public void dualstackEnabled_UsesVirtualAddressingWithDualstackEndpoint() throws Exception {
    S3Presigner presigner = presignerBuilder().serviceConfiguration(S3Configuration.builder()
            .dualstackEnabled(true)
            .build())
            .build();

    PresignedGetObjectRequest presignedRequest =
            presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                    .getObjectRequest(go -> go.bucket(BUCKET)
                            .key("bar")));

    assertThat(presignedRequest.httpRequest().host()).contains(String.format("%s.s3.dualstack.us-west-2.amazonaws.com", BUCKET));
}
 
Example 10
Source File: S3PresignedGetObject.java    From piper with Apache License 2.0 3 votes vote down vote up
@Override
public Object handle (TaskExecution aTask) throws Exception {
  AmazonS3URI s3Uri = new AmazonS3URI(aTask.getRequiredString("uri"));
  
  S3Presigner presigner = S3Presigner.create();
  
  PresignedGetObjectRequest presignedRequest =
      presigner.presignGetObject(z -> z.signatureDuration(Duration.parse("PT"+aTask.getRequiredString("signatureDuration")))
              .getObjectRequest(por -> por.bucket(s3Uri.getBucket()).key(s3Uri.getKey())));
  
  
  return presignedRequest.url().toString();
}