Example usage for com.amazonaws.services.s3.model S3ObjectSummary getKey

List of usage examples for com.amazonaws.services.s3.model S3ObjectSummary getKey

Introduction

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

Prototype

public String getKey() 

Source Link

Document

Gets the key under which this object is stored in Amazon S3.

Usage

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;//from w  w w.  j  av  a2 s.co  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 {//from w  w w. ja va 2s .  com
        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 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++;/*  w ww  . j  av 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  a v  a 2 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:net.solarnetwork.node.backup.s3.SdkS3Client.java

License:Open Source License

@Override
public Set<S3ObjectReference> listObjects(String prefix) throws IOException {
    AmazonS3 client = getClient();/*  w  ww  . jav  a2s . c om*/
    Set<S3ObjectReference> result = new LinkedHashSet<>(100);
    try {
        final ListObjectsV2Request req = new ListObjectsV2Request();
        req.setBucketName(bucketName);
        req.setMaxKeys(maximumKeysPerRequest);
        req.setPrefix(prefix);
        ListObjectsV2Result listResult;
        do {
            listResult = client.listObjectsV2(req);

            for (S3ObjectSummary objectSummary : listResult.getObjectSummaries()) {
                result.add(new S3ObjectReference(objectSummary.getKey(), objectSummary.getSize(),
                        objectSummary.getLastModified()));
            }
            req.setContinuationToken(listResult.getNextContinuationToken());
        } while (listResult.isTruncated() == true);

    } catch (AmazonServiceException e) {
        log.warn("AWS error: {}; HTTP code {}; AWS code {}; type {}; request ID {}", e.getMessage(),
                e.getStatusCode(), e.getErrorCode(), e.getErrorType(), e.getRequestId());
        throw new RemoteServiceException("Error listing S3 objects at " + prefix, e);
    } catch (AmazonClientException e) {
        log.debug("Error communicating with AWS: {}", e.getMessage());
        throw new IOException("Error communicating with AWS", e);
    }
    return result;
}

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  ww.j  a v  a 2s.  c  o  m

    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;
}

From source file:nl.nn.adapterframework.filesystem.AmazonS3FileSystem.java

License:Apache License

@Override
public Iterator<S3Object> listFiles(String folder) throws FileSystemException {
    List<S3ObjectSummary> summaries = null;
    String prefix = folder != null ? folder + "/" : "";
    try {//  www .  j av a 2s . c  o  m
        ObjectListing listing = s3Client.listObjects(bucketName, prefix);
        summaries = listing.getObjectSummaries();
        while (listing.isTruncated()) {
            listing = s3Client.listNextBatchOfObjects(listing);
            summaries.addAll(listing.getObjectSummaries());
        }
    } catch (AmazonServiceException e) {
        throw new FileSystemException("Cannot process requested action", e);
    }

    List<S3Object> list = new ArrayList<S3Object>();
    for (S3ObjectSummary summary : summaries) {
        S3Object object = new S3Object();
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(summary.getSize());

        object.setBucketName(summary.getBucketName());
        object.setKey(summary.getKey());
        object.setObjectMetadata(metadata);
        if (!object.getKey().endsWith("/") && !(prefix.isEmpty() && object.getKey().contains("/"))) {
            list.add(object);
        }
    }

    return list.iterator();
}

From source file:nl.nn.adapterframework.filesystem.AmazonS3FileSystem.java

License:Apache License

@Override
public boolean folderExists(String folder) throws FileSystemException {
    ObjectListing objectListing = s3Client.listObjects(bucketName);
    Iterator<S3ObjectSummary> objIter = objectListing.getObjectSummaries().iterator();
    while (objIter.hasNext()) {
        S3ObjectSummary s3ObjectSummary = objIter.next();
        String key = s3ObjectSummary.getKey();
        if (key.endsWith("/") && key.equals(folder + "/")) {
            return true;
        }//  w  w w  .j a  va  2 s .c  o m
    }
    return false;
}

From source file:ohnosequences.ivy.S3Repository.java

License:Apache License

@Override
public List<String> list(String parent) {
    try {/*w ww .j  ava  2  s . c  o m*/
        String marker = null;
        List<String> keys = new ArrayList<String>();

        do {
            ListObjectsRequest request = new ListObjectsRequest().withBucketName(S3Utils.getBucket(parent))
                    .withPrefix(S3Utils.getKey(parent)).withDelimiter("/") // RFC 2396
                    .withMarker(marker);

            ObjectListing listing = getS3Client().listObjects(request);

            // Add "directories"
            keys.addAll(listing.getCommonPrefixes());

            // Add "files"
            for (S3ObjectSummary summary : listing.getObjectSummaries()) {
                keys.add(summary.getKey());
            }

            marker = listing.getNextMarker();
        } while (marker != null);

        return keys;
    } catch (AmazonServiceException e) {
        throw new S3RepositoryException(e);
    }
}

From source file:opendap.aws.s3.SimpleS3Uploader.java

License:Open Source License

public void listBucket() {
    System.out.println("- - - - - - - - - - - - - - - - - - - - - -");
    System.out.println("S3 Bucket: " + s3BucketName);
    System.out.println("Listing: ");

    long totalSize = 0;
    int totalItems = 0;

    ObjectListing objects = s3.listObjects(s3BucketName);
    do {//from   w w  w.  j  ava2s  .c  o  m
        for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
            System.out.println("   " + objectSummary.getKey() + " " + objectSummary.getSize() + " bytes");
            totalSize += objectSummary.getSize();
            totalItems++;
        }
        objects = s3.listNextBatchOfObjects(objects);
    } while (objects.isTruncated());

    System.out.println("The  Amazon S3 bucket '" + s3BucketName + "'" + "contains " + totalItems
            + " objects with a total size of " + totalSize + " bytes.");

}