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

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

Introduction

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

Prototype

public String getPrefix() 

Source Link

Document

Gets the prefix parameter originally used to request this object listing, or null if no prefix was specified.

Usage

From source file:com.digitaslbi.helios.mock.utils.ConnectionHelper.java

private static Map convertResultToFile(ListObjectsRequest listObjectsRequest, String path, String parentPath) {
    Map<String, File> filesMap = new HashMap<String, File>();
    File aux;/*from   ww  w .  j  a v  a  2s  .c om*/

    ObjectListing objListing = s3Client.listObjects(listObjectsRequest);

    // creates the parent elemment
    aux = new File();
    aux.setIsParent(true);
    aux.setFullPath(parentPath);
    aux.setPath(parentPath);

    // adapts the path in order to use as map key
    if (aux.getPath() != null) {
        aux.setPath(clearPathDelimiter(aux.getPath()));

        if (aux.getPath().lastIndexOf(MocksConstants.AWS_PARENT_DELIMITER.getValue()) > 0) {
            aux.setPath(aux.getPath()
                    .substring(aux.getPath().lastIndexOf(MocksConstants.AWS_PARENT_DELIMITER.getValue()) + 1));
        }
    }
    filesMap.put(aux.getPath(), aux);

    for (S3ObjectSummary objSummary : objListing.getObjectSummaries()) {
        aux = new File();
        aux.setFullPath(objSummary.getKey());
        aux.setParent(objListing.getPrefix());

        // if size is 0 is considered a folder
        aux.setIsFile((objSummary.getSize() == 0) ? false : true);

        if (aux.getParent() != null) {
            if (!aux.getFullPath().equals(aux.getParent())) {
                aux.setPath(aux.getFullPath().replace(aux.getParent(), ""));
            }
        } else {
            aux.setPath(aux.getFullPath());
        }

        if (aux.getPath() != null) {
            filesMap.put(clearPathDelimiter(aux.getPath()), aux);
        }
    }

    for (String folderNames : objListing.getCommonPrefixes()) {
        aux = new File();
        aux.setFullPath(folderNames);
        aux.setParent(objListing.getPrefix());
        aux.setIsFile(false);

        if (aux.getParent() != null) {
            aux.setPath(aux.getFullPath().replace(aux.getParent(), ""));
        } else {
            aux.setPath(aux.getFullPath());
        }

        if (aux.getPath() != null) {
            filesMap.put(clearPathDelimiter(aux.getPath()), aux);
        }
    }

    return filesMap;
}

From source file:com.digitaslbi.helios.utils.S3Helper.java

public static Folder getFolder(String path) {
    ListObjectsRequest listObjectsRequest;
    Folder folder;//w w w . jav a 2s.c o m
    File aux;

    if (path == null || path.equals("/")) {
        listObjectsRequest = getRootFolders();
    } else {
        listObjectsRequest = getContentByPreffix(path);
    }

    ObjectListing objListing = s3Client.listObjects(listObjectsRequest);

    folder = new Folder();
    folder.setFiles(new ArrayList<File>());

    folder.setPath(path);
    folder.setParent(obtainParentPath(path));

    for (S3ObjectSummary objSummary : objListing.getObjectSummaries()) {
        aux = new File();
        aux.setPath(objSummary.getKey());
        aux.setParent(objListing.getPrefix());

        // if size is 0 is considered a folder
        aux.setIsFile((objSummary.getSize() == 0) ? false : true);

        if (!aux.getPath().equals(path)) {
            folder.getFiles().add(aux);
        }
    }

    for (String folderNames : objListing.getCommonPrefixes()) {
        aux = new File();
        aux.setPath(folderNames);
        aux.setParent(objListing.getPrefix());
        aux.setIsFile(false);

        folder.getFiles().add(aux);
    }

    log.info("[S3Helper][getFolder] Path: " + path + " items found: " + folder.getFiles().size());

    return folder;
}

From source file:com.eucalyptus.objectstorage.providers.s3.S3ProviderClient.java

License:Open Source License

@Override
public ListBucketResponseType listBucket(ListBucketType request) throws S3Exception {
    ListBucketResponseType reply = request.getReply();
    User requestUser = getRequestUser(request);
    OsgInternalS3Client internalS3Client = null;
    try {/*  w w w  .j av  a 2  s.c o m*/
        internalS3Client = getS3Client(requestUser);
        AmazonS3Client s3Client = internalS3Client.getS3Client();
        ListObjectsRequest listRequest = new ListObjectsRequest();
        listRequest.setBucketName(request.getBucket());
        listRequest.setDelimiter(Strings.isNullOrEmpty(request.getDelimiter()) ? null : request.getDelimiter());
        listRequest.setMarker(Strings.isNullOrEmpty(request.getMarker()) ? null : request.getMarker());
        listRequest.setMaxKeys((request.getMaxKeys() == null ? null : Integer.parseInt(request.getMaxKeys())));
        listRequest.setPrefix(Strings.isNullOrEmpty(request.getPrefix()) ? null : request.getPrefix());

        ObjectListing response = s3Client.listObjects(listRequest);

        /* Non-optional, must have non-null values */
        reply.setName(request.getBucket());
        reply.setMaxKeys(response.getMaxKeys());
        reply.setMarker(response.getMarker() == null ? "" : response.getMarker());
        reply.setPrefix(response.getPrefix() == null ? "" : response.getPrefix());
        reply.setIsTruncated(response.isTruncated());

        /* Optional */
        reply.setNextMarker(response.getNextMarker());
        reply.setDelimiter(response.getDelimiter());
        if (reply.getContents() == null) {
            reply.setContents(new ArrayList<ListEntry>());
        }
        if (reply.getCommonPrefixesList() == null) {
            reply.setCommonPrefixesList(new ArrayList<CommonPrefixesEntry>());
        }

        for (S3ObjectSummary obj : response.getObjectSummaries()) {
            //Add entry, note that the canonical user is set based on requesting user, not returned user
            reply.getContents()
                    .add(new ListEntry(obj.getKey(),
                            DateFormatter.dateToHeaderFormattedString(obj.getLastModified()), obj.getETag(),
                            obj.getSize(), getCanonicalUser(requestUser), obj.getStorageClass()));
        }

        if (response.getCommonPrefixes() != null && response.getCommonPrefixes().size() > 0) {
            reply.setCommonPrefixesList(new ArrayList<CommonPrefixesEntry>());

            for (String s : response.getCommonPrefixes()) {
                reply.getCommonPrefixesList().add(new CommonPrefixesEntry(s));
            }
        }

        return reply;
    } catch (AmazonServiceException e) {
        LOG.debug("Error from backend", e);
        throw S3ExceptionMapper.fromAWSJavaSDK(e);
    }
}

From source file:com.upplication.s3fs.util.AmazonS3ClientMock.java

License:Open Source License

@Override
public ObjectListing listNextBatchOfObjects(ObjectListing previousObjectListing) {

    ObjectListing objectListing = new ObjectListing();
    objectListing.setBucketName(previousObjectListing.getBucketName());
    objectListing.setPrefix(previousObjectListing.getPrefix());
    objectListing.setMarker(previousObjectListing.getMarker());
    objectListing.setDelimiter(previousObjectListing.getDelimiter());

    if (!previousObjectListing.isTruncated() || previousObjectListing.getNextMarker() == null) {
        return objectListing;
    }/*from  ww w. ja  va 2s  . c om*/

    Bucket bucket = find(previousObjectListing.getBucketName());
    Iterator<S3Element> iterator = objects.get(bucket).iterator();

    int i = 0;
    boolean continueElement = false;

    while (iterator.hasNext()) {

        S3Element elem = iterator.next();

        if (!continueElement && elem.getS3Object().getKey().equals(previousObjectListing.getNextMarker())) {
            continueElement = true;
        }

        if (continueElement) {
            // TODO. add delimiter and marker support
            if (previousObjectListing.getPrefix() != null
                    && elem.getS3Object().getKey().startsWith(previousObjectListing.getPrefix())) {

                S3ObjectSummary s3ObjectSummary = parseToS3ObjectSummary(elem);
                objectListing.getObjectSummaries().add(s3ObjectSummary);
                // max 1000 elements at same time.
                if (i + 1 == LIMIT_AWS_MAX_ELEMENTS && iterator.hasNext()) {
                    objectListing.setTruncated(true);
                    objectListing.setNextMarker(iterator.next().getS3Object().getKey());
                    return objectListing;
                } else {
                    objectListing.setTruncated(false);
                }

                i++;
            }
        }
    }

    return objectListing;
}

From source file:org.gradle.internal.resource.transport.aws.s3.S3ResourceResolver.java

License:Apache License

private List<String> resolveDirectoryResourceNames(ObjectListing objectListing) {
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    if (objectListing.getCommonPrefixes() != null) {
        for (String prefix : objectListing.getCommonPrefixes()) {
            /**/*  ww  w . j a  va  2  s  .co m*/
             * The common prefixes will also include the prefix of the <code>ObjectListing</code>
             */
            String directChild = prefix.split(Pattern.quote(objectListing.getPrefix()))[1];
            if (directChild.endsWith("/")) {
                builder.add(directChild.substring(0, directChild.length() - 1));
            } else {
                builder.add(directChild);
            }
        }
        return builder.build();
    }
    return Collections.emptyList();
}