Search in sources :

Example 1 with ListObjectsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectsResponse in project flink by apache.

the class AWSServicesTestUtils method listBucketObjects.

public static List<S3Object> listBucketObjects(S3AsyncClient s3, String bucketName) throws ExecutionException, InterruptedException {
    ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
    CompletableFuture<ListObjectsResponse> res = s3.listObjects(listObjects);
    return res.get().contents();
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse)

Example 2 with ListObjectsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectsResponse in project aws-doc-sdk-examples by awsdocs.

the class ListObjects method listBucketObjects.

// snippet-start:[s3.java2.list_objects.main]
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);
    }
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) ListIterator(java.util.ListIterator)

Example 3 with ListObjectsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectsResponse in project aws-doc-sdk-examples by awsdocs.

the class S3Service method listBucketObjects.

// Returns the names of all images in the given bucket.
public List<String> listBucketObjects(String bucketName) {
    S3Client s3 = getClient();
    String keyName;
    List<String> keys = new ArrayList<>();
    try {
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            keyName = myValue.key();
            keys.add(keyName);
        }
        return keys;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client)

Example 4 with ListObjectsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectsResponse in project aws-doc-sdk-examples by awsdocs.

the class IAMScenario method assumeGivenRole.

// Invoke an Amazon S3 operation using the Assumed Role.
public static void assumeGivenRole(String roleArn, String roleSessionName, String bucketName) {
    StsClient stsClient = StsClient.builder().region(Region.US_EAST_1).build();
    try {
        AssumeRoleRequest roleRequest = AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName(roleSessionName).build();
        AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest);
        Credentials myCreds = roleResponse.credentials();
        String key = myCreds.accessKeyId();
        String secKey = myCreds.secretAccessKey();
        String secToken = myCreds.sessionToken();
        // List all objects in an Amazon S3 bucket using the temp creds.
        Region region = Region.US_EAST_1;
        S3Client s3 = S3Client.builder().credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create(key, secKey, secToken))).region(region).build();
        System.out.println("Created a S3Client using temp credentials.");
        System.out.println("Listing objects in " + bucketName);
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            System.out.println("The name of the key is " + myValue.key());
            System.out.println("The owner is " + myValue.owner());
        }
    } catch (StsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : StsClient(software.amazon.awssdk.services.sts.StsClient) AssumeRoleRequest(software.amazon.awssdk.services.sts.model.AssumeRoleRequest) ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) StsException(software.amazon.awssdk.services.sts.model.StsException) AssumeRoleResponse(software.amazon.awssdk.services.sts.model.AssumeRoleResponse) Region(software.amazon.awssdk.regions.Region) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client) AwsSessionCredentials(software.amazon.awssdk.auth.credentials.AwsSessionCredentials) Credentials(software.amazon.awssdk.services.sts.model.Credentials)

Example 5 with ListObjectsResponse

use of software.amazon.awssdk.services.s3.model.ListObjectsResponse in project aws-doc-sdk-examples by awsdocs.

the class S3Service method listBucketObjects.

// Returns the names of all images in the given bucket.
public List<String> listBucketObjects(String bucketName) {
    S3Client s3 = getClient();
    String keyName;
    List<String> keys = new ArrayList<>();
    try {
        ListObjectsRequest listObjects = ListObjectsRequest.builder().bucket(bucketName).build();
        ListObjectsResponse res = s3.listObjects(listObjects);
        List<S3Object> objects = res.contents();
        for (S3Object myValue : objects) {
            keyName = myValue.key();
            keys.add(keyName);
        }
        return keys;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : ListObjectsRequest(software.amazon.awssdk.services.s3.model.ListObjectsRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ArrayList(java.util.ArrayList) ListObjectsResponse(software.amazon.awssdk.services.s3.model.ListObjectsResponse) S3Object(software.amazon.awssdk.services.s3.model.S3Object) S3Client(software.amazon.awssdk.services.s3.S3Client)

Aggregations

ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)6 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)6 S3Object (software.amazon.awssdk.services.s3.model.S3Object)6 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)4 S3Client (software.amazon.awssdk.services.s3.S3Client)3 ArrayList (java.util.ArrayList)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ListIterator (java.util.ListIterator)1 S3TestUtils.buildMockedS3FileSystem (org.apache.beam.sdk.io.aws2.s3.S3TestUtils.buildMockedS3FileSystem)1 Test (org.junit.Test)1 AwsSessionCredentials (software.amazon.awssdk.auth.credentials.AwsSessionCredentials)1 SdkServiceException (software.amazon.awssdk.core.exception.SdkServiceException)1 Region (software.amazon.awssdk.regions.Region)1 HeadObjectRequest (software.amazon.awssdk.services.s3.model.HeadObjectRequest)1 HeadObjectResponse (software.amazon.awssdk.services.s3.model.HeadObjectResponse)1 ListObjectsV2Response (software.amazon.awssdk.services.s3.model.ListObjectsV2Response)1 StsClient (software.amazon.awssdk.services.sts.StsClient)1 AssumeRoleRequest (software.amazon.awssdk.services.sts.model.AssumeRoleRequest)1 AssumeRoleResponse (software.amazon.awssdk.services.sts.model.AssumeRoleResponse)1