software.amazon.awssdk.services.s3.presigner.S3Presigner Java Examples

The following examples show how to use software.amazon.awssdk.services.s3.presigner.S3Presigner. 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 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 #3
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 #4
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 #5
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 #6
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void putObject_EndpointOverrideIsIncludedInPresignedUrl() {
    S3Presigner presigner = presignerBuilder().endpointOverride(URI.create("http://foo.com")).build();
    PresignedPutObjectRequest presigned =
        presigner.presignPutObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .putObjectRequest(go -> go.bucket("foo34343434")
                                                                   .key("bar")));

    assertThat(presigned.url().toString()).startsWith("http://foo.com/foo34343434/bar?");
    assertThat(presigned.isBrowserExecutable()).isFalse();
    assertThat(presigned.signedHeaders().get("host")).containsExactly("foo.com");
    assertThat(presigned.signedPayload()).isEmpty();
}
 
Example #7
Source File: GeneratePresignedUrlAndUploadObject.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        if (args.length < 2) {
            System.out.println("Please specify a bucket name and a key name");
            System.exit(1);
        }

        String bucketName = args[0];
        String keyName = args[1];

        // Create a S3Presigner by using the default AWS Region and credentials
        S3Presigner presigner = S3Presigner.create();
        signBucket(presigner, bucketName, keyName);
    }
 
Example #8
Source File: GetObjectPresignedUrl.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Please specify a bucket name and key name");
        System.exit(1);
    }

    String bucketName = args[0];
    String keyName = args[1];

    // Create an S3Presigner by using the default AWS Region and credentials
    S3Presigner presigner = S3Presigner.create();
    getPresignedUrl(presigner, bucketName,keyName);

}
 
Example #9
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void accessPointArn_differentRegion_useArnRegionFalse_throwsIllegalArgumentException() throws Exception {
    String accessPointArn = "arn:aws:s3:us-east-1:12345678910:accesspoint:foobar";

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

    assertThatThrownBy(() -> presigner.presignGetObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                    .getObjectRequest(go -> go.bucket(accessPointArn).key("bar"))))
            .isInstanceOf(IllegalArgumentException.class)
            .hasMessageContaining("region");
}
 
Example #10
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 #11
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 #12
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void putObject_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();

    PresignedPutObjectRequest presignedWithClientCredentials =
        presigner.presignPutObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .putObjectRequest(go -> go.bucket("foo34343434")
                                                                   .key("bar")));

    PresignedPutObjectRequest presignedWithRequestCredentials =
        presigner.presignPutObject(r -> r.signatureDuration(Duration.ofMinutes(5))
                                         .putObjectRequest(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 #13
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 #14
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 #15
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private S3Presigner generateMaximal() {
    return S3Presigner.builder()
                      .serviceConfiguration(S3Configuration.builder()
                            .checksumValidationEnabled(false)
                            .build())
                      .credentialsProvider(() -> AwsBasicCredentials.create("x", "x"))
                      .region(Region.US_EAST_1)
                      .endpointOverride(FAKE_URL)
                      .build();
}
 
Example #16
Source File: InvalidRegionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void invalidS3PresignerArnRegionAtRequestGivesHelpfulMessage() {
    S3Presigner presigner = S3Presigner.builder()
                                       .region(Region.of("us-east-1"))
                                       .credentialsProvider(AnonymousCredentialsProvider.create())
                                       .serviceConfiguration(S3Configuration.builder().useArnRegionEnabled(true).build())
                                       .build();

    String arn = "arn:aws:s3:US_EAST_1:123456789012:accesspoint/test";
    assertThatThrownBy(() -> presigner.presignGetObject(r -> r.getObjectRequest(g -> g.bucket(arn).key("test"))
                                                              .signatureDuration(Duration.ofMinutes(15))))
        .isInstanceOf(SdkClientException.class)
        .hasMessageContaining("US_EAST_1")
        .hasMessageContaining("region");
}
 
Example #17
Source File: InvalidRegionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void invalidS3PresignerRegionAtClientGivesHelpfulMessage() {
    assertThatThrownBy(() -> S3Presigner.builder().region(Region.of("US_EAST_1")).build())
        .isInstanceOf(SdkClientException.class)
        .hasMessageContaining("US_EAST_1")
        .hasMessageContaining("region")
        .hasMessageContaining("us-east-1");
}
 
Example #18
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private S3Presigner generateMinimal() {
    return S3Presigner.builder()
                      .credentialsProvider(() -> AwsBasicCredentials.create("x", "x"))
                      .region(Region.US_EAST_1)
                      .build();
}
 
Example #19
Source File: S3PresignerTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private S3Presigner.Builder presignerBuilder() {
    return S3Presigner.builder()
                      .region(Region.US_WEST_2)
                      .credentialsProvider(() -> AwsBasicCredentials.create("x", "x"));
}
 
Example #20
Source File: S3PresignerIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUpInstance() {
    this.presigner = S3Presigner.create();
}
 
Example #21
Source File: DefaultS3Presigner.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
@Override
public S3Presigner build() {
    return new DefaultS3Presigner(this);
}
 
Example #22
Source File: DefaultS3Presigner.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
public static S3Presigner.Builder builder() {
    return new Builder();
}
 
Example #23
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();
}