Search in sources :

Example 51 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project onebusaway-application-modules by camsys.

the class S3FileServiceImpl method bundleDirectoryExists.

@Override
public /**
 * check to see if the given bundle directory exists in the configured bucket.
 * Do not include leading slashes in the filename(key).
 */
boolean bundleDirectoryExists(String filename) {
    ListObjectsRequest request = new ListObjectsRequest(_bucketName, filename, null, null, 1);
    ObjectListing listing = _s3.listObjects(request);
    return listing.getObjectSummaries().size() > 0;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing)

Example 52 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project onebusaway-application-modules by camsys.

the class S3FileServiceImpl method listBundleDirectories.

@Override
public /**
 * Return tabular data (filename, flag, modified date) about bundle directories.
 */
List<String[]> listBundleDirectories(int maxResults) {
    List<String[]> rows = new ArrayList<String[]>();
    HashMap<String, String> map = new HashMap<String, String>();
    ListObjectsRequest request = new ListObjectsRequest(_bucketName, null, null, "/", maxResults);
    ObjectListing listing = null;
    do {
        if (listing == null) {
            listing = _s3.listObjects(request);
            if (listing.getCommonPrefixes() != null) {
                // short circuit if common prefixes works
                List<String> commonPrefixes = listing.getCommonPrefixes();
                for (String key : commonPrefixes) {
                    Date lastModified = getLastModifiedTimeForKey(key);
                    String lastModifiedStr = "n/a";
                    if (lastModified != null) {
                        lastModifiedStr = "" + lastModified.toString();
                    }
                    String[] columns = { parseKey(key), getStatus(key), lastModifiedStr };
                    rows.add(columns);
                }
                return rows;
            }
            _log.error("prefixes=" + listing.getCommonPrefixes());
        } else {
            listing = _s3.listNextBatchOfObjects(listing);
        }
        for (S3ObjectSummary summary : listing.getObjectSummaries()) {
            String key = parseKey(summary.getKey());
            if (!map.containsKey(key)) {
                String[] columns = { key, " ", "" + summary.getLastModified().getTime() };
                rows.add(columns);
                map.put(key, key);
            }
        }
    } while (listing.isTruncated());
    return rows;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Date(java.util.Date)

Example 53 with ListObjectsRequest

use of software.amazon.awssdk.services.s3.model.ListObjectsRequest in project onebusaway-application-modules by camsys.

the class S3FileServiceImpl method getLastModifiedTimeForKey.

private Date getLastModifiedTimeForKey(String key) {
    ListObjectsRequest request = new ListObjectsRequest(_bucketName, key, null, "/", 1);
    ObjectListing listing = _s3.listObjects(request);
    if (!listing.getObjectSummaries().isEmpty())
        return listing.getObjectSummaries().get(0).getLastModified();
    return null;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) ObjectListing(com.amazonaws.services.s3.model.ObjectListing)

Example 54 with ListObjectsRequest

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

the class S3BlobContainer method children.

@Override
public Map<String, BlobContainer> children() throws IOException {
    try (AmazonS3Reference clientReference = blobStore.clientReference()) {
        ObjectListing prevListing = null;
        final var result = new LinkedHashMap<String, BlobContainer>();
        while (true) {
            ObjectListing list;
            if (prevListing != null) {
                final ObjectListing finalPrevListing = prevListing;
                list = clientReference.client().listNextBatchOfObjects(finalPrevListing);
            } else {
                final ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
                listObjectsRequest.setBucketName(blobStore.bucket());
                listObjectsRequest.setPrefix(keyPath);
                listObjectsRequest.setDelimiter("/");
                list = clientReference.client().listObjects(listObjectsRequest);
            }
            for (final String summary : list.getCommonPrefixes()) {
                final String name = summary.substring(keyPath.length());
                if (name.isEmpty() == false) {
                    // Stripping the trailing slash off of the common prefix
                    final String last = name.substring(0, name.length() - 1);
                    final BlobPath path = path().add(last);
                    result.put(last, blobStore.blobContainer(path));
                }
            }
            assert list.getObjectSummaries().stream().noneMatch(s -> {
                for (String commonPrefix : list.getCommonPrefixes()) {
                    if (s.getKey().substring(keyPath.length()).startsWith(commonPrefix)) {
                        return true;
                    }
                }
                return false;
            }) : "Response contained children for listed common prefixes.";
            if (list.isTruncated()) {
                prevListing = list;
            } else {
                break;
            }
        }
        return result;
    } catch (final AmazonClientException e) {
        throw new IOException("Exception when listing children of [" + path().buildAsString() + ']', e);
    }
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) BlobPath(org.elasticsearch.common.blobstore.BlobPath) AmazonClientException(com.amazonaws.AmazonClientException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap)

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