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

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

Introduction

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

Prototype

public ListObjectsRequest withBucketName(String bucketName) 

Source Link

Document

Sets the name of the Amazon S3 bucket whose objects are to be listed.

Usage

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

License:Open Source License

protected List<S3ObjectSummary> getS3ObjectSummaries(String prefix) {
    try {/*  w ww  .j  a va  2 s. co  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 ww  .  j a  va2  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 {//  w  w  w .j a v a 2s.  c  o 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;
}

From source file:surrey.repository.impl.S3Repository.java

License:Open Source License

/**
 * @throws IOException/*from  w w  w.j  a  va 2  s  . c om*/
 * @see surrey.repository.Repository#createUniqueFile(java.lang.String,
 *      java.lang.String)
 */
@Override
public RepositoryFile createUniqueFile(String prefix, String name) throws IOException {
    initialise();
    String cleanPrefix = prefix != null ? prefix.replaceAll("\\\\", "/") : "empty";
    if (cleanPrefix.startsWith("/")) {
        cleanPrefix = cleanPrefix.substring(1);
    }
    // create name, get list of files with that name
    String finalUri = cleanPrefix + getEnd(cleanPrefix);

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
    String result = finalUri + name;
    listObjectsRequest.withBucketName(baseURL).withPrefix(result);
    listObjectsRequest.setMaxKeys(20);
    ObjectListing listing = s3.listObjects(listObjectsRequest);
    int counter = 0;
    while (listing.getObjectSummaries() != null && listing.getObjectSummaries().size() > 0) {
        boolean found = false;
        boolean firstCall = true;
        do {
            if (!firstCall) {
                listing = s3.listNextBatchOfObjects(listing);
            }
            for (S3ObjectSummary summary : listing.getObjectSummaries()) {
                if (summary.getKey().equals(result)) {
                    result = finalUri + counter++ + name;
                    found = true;
                    break;
                }
            }
            if (found) {
                break;
            }
            firstCall = false;
        } while (listing.isTruncated());
        if (!found) {
            break;
        }
        listObjectsRequest.setPrefix(result);
        listing = s3.listObjects(listObjectsRequest);
    }
    // result is now what should be used so create a zero byte file to lock
    // that name to us

    S3RepositoryFile repoFile = new S3RepositoryFile(baseURL, result, transferManager);
    ByteArrayInputStream source = new ByteArrayInputStream(new byte[] { (byte) 0 });
    repoFile.write(source, 1);
    return repoFile;
}

From source file:surrey.repository.impl.S3RepositoryFile.java

License:Open Source License

@Override
public boolean exists() {

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest();
    listObjectsRequest.withBucketName(bucketName).withPrefix(key);
    listObjectsRequest.setMaxKeys(1);/*from w  ww.j  a  v a  2  s .  co m*/
    ObjectListing listing = transferManager.getAmazonS3Client().listObjects(listObjectsRequest);

    return listing.getObjectSummaries() != null && listing.getObjectSummaries().size() > 0;
}