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:org.exem.flamingo.web.filesystem.s3.S3BrowserController.java

License:Apache License

@RequestMapping(value = "listObjects", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)//from  w w w . j  a  va2s  .  co  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;
}

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

License:Apache License

@Override
public void deleteBucket(String bucketName) {
    ListObjectsV2Request request = new ListObjectsV2Request();
    request.withBucketName(bucketName);/*from w  w w .  j a v a2 s  .  c  o m*/
    ListObjectsV2Result result;

    while (true) {
        result = this.s3.listObjectsV2(request);
        for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
            this.s3.deleteObject(bucketName, objectSummary.getKey());
        }
        if (result.isTruncated()) {
            request.setContinuationToken(result.getNextContinuationToken());
        } else {
            break;
        }
    }

    this.s3.deleteBucket(bucketName);
}

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

License:Apache License

@Override
public void deleteFolder(String bucketName, String key) {
    ListObjectsV2Request request = new ListObjectsV2Request();
    request.withBucketName(bucketName).withPrefix(key).withDelimiter(S3Constansts.DELIMITER);

    ListObjectsV2Result result;//from   www  . ja  v a  2  s .c  o m

    while (true) {
        result = this.s3.listObjectsV2(request);
        for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
            this.s3.deleteObject(bucketName, objectSummary.getKey());
        }
        if (result.isTruncated()) {
            request.setContinuationToken(result.getNextContinuationToken());
        } else {
            break;
        }
    }
}

From source file:org.fcrepo.modeshape.binary.S3BinaryStore.java

License:Apache License

@Override
public Iterable<BinaryKey> getAllBinaryKeys() throws BinaryStoreException {
    try {//from  w w  w.  ja va  2 s  . co  m
        final Iterator<S3ObjectSummary> objectsIterator = S3Objects.inBucket(s3Client, bucketName).iterator();
        // Lambda to hand back BinaryKeys rather than S3ObjectSummaries
        return () -> {
            return new Iterator<BinaryKey>() {
                @Override
                public boolean hasNext() {
                    return objectsIterator.hasNext();
                }

                @Override
                public BinaryKey next() {
                    S3ObjectSummary object = objectsIterator.next();
                    return new BinaryKey(object.getKey());
                }
            };
        };
    } catch (AmazonClientException e) {
        throw new BinaryStoreException(e);
    }
}

From source file:org.finra.dm.dao.impl.S3DaoImpl.java

License:Apache License

/**
 * Lists all S3 objects matching the S3 key prefix in the given bucket (S3 bucket name). The S3 bucket name and S3 key prefix that identify the S3 objects
 * to get listed are taken from the S3 file transfer request parameters DTO.
 *
 * @param params the S3 file transfer request parameters
 * @param ignoreZeroByteDirectoryMarkers specifies whether to ignore 0 byte objects that represent S3 directories
 *
 * @return the list of all S3 objects represented as storage files that match the prefix in the given bucket
 *///from  w  w  w. java 2s.co  m
private List<StorageFile> listObjectsMatchingKeyPrefix(final S3FileTransferRequestParamsDto params,
        boolean ignoreZeroByteDirectoryMarkers) {
    AmazonS3Client s3Client = null;
    List<StorageFile> storageFiles = new ArrayList<>();

    try {
        s3Client = getAmazonS3(params);
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(params.getS3BucketName()).withPrefix(params.getS3KeyPrefix());
        ObjectListing objectListing;

        do {
            objectListing = s3Operations.listObjects(listObjectsRequest, s3Client);

            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                // Ignore 0 byte objects that represent S3 directories.
                if (!(ignoreZeroByteDirectoryMarkers && objectSummary.getKey().endsWith("/")
                        && objectSummary.getSize() == 0L)) {
                    storageFiles.add(new StorageFile(objectSummary.getKey(), objectSummary.getSize(), null));
                }
            }

            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonS3Exception amazonS3Exception) {
        if (S3Operations.ERROR_CODE_NO_SUCH_BUCKET.equals(amazonS3Exception.getErrorCode())) {
            throw new IllegalArgumentException(
                    "The specified bucket '" + params.getS3BucketName() + "' does not exist.",
                    amazonS3Exception);
        }
        throw new IllegalStateException("Error accessing S3", amazonS3Exception);
    } catch (AmazonClientException e) {
        throw new IllegalStateException(
                String.format("Failed to list keys/objects with prefix \"%s\" from bucket \"%s\". Reason: %s",
                        params.getS3KeyPrefix(), params.getS3BucketName(), e.getMessage()),
                e);
    } finally {
        // Shutdown the AmazonS3Client instance to release resources.
        if (s3Client != null) {
            s3Client.shutdown();
        }
    }

    return storageFiles;
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

License:Apache License

@Override
public List<S3ObjectSummary> listDirectory(final S3FileTransferRequestParamsDto params,
        boolean ignoreZeroByteDirectoryMarkers) {
    Assert.isTrue(!isRootKeyPrefix(params.getS3KeyPrefix()),
            "Listing of S3 objects from root directory is not allowed.");

    AmazonS3Client s3Client = getAmazonS3(params);
    List<S3ObjectSummary> s3ObjectSummaries = new ArrayList<>();

    try {/*from w ww  .ja  v a2s  . co  m*/
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
                .withBucketName(params.getS3BucketName()).withPrefix(params.getS3KeyPrefix());
        ObjectListing objectListing;

        do {
            objectListing = s3Operations.listObjects(listObjectsRequest, s3Client);

            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                // Ignore 0 byte objects that represent S3 directories.
                if (!(ignoreZeroByteDirectoryMarkers && objectSummary.getKey().endsWith("/")
                        && objectSummary.getSize() == 0L)) {
                    s3ObjectSummaries.add(objectSummary);
                }
            }

            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonS3Exception amazonS3Exception) {
        if (S3Operations.ERROR_CODE_NO_SUCH_BUCKET.equals(amazonS3Exception.getErrorCode())) {
            throw new IllegalArgumentException(
                    "The specified bucket '" + params.getS3BucketName() + "' does not exist.",
                    amazonS3Exception);
        }
        throw new IllegalStateException("Error accessing S3", amazonS3Exception);
    } catch (AmazonClientException e) {
        throw new IllegalStateException(
                String.format("Failed to list keys with prefix \"%s\" from bucket \"%s\". Reason: %s",
                        params.getS3KeyPrefix(), params.getS3BucketName(), e.getMessage()),
                e);
    } finally {
        // Shutdown the AmazonS3Client instance to release resources.
        s3Client.shutdown();
    }

    return s3ObjectSummaries;
}

From source file:org.finra.herd.dao.impl.S3DaoImpl.java

License:Apache License

@Override
public void tagObjects(final S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto,
        final S3FileTransferRequestParamsDto s3ObjectTaggerParamsDto,
        final List<S3ObjectSummary> s3ObjectSummaries, final Tag tag) {
    LOGGER.info(//from w  w  w .  ja v  a  2 s  .c  om
            "Tagging objects in S3... s3BucketName=\"{}\" s3KeyPrefix=\"{}\" s3KeyCount={} s3ObjectTagKey=\"{}\" s3ObjectTagValue=\"{}\"",
            s3FileTransferRequestParamsDto.getS3BucketName(), s3FileTransferRequestParamsDto.getS3KeyPrefix(),
            CollectionUtils.size(s3ObjectSummaries), tag.getKey(), tag.getValue());

    if (!CollectionUtils.isEmpty(s3ObjectSummaries)) {
        // Convert a list of S3 object summaries to S3 version summaries without version identifiers.
        List<S3VersionSummary> s3VersionSummaries = new ArrayList<>();
        for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
            S3VersionSummary s3VersionSummary = new S3VersionSummary();
            s3VersionSummary.setBucketName(s3ObjectSummary.getBucketName());
            s3VersionSummary.setKey(s3ObjectSummary.getKey());
            s3VersionSummaries.add(s3VersionSummary);
        }

        // Tag S3 objects.
        tagVersionsHelper(s3FileTransferRequestParamsDto, s3ObjectTaggerParamsDto, s3VersionSummaries, tag);

        // Log a list of files tagged in the S3 bucket.
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Successfully tagged files in S3 bucket. "
                    + "s3BucketName=\"{}\" s3KeyPrefix=\"{}\" s3KeyCount={} s3ObjectTagKey=\"{}\" s3ObjectTagValue=\"{}\"",
                    s3FileTransferRequestParamsDto.getS3BucketName(),
                    s3FileTransferRequestParamsDto.getS3KeyPrefix(), CollectionUtils.size(s3ObjectSummaries),
                    tag.getKey(), tag.getValue());

            for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
                LOGGER.info("s3Key=\"{}\"", s3ObjectSummary.getKey());
            }
        }
    }
}

From source file:org.finra.herd.dao.S3DaoTestHelper.java

License:Apache License

/**
 * Validates uploaded S3 files.//from ww  w . ja  va 2 s  .c  o  m
 *
 * @param s3FileTransferRequestParamsDto the S3 file transfer request parameters DTO
 * @param expectedS3Keys the list of expected S3 keys
 */
public void validateS3FileUpload(S3FileTransferRequestParamsDto s3FileTransferRequestParamsDto,
        List<String> expectedS3Keys) {
    // Validate the upload.
    List<S3ObjectSummary> s3ObjectSummaries = s3Dao.listDirectory(s3FileTransferRequestParamsDto);
    assertTrue(s3ObjectSummaries.size() == expectedS3Keys.size());

    // Build a list of the actual S3 keys.
    List<String> actualS3Keys = new ArrayList<>();
    for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
        actualS3Keys.add(s3ObjectSummary.getKey());
    }

    // Check that all local test files got uploaded.
    assertTrue(expectedS3Keys.containsAll(actualS3Keys));
    assertTrue(actualS3Keys.containsAll(expectedS3Keys));
}

From source file:org.finra.herd.service.helper.StorageFileHelper.java

License:Apache License

/**
 * Creates a list of storage files from the list of S3 object summaries.
 *
 * @param s3ObjectSummaries the list of S3 object summaries
 *
 * @return the list of storage files//from w  ww .ja v a  2 s .c o m
 */
public List<StorageFile> createStorageFilesFromS3ObjectSummaries(List<S3ObjectSummary> s3ObjectSummaries) {
    List<StorageFile> storageFiles = new ArrayList<>();

    for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
        storageFiles.add(new StorageFile(s3ObjectSummary.getKey(), s3ObjectSummary.getSize(), null));
    }

    return storageFiles;
}

From source file:org.finra.herd.service.helper.StorageFileHelper.java

License:Apache License

/**
 * Returns a list of file paths extracted from the specified list of S3 object summaries.
 *
 * @param s3ObjectSummaries the list of of S3 object summaries
 *
 * @return the list of file paths/*from  w w w  . j a v a  2s  .c  o m*/
 */
public List<String> getFilePathsFromS3ObjectSummaries(List<S3ObjectSummary> s3ObjectSummaries) {
    List<String> filePaths = new ArrayList<>();

    for (S3ObjectSummary s3ObjectSummary : s3ObjectSummaries) {
        filePaths.add(s3ObjectSummary.getKey());
    }

    return filePaths;
}