Example usage for com.amazonaws.services.s3.model ListObjectsV2Result getCommonPrefixes

List of usage examples for com.amazonaws.services.s3.model ListObjectsV2Result getCommonPrefixes

Introduction

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

Prototype

public List<String> getCommonPrefixes() 

Source Link

Document

Gets the common prefixes included in this object listing.

Usage

From source file:alluxio.underfs.s3a.S3AUnderFileSystem.java

License:Apache License

/**
 * Lists the files in the given path, the paths will be their logical names and not contain the
 * folder suffix. Note that, the list results are unsorted.
 *
 * @param path the key to list//from w  w  w. jav  a 2 s  .c o  m
 * @param recursive if true will list children directories as well
 * @return an array of the file and folder names in this directory
 * @throws IOException if an I/O error occurs
 */
private String[] listInternal(String path, boolean recursive) throws IOException {
    path = stripPrefixIfPresent(path);
    path = PathUtils.normalizePath(path, PATH_SEPARATOR);
    path = path.equals(PATH_SEPARATOR) ? "" : path;
    String delimiter = recursive ? "" : PATH_SEPARATOR;
    Set<String> children = new HashSet<>();
    try {
        ListObjectsV2Request request = new ListObjectsV2Request().withBucketName(mBucketName).withPrefix(path)
                .withDelimiter(delimiter).withMaxKeys(LISTING_LENGTH);
        ListObjectsV2Result result = null;
        while (result == null || result.isTruncated()) {
            // Query S3 for the next batch of objects
            result = mClient.listObjectsV2(request);
            // Advance the request continuation token to the next set of objects
            request.setContinuationToken(result.getNextContinuationToken());

            // Directories in S3 UFS can be possibly encoded in two different ways:
            // (1) as file objects with FOLDER_SUFFIX for directories created through Alluxio or
            // (2) as "common prefixes" of other files objects for directories not created through
            // Alluxio
            //
            // Case (1) (and file objects) is accounted for by iterating over chunk.getObjects() while
            // case (2) is accounted for by iterating over chunk.getCommonPrefixes().
            //
            // An example, with prefix="ufs" and delimiter="/" and LISTING_LENGTH=5
            // - objects.key = ufs/, child =
            // - objects.key = ufs/dir1_$folder$, child = dir1
            // - objects.key = ufs/file, child = file
            // - commonPrefix = ufs/dir1/, child = dir1
            // - commonPrefix = ufs/dir2/, child = dir2

            // Handle case (1)
            for (S3ObjectSummary obj : result.getObjectSummaries()) {
                // Remove parent portion of the key
                String child = getChildName(obj.getKey(), path);
                // Prune the special folder suffix
                child = CommonUtils.stripSuffixIfPresent(child, FOLDER_SUFFIX);
                // Only add if the path is not empty (removes results equal to the path)
                if (!child.isEmpty()) {
                    children.add(child);
                }
            }
            // Handle case (2)
            for (String commonPrefix : result.getCommonPrefixes()) {
                // Remove parent portion of the key
                String child = getChildName(commonPrefix, path);
                // Remove any portion after the last path delimiter
                int childNameIndex = child.lastIndexOf(PATH_SEPARATOR);
                child = childNameIndex != -1 ? child.substring(0, childNameIndex) : child;
                if (!child.isEmpty() && !children.contains(child)) {
                    // This directory has not been created through Alluxio.
                    mkdirsInternal(commonPrefix);
                    children.add(child);
                }
            }
        }
        return children.toArray(new String[children.size()]);
    } catch (AmazonClientException e) {
        LOG.error("Failed to list path {}", path, e);
        return null;
    }
}

From source file:org.exem.flamingo.web.filesystem.s3.S3BrowserController.java

License:Apache License

@RequestMapping(value = "listFolders", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)//from w ww  .  j av a 2s  .c  om
public Response listFolders(@RequestParam(required = false) String bucketName,
        @RequestParam(required = false) String prefix,
        @RequestParam(required = false) String continuationToken) {

    // Get bucket list
    if (StringUtils.isEmpty(bucketName)) {
        Response response = new Response();
        response.getList().addAll(getBucketList());
        response.setSuccess(true);
        return response;
    }

    // Get folder list
    ListObjectsV2Result result = s3BrowserService.listObjects(bucketName, prefix, continuationToken);

    List<S3ObjectInfo> list = new ArrayList<>();
    List<String> commonPrefixes = result.getCommonPrefixes();
    for (String key : commonPrefixes) {
        S3ObjectInfo object = new S3ObjectInfo();
        object.setKey(key);
        object.setName(getName(key));
        object.setBucketName(bucketName);
        object.setFolder(true);
        list.add(object);
    }

    Map<String, String> map = new HashMap<>();
    map.put(S3Constansts.CONTINUATIONTOKEN, result.getNextContinuationToken());
    map.put(S3Constansts.ISTRUNCATED, BooleanUtils.toStringTrueFalse(result.isTruncated()));

    Response response = new Response();
    response.getList().addAll(list);
    response.getMap().putAll(map);
    response.setSuccess(true);
    return response;
}

From source file:org.exem.flamingo.web.filesystem.s3.S3BrowserController.java

License:Apache License

@RequestMapping(value = "listObjects", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)//from w ww.j  a  va2 s  .  c o m
public Response listObjects(@RequestParam(required = false) String bucketName,
        @RequestParam(required = false) String prefix,
        @RequestParam(required = false) String continuationToken) {
    // Get bucket list
    if (StringUtils.isEmpty(bucketName)) {
        Response response = new Response();
        response.getList().addAll(getBucketList());
        response.setSuccess(true);
        return response;
    }

    // Get folder & bucket list
    ListObjectsV2Result result = s3BrowserService.listObjects(bucketName, prefix, continuationToken);

    List<S3ObjectInfo> list = new ArrayList<>();
    List<String> commonPrefixes = result.getCommonPrefixes();
    for (String key : commonPrefixes) {
        S3ObjectInfo object = new S3ObjectInfo();
        object.setBucketName(bucketName);
        object.setKey(key);
        object.setName(getName(key));
        object.setFolder(true);
        list.add(object);
    }

    List<S3ObjectSummary> objectSummaries = result.getObjectSummaries();

    if (!StringUtils.endsWith(prefix, S3Constansts.DELIMITER)) {
        prefix = prefix + S3Constansts.DELIMITER;
    }
    for (S3ObjectSummary s3Object : objectSummaries) {
        String key = s3Object.getKey();
        if (prefix.equals(key)) {
            continue;
        }
        S3ObjectInfo object = new S3ObjectInfo();
        object.setBucketName(bucketName);
        object.setPrefix(prefix);
        object.setKey(key);
        object.setName(getName(key));
        object.setObject(true);
        object.setSize(s3Object.getSize());
        object.setLastModified(s3Object.getLastModified());
        object.setStorageClass(s3Object.getStorageClass());
        list.add(object);
    }

    Map<String, String> map = new HashMap<>();
    map.put(S3Constansts.CONTINUATIONTOKEN, result.getNextContinuationToken());
    map.put(S3Constansts.ISTRUNCATED, BooleanUtils.toStringTrueFalse(result.isTruncated()));

    Response response = new Response();
    response.getList().addAll(list);
    response.getMap().putAll(map);
    response.setSuccess(true);
    return response;
}