Example usage for com.amazonaws.services.s3.model ListObjectsRequest withPrefix

List of usage examples for com.amazonaws.services.s3.model ListObjectsRequest withPrefix

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model ListObjectsRequest withPrefix.

Prototype

public ListObjectsRequest withPrefix(String prefix) 

Source Link

Document

Sets the optional prefix parameter restricting the response to keys that begin with the specified prefix.

Usage

From source file:com.ikanow.infinit.e.harvest.extraction.document.file.AwsInfiniteFile.java

License:Open Source License

@Override
public InfiniteFile[] listFiles(Date optionalFilterDate, int maxDocs) {
    InfiniteFile[] fileList = null;// w w w.  j  a  va2  s .c  om
    ObjectListing list = null;
    _overwriteTime = 0L;
    ListObjectsRequest listRequest = new ListObjectsRequest().withBucketName(_awsBucketName);
    if (null != _awsObjectName) {
        listRequest.withPrefix(_awsObjectName);
    }
    listRequest.withDelimiter("/");
    list = ((AmazonS3Client) _awsClient).listObjects(listRequest);
    fileList = new InfiniteFile[list.getObjectSummaries().size() + list.getCommonPrefixes().size()];
    //TESTED (3.2)
    int nAdded = 0;
    // Get the sub-directories
    for (String subDir : list.getCommonPrefixes()) {
        // Create directories:
        fileList[nAdded] = new AwsInfiniteFile(_awsBucketName, subDir, null, _awsClient);
        nAdded++;
    } //TESTED (3b.3)
      // Get the files:
    for (S3ObjectSummary s3Obj : list.getObjectSummaries()) {
        if (!s3Obj.getKey().endsWith("/")) {
            fileList[nAdded] = new AwsInfiniteFile(s3Obj.getBucketName(), s3Obj.getKey(),
                    s3Obj.getLastModified(), _awsClient);
            long fileTime = fileList[nAdded].getDate();
            if (fileTime > _overwriteTime) {
                _overwriteTime = fileTime;
            } //TESTED (3.2)
            nAdded++;
        }
    }
    return fileList;
}

From source file:com.liferay.portal.store.s3.S3Store.java

License:Open Source License

protected List<S3ObjectSummary> getS3ObjectSummaries(String prefix) {
    try {/*  w w  w.j  a va2 s .c  o  m*/
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest();

        listObjectsRequest.withBucketName(_bucketName);
        listObjectsRequest.withPrefix(prefix);

        ObjectListing objectListing = _amazonS3.listObjects(listObjectsRequest);

        List<S3ObjectSummary> s3ObjectSummaries = new ArrayList<>(objectListing.getMaxKeys());

        while (true) {
            s3ObjectSummaries.addAll(objectListing.getObjectSummaries());

            if (objectListing.isTruncated()) {
                objectListing = _amazonS3.listNextBatchOfObjects(objectListing);
            } else {
                break;
            }
        }

        return s3ObjectSummaries;
    } catch (AmazonClientException ace) {
        throw transform(ace);
    }
}

From source file:com.sludev.commons.vfs2.provider.s3.SS3FileObject.java

License:Apache License

/**
 * Lists the children of this file.  Is only called if {@link #doGetType}
 * returns {@link FileType#FOLDER}.  The return value of this method
 * is cached, so the implementation can be expensive.<br />
 * @return a possible empty String array if the file is a directory or null or an exception if the
 * file is not a directory or can't be read.
 * @throws Exception if an error occurs.
 *///from   w w w. j  a v  a 2  s.c o  m
@Override
protected String[] doListChildren() throws Exception {
    String[] res = null;

    Pair<String, String> path = getContainerAndPath();

    String prefix = path.getRight();
    if (prefix.endsWith("/") == false) {
        // We need folders ( prefixes ) to end with a slash
        prefix += "/";
    }
    ListObjectsRequest loReq = new ListObjectsRequest();
    loReq.withBucketName(path.getLeft());
    loReq.withPrefix(prefix);
    loReq.withDelimiter("/");

    ObjectListing blobs = fileSystem.getClient().listObjects(loReq);

    List<String> resList = new ArrayList<>();
    for (S3ObjectSummary osum : blobs.getObjectSummaries()) {
        String currBlobStr = osum.getKey();
        resList.add(String.format("/%s/%s", path.getLeft(), currBlobStr));
    }

    List<String> commPrefixes = blobs.getCommonPrefixes();
    if (commPrefixes != null) {
        for (String currFld : commPrefixes) {
            resList.add(String.format("/%s/%s", path.getLeft(), currFld));
        }
    }

    res = resList.toArray(new String[resList.size()]);

    return res;
}

From source file:net.geoprism.data.aws.AmazonEndpoint.java

License:Open Source License

private List<String> listFiles(String prefix) {
    List<String> files = new LinkedList<String>();

    try {/*ww w.  j a v  a  2 s.co  m*/
        AmazonS3 s3Client = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());

        ListObjectsRequest request = new ListObjectsRequest();
        request = request.withBucketName("geodashboarddata");
        request = request.withPrefix(prefix);

        ObjectListing listing;

        do {
            listing = s3Client.listObjects(request);

            List<S3ObjectSummary> summaries = listing.getObjectSummaries();

            for (S3ObjectSummary summary : summaries) {
                String key = summary.getKey();

                if (key.endsWith(".xml.gz")) {
                    files.add(key);
                }
            }

            request.setMarker(listing.getNextMarker());
        } while (listing != null && listing.isTruncated());
    } catch (Exception e) {
        logger.error("Unable to retrieve files", e);
    }

    return files;
}