Example usage for com.amazonaws.services.s3.model ObjectListing getObjectSummaries

List of usage examples for com.amazonaws.services.s3.model ObjectListing getObjectSummaries

Introduction

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

Prototype

public List<S3ObjectSummary> getObjectSummaries() 

Source Link

Document

Gets the list of object summaries describing the objects stored in the S3 bucket.

Usage

From source file:mail.server.storage.AWSStorageDelete.java

License:GNU General Public License

protected void deleteBucketContents(AmazonS3 s3, String bucketName) throws Exception {
    while (true) {
        List<String> keys = new ArrayList<String>();

        log.debug("creating batch delete");

        ObjectListing listing = s3.listObjects(bucketName);
        for (S3ObjectSummary i : listing.getObjectSummaries()) {
            log.debug("key", i.getKey());
            keys.add(i.getKey());/*from w ww .jav a  2  s .com*/
        }

        if (keys.isEmpty())
            break;

        DeleteObjectsRequest req = new DeleteObjectsRequest(bucketName).withKeys(keys.toArray(new String[0]));
        log.debug("deleting");
        s3.deleteObjects(req);
    }
}

From source file:n3phele.agent.repohandlers.S3Large.java

License:Open Source License

@Override
public List<String> getFileList() {
    List<String> result = new ArrayList<String>();
    int starIndex = key.indexOf("*");
    int questionIndex = key.indexOf("?");
    int curlyIndex = key.indexOf("{");
    if (starIndex == -1 && questionIndex == -1 && curlyIndex == -1) {
        this.totalLength = null;
        return result; // not wildcard
    }/*from   w ww . j a  va2  s. c  o m*/

    if (starIndex == -1)
        starIndex = Integer.MAX_VALUE;
    if (questionIndex == -1)
        questionIndex = Integer.MAX_VALUE;
    if (curlyIndex == -1)
        curlyIndex = Integer.MAX_VALUE;

    int wildStart = Math.min(Math.min(starIndex, questionIndex), curlyIndex);

    base = key.substring(0, wildStart);
    base = base.substring(0, base.lastIndexOf("/") + 1);
    // String wild = key.substring(base.lastIndexOf("/")+1);
    boolean done = false;
    this.totalLength = 0L;
    Pattern pattern = Pattern.compile("^" + Helper.wildcardToRegex(key));
    ObjectListing listing = s3().listObjects(this.root, base);
    while (!done) {
        done = !listing.isTruncated();

        for (S3ObjectSummary f : listing.getObjectSummaries()) {
            if (pattern.matcher(f.getKey()).matches()) {
                String file = f.getKey().substring(base.length());
                result.add(file);
                this.totalLength += f.getSize();
                log.info("Adding " + file + " size " + f.getSize());
            }
        }
        if (!done)
            listing = s3().listNextBatchOfObjects(listing);
    }

    return result;
}

From source file:n3phele.storage.s3.CloudStorageImpl.java

License:Open Source License

public FileNode getMetadata(Repository repo, String filename) {
    FileNode f = new FileNode();
    UriBuilder result = UriBuilder.fromUri(repo.getTarget());
    result.path(repo.getRoot()).path(filename);
    f.setName(result.build().toString());
    Credential credential = repo.getCredential().decrypt();

    log.info("Get info on " + repo.getRoot() + " " + filename);
    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    try {/*from w  w w .  j av  a 2  s. c  o  m*/
        ObjectListing metadata = s3.listObjects(
                new ListObjectsRequest().withBucketName(repo.getRoot()).withPrefix(filename).withMaxKeys(1));
        List<S3ObjectSummary> metadataList = metadata.getObjectSummaries();
        if (metadataList == null || metadataList.size() != 1) {
            throw new NotFoundException(filename);
        }
        f.setModified(metadataList.get(0).getLastModified());
        f.setSize(metadataList.get(0).getSize());

        log.info(filename + " " + f.getModified() + " " + f.getSize());
    } catch (AmazonServiceException e) {
        log.log(Level.WARNING, "Service Error processing " + f + repo, e);
        throw new NotFoundException("Retrieve " + filename + " fails " + e.toString());
    } catch (AmazonClientException e) {
        log.log(Level.SEVERE, "Client Error processing " + f + repo, e);
        throw new NotFoundException("Retrieve " + filename + " fails " + e.toString());
    }
    return f;
}

From source file:n3phele.storage.s3.CloudStorageImpl.java

License:Open Source License

public boolean deleteFolder(Repository repo, String filename) {
    boolean result = false;
    Credential credential = repo.getCredential().decrypt();
    int retry = 3;

    setPermissions(repo, filename, false);
    if (!filename.endsWith("/")) {
        filename += "/";
    }/*w w w . ja  v  a2  s.  c  o  m*/

    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    while (retry-- > 0) {
        try {
            ObjectListing objects = s3.listObjects(repo.getRoot(), filename);
            for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                log.info("Delete " + repo.getRoot() + ":" + objectSummary.getKey());
                s3.deleteObject(repo.getRoot(), objectSummary.getKey());
            }
            if (objects.isTruncated()) {
                retry++;
                log.info("Doing next portion");
                continue;
            }
            result = true;
            break;
        } catch (AmazonServiceException e) {
            log.log(Level.WARNING, "Service Error processing " + repo, e);
        } catch (AmazonClientException e) {
            log.log(Level.SEVERE, "Client Error processing " + repo, e);
        }
    }
    return result;
}

From source file:n3phele.storage.s3.CloudStorageImpl.java

License:Open Source License

public List<FileNode> getFileList(Repository repo, String prefix, int max) {
    List<FileNode> result = new ArrayList<FileNode>();
    Credential credential = repo.getCredential().decrypt();

    AmazonS3Client s3 = new AmazonS3Client(
            new BasicAWSCredentials(credential.getAccount(), credential.getSecret()));
    s3.setEndpoint(repo.getTarget().toString());
    Policy p = null;/* w w w  .ja  v a2  s  . c o m*/
    try {
        BucketPolicy bp = s3.getBucketPolicy(repo.getRoot());
        log.info("Policy text " + bp.getPolicyText());
        if (bp != null && bp.getPolicyText() != null) {
            p = PolicyHelper.parse(bp.getPolicyText());
            log.info("Policy object is " + (p == null ? null : p.toJson()));
        }
    } catch (Exception e) {
        log.log(Level.WARNING, "Policy not supported", e);
    }

    try {
        ObjectListing s3Objects = s3
                .listObjects(new ListObjectsRequest(repo.getRoot(), prefix, null, "/", max));
        if (s3Objects.getCommonPrefixes() != null) {
            for (String dirName : s3Objects.getCommonPrefixes()) {
                String name = dirName;
                if (dirName.endsWith("/")) {
                    name = dirName.substring(0, dirName.length() - 1);
                }
                boolean isPublic = isPublicFolder(p, repo.getRoot(), name);
                name = name.substring(name.lastIndexOf("/") + 1);
                FileNode folder = FileNode.newFolder(name, prefix, repo, isPublic);
                log.info("Folder:" + folder);
                result.add(folder);
            }
        }
        if (s3Objects.getObjectSummaries() != null) {
            for (S3ObjectSummary fileSummary : s3Objects.getObjectSummaries()) {
                String key = fileSummary.getKey();
                if (key != null && !key.equals(prefix)) {
                    String name = key.substring(key.lastIndexOf("/") + 1);
                    UriBuilder builder = UriBuilder.fromUri(repo.getTarget()).path(repo.getRoot());
                    if (prefix != null && !prefix.isEmpty()) {
                        builder = builder.path(prefix);
                    }
                    FileNode file = FileNode.newFile(name, prefix, repo, fileSummary.getLastModified(),
                            fileSummary.getSize(), builder.path(name).toString());
                    log.info("File:" + file);
                    result.add(file);
                }
            }
        }

    } catch (AmazonServiceException e) {
        log.log(Level.WARNING, "Service Error processing " + repo, e);
    } catch (AmazonClientException e) {
        log.log(Level.SEVERE, "Client Error processing " + repo, e);
    }
    return result;
}

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 {/*www  .j  a v  a2  s . 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:net.henryhu.roxlab2.NotePadProvider.java

License:Apache License

private List<ContentValues> s3list() {
    if (bucket == null)
        bucket = s3.createBucket(bucketName);
    List<ContentValues> items = new ArrayList<ContentValues>();
    ObjectListing ol = s3.listObjects(bucketName);
    for (Object o : ol.getObjectSummaries()) {
        ContentValues ret = new ContentValues();
        String key = ((S3ObjectSummary) o).getKey();
        String id = objectKeyToId(key);
        String title = objectKeyToTitle(key);
        ret.put(NotePad.Notes.COLUMN_NAME_TITLE, title);
        ret.put(NotePad.Notes._ID, id);/*  ww w . j  ava2s . c o  m*/
        Log.w("s3list()", "id: " + id + " title: " + title);
        items.add(ret);
    }
    Log.w("s3list()", "total id: " + items.size());
    return items;
}

From source file:net.henryhu.roxlab2.NotePadProvider.java

License:Apache License

private int s3delete(String id) {
    if (bucket == null)
        bucket = s3.createBucket(bucketName);
    int count = 0;
    ObjectListing ol = s3.listObjects(bucketName, id + "_");
    for (Object o : ol.getObjectSummaries()) {
        S3ObjectSummary sum = (S3ObjectSummary) o;
        s3.deleteObject(bucketName, sum.getKey());
        count++;//from   w  w w.j a  v  a2  s  . c  o m
    }
    Log.w("s3delete()", "delete with id: " + id + " count: " + count);
    return count;
}

From source file:net.henryhu.roxlab2.NotePadProvider.java

License:Apache License

private ContentValues s3query(String id) {
    if (bucket == null)
        bucket = s3.createBucket(bucketName);

    Log.w("s3query()", "query id: " + id);

    ObjectListing ol = s3.listObjects(bucketName, id + "_");
    for (Object o : ol.getObjectSummaries()) {
        S3ObjectSummary sum = (S3ObjectSummary) o;
        S3Object obj = s3.getObject(bucketName, sum.getKey());

        ObjectMetadata om = obj.getObjectMetadata();

        byte[] buf = new byte[(int) om.getContentLength()];
        try {/*from   w  w w . j ava2 s . c  om*/
            InputStream contents = obj.getObjectContent();
            contents.read(buf);
            contents.close();
            String sbuf = new String(buf, "UTF-8");
            Map<String, String> entries = ParseInfo.getEntries(sbuf);
            ContentValues vals = new ContentValues();
            for (String key : entries.keySet()) {
                vals.put(key, entries.get(key));
            }
            Log.w("s3query()", "query succ");
            return vals;
        } catch (Exception e) {
        }
    }
    Log.w("s3query()", "query fail");
    return null;
}

From source file:nl.kpmg.lcm.server.data.s3.S3FileSystemAdapter.java

License:Apache License

@Override
public List listFileNames(String subPath) throws IOException {

    if (subPath.length() > 0 && subPath.charAt(subPath.length() - 1) != '/') {
        subPath = subPath + '/';
    }/*  w  w  w. j av a  2 s .com*/

    LinkedList<String> fileNameList = new LinkedList();
    ObjectListing listing;
    do {
        listing = s3Client.listObjects(bucketName, subPath);
        List<S3ObjectSummary> objectSummaryList = listing.getObjectSummaries();

        for (S3ObjectSummary objectSummary : objectSummaryList) {
            String key = objectSummary.getKey();
            int index = StringUtils.lastIndexOf(key, "/");
            // Some of the keys contain '/'. For example "test/test1.csv". Some others don`t. For
            // example "example1.txt". If a key doesn`t contain '/' the value of index would be -1. That
            // is why we need the following check.
            if (index == -1) {
                index = 0;
            }
            String itemName = key.substring(index);
            fileNameList.add(itemName);
        }
    } while (listing.isTruncated());

    return fileNameList;
}