Search in sources :

Example 16 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project presto by prestodb.

the class TestPrestoS3FileSystem method testListPrefixModes.

@Test
public void testListPrefixModes() throws Exception {
    S3ObjectSummary rootObject = new S3ObjectSummary();
    rootObject.setStorageClass(StorageClass.Standard.toString());
    rootObject.setKey("standard-object-at-root.txt");
    rootObject.setLastModified(new Date());
    S3ObjectSummary childObject = new S3ObjectSummary();
    childObject.setStorageClass(StorageClass.Standard.toString());
    childObject.setKey("prefix/child-object.txt");
    childObject.setLastModified(new Date());
    try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) {
        MockAmazonS3 s3 = new MockAmazonS3() {

            @Override
            public ObjectListing listObjects(ListObjectsRequest listObjectsRequest) {
                ObjectListing listing = new ObjectListing();
                // Shallow listing
                if ("/".equals(listObjectsRequest.getDelimiter())) {
                    listing.getCommonPrefixes().add("prefix");
                    listing.getObjectSummaries().add(rootObject);
                    return listing;
                }
                // Recursive listing of object keys only
                listing.getObjectSummaries().addAll(Arrays.asList(childObject, rootObject));
                return listing;
            }
        };
        Path rootPath = new Path("s3n://test-bucket/");
        fs.initialize(rootPath.toUri(), new Configuration());
        fs.setS3Client(s3);
        List<LocatedFileStatus> shallowAll = remoteIteratorToList(fs.listLocatedStatus(rootPath));
        assertEquals(shallowAll.size(), 2);
        assertTrue(shallowAll.get(0).isDirectory());
        assertFalse(shallowAll.get(1).isDirectory());
        assertEquals(shallowAll.get(0).getPath(), new Path(rootPath, "prefix"));
        assertEquals(shallowAll.get(1).getPath(), new Path(rootPath, rootObject.getKey()));
        List<LocatedFileStatus> shallowFiles = remoteIteratorToList(fs.listFiles(rootPath, false));
        assertEquals(shallowFiles.size(), 1);
        assertFalse(shallowFiles.get(0).isDirectory());
        assertEquals(shallowFiles.get(0).getPath(), new Path(rootPath, rootObject.getKey()));
        List<LocatedFileStatus> recursiveFiles = remoteIteratorToList(fs.listFiles(rootPath, true));
        assertEquals(recursiveFiles.size(), 2);
        assertFalse(recursiveFiles.get(0).isDirectory());
        assertFalse(recursiveFiles.get(1).isDirectory());
        assertEquals(recursiveFiles.get(0).getPath(), new Path(rootPath, childObject.getKey()));
        assertEquals(recursiveFiles.get(1).getPath(), new Path(rootPath, rootObject.getKey()));
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) Configuration(org.apache.hadoop.conf.Configuration) ClientConfiguration(com.amazonaws.ClientConfiguration) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 17 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project presto by prestodb.

the class PrestoS3FileSystem method listPrefix.

private Iterator<LocatedFileStatus> listPrefix(Path path, OptionalInt initialMaxKeys, ListingMode mode) {
    String key = keyFromPath(path);
    if (!key.isEmpty()) {
        key += PATH_SEPARATOR;
    }
    ListObjectsRequest request = new ListObjectsRequest().withBucketName(getBucketName(uri)).withPrefix(key).withDelimiter(mode == ListingMode.RECURSIVE_FILES_ONLY ? null : PATH_SEPARATOR).withMaxKeys(initialMaxKeys.isPresent() ? initialMaxKeys.getAsInt() : null);
    STATS.newListObjectsCall();
    Iterator<ObjectListing> listings = new AbstractSequentialIterator<ObjectListing>(s3.listObjects(request)) {

        @Override
        protected ObjectListing computeNext(ObjectListing previous) {
            if (!previous.isTruncated()) {
                return null;
            }
            // Clear any max keys set for the initial request before submitting subsequent requests. Values < 0
            // are not sent in the request and the default limit is used
            previous.setMaxKeys(-1);
            return s3.listNextBatchOfObjects(previous);
        }
    };
    Iterator<LocatedFileStatus> result = Iterators.concat(Iterators.transform(listings, this::statusFromListing));
    if (mode.isFilesOnly()) {
        // Even recursive listing can still contain empty "directory" objects, must filter them out
        result = Iterators.filter(result, LocatedFileStatus::isFile);
    }
    return result;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) LocatedFileStatus(org.apache.hadoop.fs.LocatedFileStatus) AbstractSequentialIterator(com.google.common.collect.AbstractSequentialIterator)

Example 18 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest 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 19 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest 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 20 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest 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 (com.amazonaws.services.s3.model.ListObjectsRequest)48 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)46 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)32 ArrayList (java.util.ArrayList)23 AmazonClientException (com.amazonaws.AmazonClientException)11 IOException (java.io.IOException)9 Path (org.apache.hadoop.fs.Path)9 HashMap (java.util.HashMap)8 LocatedFileStatus (org.apache.hadoop.fs.LocatedFileStatus)8 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)6 FileStatus (org.apache.hadoop.fs.FileStatus)6 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)6 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)6 Date (java.util.Date)5 Test (org.junit.Test)5 S3Object (software.amazon.awssdk.services.s3.model.S3Object)5 StocatorPath (com.ibm.stocator.fs.common.StocatorPath)4 FileNotFoundException (java.io.FileNotFoundException)4 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3