Java Code Examples for software.amazon.awssdk.services.s3.S3Client#listObjects()

The following examples show how to use software.amazon.awssdk.services.s3.S3Client#listObjects() . 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: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 7 votes vote down vote up
@Test
public void regionalSettingEnabled_usesRegionalIadEndpoint() throws UnsupportedEncodingException {
    EnvironmentVariableHelper environmentVariableHelper = new EnvironmentVariableHelper();
    environmentVariableHelper.set(SdkSystemSetting.AWS_S3_US_EAST_1_REGIONAL_ENDPOINT.environmentVariable(), "regional");

    mockHttpClient.stubNextResponse(mockListObjectsResponse());

    S3Client s3Client = S3Client.builder()
            .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")))
            .httpClient(mockHttpClient)
            .region(Region.US_EAST_1)
            .serviceConfiguration(S3Configuration.builder()
                    .pathStyleAccessEnabled(true)
                    .build())
            .build();
    try {
        s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());
        assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.us-east-1.amazonaws.com");
    } finally {
        environmentVariableHelper.reset();
    }
}
 
Example 2
Source File: ListObjects.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void listBucketObjects(S3Client s3, String bucketName ) {

       try {
            ListObjectsRequest listObjects = ListObjectsRequest
                    .builder()
                    .bucket(bucketName)
                    .build();

            ListObjectsResponse res = s3.listObjects(listObjects);
            List<S3Object> objects = res.contents();

            for (ListIterator iterVals = objects.listIterator(); iterVals.hasNext(); ) {
                S3Object myValue = (S3Object) iterVals.next();
                System.out.print("\n The name of the key is " + myValue.key());
                System.out.print("\n The object is " + calKb(myValue.size()) + " KBs");
                System.out.print("\n The owner is " + myValue.owner());
                }
        } catch (S3Exception e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
    }
 
Example 3
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void regionalSettingUnset_usesGlobalEndpoint() throws UnsupportedEncodingException {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());

    S3Client s3Client = S3Client.builder()
            .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")))
            .httpClient(mockHttpClient)
            .region(Region.US_EAST_1)
            .serviceConfiguration(S3Configuration.builder()
                    .pathStyleAccessEnabled(true)
                    .build())
            .build();

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());
    assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.amazonaws.com");
}
 
Example 4
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void regionalSettingDisabled_usesGlobalEndpoint() throws UnsupportedEncodingException {
    EnvironmentVariableHelper environmentVariableHelper = new EnvironmentVariableHelper();
    environmentVariableHelper.set(SdkSystemSetting.AWS_S3_US_EAST_1_REGIONAL_ENDPOINT.environmentVariable(), "nonregional");

    mockHttpClient.stubNextResponse(mockListObjectsResponse());

    S3Client s3Client = S3Client.builder()
            .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")))
            .httpClient(mockHttpClient)
            .region(Region.US_EAST_1)
            .serviceConfiguration(S3Configuration.builder()
                    .pathStyleAccessEnabled(true)
                    .build())
            .build();
    try {
        s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());
        assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.amazonaws.com");
    } finally {
        environmentVariableHelper.reset();
    }
}
 
Example 5
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void regionalSettingEnabledViaProfile_usesRegionalIadEndpoint() throws UnsupportedEncodingException {
    String profile =
        "[profile test]\n" +
        "s3_us_east_1_regional_endpoint = regional";

    ProfileFile profileFile = ProfileFile.builder()
                                         .content(new StringInputStream(profile))
                                         .type(ProfileFile.Type.CONFIGURATION)
                                         .build();

    mockHttpClient.stubNextResponse(mockListObjectsResponse());

    S3Client s3Client = S3Client.builder()
                                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", "skid")))
                                .httpClient(mockHttpClient)
                                .region(Region.US_EAST_1)
                                .overrideConfiguration(c -> c.defaultProfileFile(profileFile)
                                                             .defaultProfileName("test"))
                                .serviceConfiguration(c -> c.pathStyleAccessEnabled(true))
                                .build();

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());
    assertThat(mockHttpClient.getLastRequest().getUri().getHost()).isEqualTo("s3.us-east-1.amazonaws.com");
}
 
Example 6
Source File: S3EndpointResolutionTest.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 {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = buildClient(withDualstackEnabled());

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(),
                          String.format("https://%s.s3.dualstack.ap-south-1.amazonaws.com", BUCKET));
}
 
Example 7
Source File: S3ListObjects.java    From piper with Apache License 2.0 5 votes vote down vote up
@Override
public List<S3ObjectDescription> handle (TaskExecution aTask) throws Exception {
  
  S3Client s3 = S3Client.builder().build();
  
  ListObjectsResponse response = s3.listObjects(ListObjectsRequest.builder()
                                   .bucket(aTask.getRequiredString("bucket"))
                                   .prefix(aTask.getRequiredString("prefix"))
                                   .build());
  
  return response.contents()
                 .stream()
                 .map(o->new S3ObjectDescription(aTask.getRequiredString("bucket"),o))
                 .collect(Collectors.toList());
}
 
Example 8
Source File: VirtualHostAddressingSepTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void assertTestCase() throws UnsupportedEncodingException {
    final String bucket = testCaseModel.getBucket();
    final String expectedUri = testCaseModel.getExpectedUri();

    S3Client s3Client = constructClient(testCaseModel);

    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    s3Client.listObjects(ListObjectsRequest.builder().bucket(bucket).build());

    assertThat(mockHttpClient.getLastRequest().getUri())
        .isEqualTo(URI.create(expectedUri));
}
 
Example 9
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 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 {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = buildClient(withDualstackAndAccelerateEnabled());

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(),
                          String.format("https://%s.s3-accelerate.dualstack.amazonaws.com", BUCKET));
}
 
Example 10
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Dualstack also supports path style endpoints just like the normal endpoints.
 */
@Test
public void dualstackAndPathStyleEnabled_UsesPathStyleAddressingWithDualstackEndpoint() throws Exception {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = buildClient(withDualstackAndPathStyleEnabled());

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(), "https://s3.dualstack.ap-south-1.amazonaws.com/" + BUCKET);
}
 
Example 11
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void accessPointArn_correctlyRewritesEndpoint() throws Exception {
    URI customEndpoint = URI.create("https://foobar-12345678910.s3-accesspoint.ap-south-1.amazonaws.com");
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = clientBuilder().build();
    String accessPointArn = "arn:aws:s3:ap-south-1:12345678910:accesspoint:foobar";

    s3Client.listObjects(ListObjectsRequest.builder().bucket(accessPointArn).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(), customEndpoint.toString());
}
 
Example 12
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * S3 accelerate has a global endpoint, we use that when accelerate mode is enabled in the advanced configuration.
 */
@Test
public void accelerateEnabled_UsesVirtualAddressingWithAccelerateEndpoint() throws Exception {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = buildClient(withAccelerateEnabled());

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(),
                          String.format("https://%s.s3-accelerate.amazonaws.com", BUCKET));
}
 
Example 13
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * By default we use virtual addressing when possible.
 */
@Test
public void emptyServiceConfigurationProvided_UsesVirtualAddressingWithStandardEndpoint() throws Exception {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = buildClient(S3Configuration.builder().build());

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(), ENDPOINT_WITH_BUCKET);
}
 
Example 14
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * By default we use virtual addressing when possible.
 */
@Test
public void noServiceConfigurationProvided_UsesVirtualAddressingWithStandardEndpoint() throws Exception {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = buildClient(null);

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(), ENDPOINT_WITH_BUCKET);
}
 
Example 15
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * When path style is enabled in the advanced configuration we should always use it.
 */
@Test
public void pathStyleConfigured_UsesPathStyleAddressing() throws Exception {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = buildClient(withPathStyle());

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(), ENDPOINT_WITHOUT_BUCKET + "/" + BUCKET);
}
 
Example 16
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * In us-east-1 buckets can have non-DNS compliant names. For those buckets we must always use path style even when it
 * is disabled per the advanced configuration.
 */
@Test
public void pathStyleDisabled_NonDnsCompatibleBucket_StillUsesPathStyleAddressing() throws Exception {
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = buildClient(null);

    s3Client.listObjects(ListObjectsRequest.builder().bucket(NON_DNS_COMPATIBLE_BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(), ENDPOINT_WITHOUT_BUCKET + "/" + NON_DNS_COMPATIBLE_BUCKET);
}
 
Example 17
Source File: S3TogglzConfiguration.java    From edison-microservice with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(name = "edison.togglz.s3.check-bucket", havingValue = "true")
public Boolean checkBucketAvailability(final S3Client s3Client, final TogglzProperties togglzProperties){

    ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder()
            .bucket(togglzProperties.getS3().getBucketName())
            .build();
    //throws exception on missing bucket
    s3Client.listObjects(listObjectsRequest);

    return true;
}
 
Example 18
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * If a custom S3 endpoint is provided (like s3-external-1 or a FIPS endpoint) then we should still use virtual addressing
 * when possible.
 */
@Test
public void customS3EndpointProvided_UsesVirtualAddressing() throws Exception {
    URI customEndpoint = URI.create("https://s3-external-1.amazonaws.com");
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = clientBuilder().endpointOverride(customEndpoint).build();

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(),
                          String.format("https://%s.s3-external-1.amazonaws.com", BUCKET));
}
 
Example 19
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * If a custom, non-s3 endpoint is used we revert to path style addressing. This is useful for alternative S3 implementations
 * like Ceph that do not support virtual style addressing.
 */
@Test
public void nonS3EndpointProvided_DoesNotUseVirtualAddressing() throws Exception {
    URI customEndpoint = URI.create("https://foobar.amazonaws.com");
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = clientBuilder().endpointOverride(customEndpoint).build();

    s3Client.listObjects(ListObjectsRequest.builder().bucket(BUCKET).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(), customEndpoint.toString() + "/" + BUCKET);
}
 
Example 20
Source File: S3EndpointResolutionTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void accessPointArn_differentRegion_useArnRegionTrue() throws Exception {
    URI customEndpoint = URI.create("https://foobar-12345678910.s3-accesspoint.us-west-2.amazonaws.com");
    mockHttpClient.stubNextResponse(mockListObjectsResponse());
    S3Client s3Client = clientBuilder().serviceConfiguration(b -> b.useArnRegionEnabled(true)).build();
    String accessPointArn = "arn:aws:s3:us-west-2:12345678910:accesspoint:foobar";

    s3Client.listObjects(ListObjectsRequest.builder().bucket(accessPointArn).build());

    assertEndpointMatches(mockHttpClient.getLastRequest(), customEndpoint.toString());
}