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

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

Introduction

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

Prototype

public String getBucketName() 

Source Link

Document

Gets the name of the Amazon S3 bucket in which this object is stored.

Usage

From source file:org.apache.nifi.minifi.c2.cache.s3.S3WritableConfiguration.java

License:Apache License

/**
 * Creates a new S3 writable configuration.
 * @param s3 An S3 {@link AmazonS3 client}.
 * @param s3ObjectSummary The S3 object {@link S3ObjectSummary summary}.
 * @param version The version of the configuration.
 *///from  w w w. j  a  va 2  s .c o m
public S3WritableConfiguration(AmazonS3 s3, S3ObjectSummary s3ObjectSummary, String version) {

    this.s3 = s3;
    this.s3Object = s3.getObject(s3ObjectSummary.getBucketName(), s3ObjectSummary.getKey());
    this.version = version;

}

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(/*  ww  w. j  a v  a  2  s .  c o m*/
            "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.openflamingo.fs.s3.S3ObjectProvider.java

License:Apache License

public List<FileInfo> getFiles(String path) {
    String bucket = null;//from   w  ww. j a va  2  s.  c o  m

    if (!"/".equals(path)) {
        bucket = S3Utils.getBucket(path + "/");
    }

    String relativePath = S3Utils.getObjectKey(path);

    List<FileInfo> filesList = new ArrayList<FileInfo>();
    if ("".equals(relativePath)) {
        return filesList;
    }

    try {
        ObjectListing objectListing = awsClient.listObjects(
                new ListObjectsRequest().withBucketName(bucket).withPrefix(relativePath).withDelimiter("/"));

        while (true) {
            List<S3ObjectSummary> summaries = objectListing.getObjectSummaries();
            for (S3ObjectSummary objectSummary : summaries) {
                if (!objectSummary.getKey().endsWith("/")) {
                    long size = objectSummary.getSize();
                    String filename = FileUtils.getFilename(objectSummary.getKey());
                    String bucketName = objectSummary.getBucketName();
                    long modified = objectSummary.getLastModified().getTime();
                    S3ObjectInfo info = new S3ObjectInfo(bucketName, objectSummary.getKey(), filename, modified,
                            size);
                    filesList.add(info);
                }
            }

            if (!objectListing.isTruncated()) {
                break;
            }
            objectListing = awsClient.listNextBatchOfObjects(objectListing);

        }

        return filesList;
    } catch (Exception ase) {
        //            throw new FileSystemException("? ? ? ? ? ?.", ase);
        throw new FileSystemException("An error has occurred.", ase);
    }
}

From source file:org.openflamingo.fs.s3.S3ObjectProvider.java

License:Apache License

@Override
public boolean delete(String path) {
    Assert.hasLength(path, "Please enter the file path.");

    if (S3Utils.isDirectory(path)) {
        ObjectListing objectListing = awsClient.listObjects(new ListObjectsRequest()
                .withBucketName(S3Utils.getBucket(path)).withPrefix(S3Utils.getObjectKey(path)));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            awsClient.deleteObject(objectSummary.getBucketName(), objectSummary.getKey());
        }//  ww w  . ja  v a2 s . c o  m
    } else {
        String bucket = S3Utils.getBucket(path);
        String relativePath = StringUtils.remove(path, "/" + bucket + "/");
        awsClient.deleteObject(bucket, relativePath);
    }
    // auditService.delete(FileSystemType.S3, username, path);
    return true;
}

From source file:org.openinfinity.cloud.domain.repository.deployer.BucketRepositoryAWSImpl.java

License:Apache License

public void deleteBucketAndObjects(String bucketName) {
    try {//from   www  .  j a va  2  s  . c o m
        // list and delete objects in a bucket 
        LOGGER.debug("deleteBucketAndObjects called for bucket: <" + bucketName + ">.");
        ObjectListing objects = simpleStorageService.listObjects(bucketName);

        do {
            for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                LOGGER.debug("Trying to delete object <" + objectSummary.getKey() + "> from bucket <: "
                        + objectSummary.getBucketName() + ">.");
                simpleStorageService.deleteObject(bucketName, objectSummary.getKey());
            }
            objects = simpleStorageService.listNextBatchOfObjects(objects);
        } while (objects.isTruncated());

        // Now that the bucket is empty, you can delete it. If you try to delete your bucket before it is empty, it will fail.
        LOGGER.debug("Trying to delete bucket <: " + bucketName + ">.");
        simpleStorageService.deleteBucket(bucketName);
        //System.out.println("Deleted bucket " + testBucket.getName());      

    } catch (Exception e) {
        // TODO: handle exception
        LOGGER.warn("Error in deleting objects and bucket: " + e + " -- " + e.getStackTrace().toString());
        e.printStackTrace();
        ExceptionUtil.throwSystemException(e.getMessage(), ExceptionLevel.ERROR,
                BucketRepository.EXCEPTION_MESSAGE_CONNECTION_FAILURE);
    }
}

From source file:org.springframework.aws.ivy.S3Resource.java

License:Apache License

public S3Resource(AmazonS3 service, S3ObjectSummary summary) {
    this.service = service;
    this.summary = summary;
    this.uri = "s3://" + summary.getBucketName() + "/" + summary.getKey();
}

From source file:org.springframework.integration.aws.s3.core.AmazonS3OperationsImpl.java

License:Apache License

public PaginatedObjectsView listObjects(String bucketName, String folder, String nextMarker, int pageSize) {
    if (logger.isDebugEnabled()) {
        logger.debug("Listing objects from bucket " + bucketName + " and folder " + folder);
        logger.debug("Next marker is " + nextMarker + " and pageSize is " + pageSize);
    }/*  w  w w. j  a v a2s. co m*/

    Assert.notNull(StringUtils.hasText(bucketName), "Bucket name should be non null and non empty");
    String prefix = null;
    if (folder != null && !"/".equals(folder)) {
        prefix = folder;
    }
    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName)
            .withPrefix(prefix).withMarker(nextMarker);

    if (pageSize > 0) {
        listObjectsRequest.withMaxKeys(pageSize);
    }

    ObjectListing listing = client.listObjects(listObjectsRequest);
    PaginatedObjectsView view = null;
    List<com.amazonaws.services.s3.model.S3ObjectSummary> summaries = listing.getObjectSummaries();
    if (summaries != null && !summaries.isEmpty()) {
        List<S3ObjectSummary> objectSummaries = new ArrayList<S3ObjectSummary>();
        for (final com.amazonaws.services.s3.model.S3ObjectSummary summary : summaries) {
            S3ObjectSummary summ = new S3ObjectSummary() {

                public long getSize() {
                    return summary.getSize();
                }

                public Date getLastModified() {
                    return summary.getLastModified();
                }

                public String getKey() {
                    return summary.getKey();
                }

                public String getETag() {
                    return summary.getETag();
                }

                public String getBucketName() {
                    return summary.getBucketName();
                }
            };
            objectSummaries.add(summ);
        }
        view = new PagninatedObjectsViewImpl(objectSummaries, listing.getNextMarker());
    }
    return view;
}

From source file:org.springframework.integration.aws.s3.core.DefaultAmazonS3Operations.java

License:Apache License

/**
 * The implementation that uses the AWS SDK to list objects from the given bucket
 *
 * @param bucketName The bucket in which we want to list the objects in
 * @param nextMarker The number of objects can be very large and this serves as the marker
 *                 for remembering the last record fetch in the last retrieve operation.
 * @param pageSize The max number of records to be retrieved in one list object operation.
 * @param prefix The prefix for the list operation, this can serve as the folder whose contents
 *              are to be listed./*from w  w w. ja v  a2  s.  c o  m*/
 */
@Override
protected PaginatedObjectsView doListObjects(String bucketName, String nextMarker, int pageSize,
        String prefix) {

    ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName)
            .withPrefix(prefix).withMarker(nextMarker);

    if (pageSize > 0) {
        listObjectsRequest.withMaxKeys(pageSize);
    }

    ObjectListing listing = client.listObjects(listObjectsRequest);
    PaginatedObjectsView view = null;
    List<com.amazonaws.services.s3.model.S3ObjectSummary> summaries = listing.getObjectSummaries();
    if (summaries != null && !summaries.isEmpty()) {
        List<S3ObjectSummary> objectSummaries = new ArrayList<S3ObjectSummary>();
        for (final com.amazonaws.services.s3.model.S3ObjectSummary summary : summaries) {
            S3ObjectSummary summ = new S3ObjectSummary() {

                public long getSize() {
                    return summary.getSize();
                }

                public Date getLastModified() {
                    return summary.getLastModified();
                }

                public String getKey() {
                    return summary.getKey();
                }

                public String getETag() {
                    return summary.getETag();
                }

                public String getBucketName() {
                    return summary.getBucketName();
                }
            };
            objectSummaries.add(summ);
        }
        view = new PagninatedObjectsViewImpl(objectSummaries, listing.getNextMarker());
    }
    return view;
}

From source file:org.symphonyoss.vb.services.JsonPersist.java

License:Apache License

public static Set<VoteSession> getVoteSessions() throws Exception {

    Set<VoteSession> voteSessions = new HashSet<>();
    Gson gson = new Gson();

    if (Boolean.parseBoolean(System.getProperty(BotConfig.S3_ENABLED))) {

        AwsS3Client awsS3Client = new AwsS3Client();
        List<S3ObjectSummary> allObjects = awsS3Client.getAllObjects(System.getProperty(BotConfig.S3_BUCKET),
                System.getProperty(BotConfig.VB_S3_PREFIX_JSON));

        for (S3ObjectSummary objectSummary : allObjects) {

            if (!objectSummary.getKey().contains(".json"))
                continue;

            logger.info("Loading vote session from aws cache [s3://{}/{}]", objectSummary.getBucketName(),
                    objectSummary.getKey());

            VoteProposal voteProposal = gson
                    .fromJson(new InputStreamReader(awsS3Client.getObject(objectSummary)), VoteProposal.class);
            voteSessions.add(voteProposal);

        }//from  w w w  .  j a  va2  s .  c  om

    } else {
        File[] files = new File(System.getProperty("files.json")).listFiles();

        if (files == null) {
            logger.error("Failed to load locate directory [{}] for json pre-load..exiting",
                    System.getProperty("files.json"));
            System.exit(1);
        }

        for (File file : files) {

            if (!file.getName().contains(".json"))
                continue;

            logger.info("Loading vote session from cache [{}]", file.getName());
            try {
                VoteProposal voteProposal = gson.fromJson(new FileReader(file), VoteProposal.class);
                voteSessions.add(voteProposal);

            } catch (IOException e) {
                logger.error("Could not load json {} ", file.getName(), e);
                throw (e);
            }
        }

    }
    return voteSessions;

}

From source file:org.symphonyoss.vb.util.AwsS3Client.java

License:Apache License

public InputStream getObject(S3ObjectSummary objectSummary) {

    S3Object object = null;//from ww  w. j a  v  a2 s .c o m

    try {
        logger.info("Retrieving object inputstream for s3://{}/{}", objectSummary.getBucketName(),
                objectSummary.getKey());
        object = s3Client
                .getObject(new GetObjectRequest(objectSummary.getBucketName(), objectSummary.getKey()));

    } catch (AmazonServiceException ase) {
        logger.error("Caught an AmazonServiceException, " + "which means your request made it "
                + "to Amazon S3, but was rejected with an error response " + "for some reason.");
        logger.error("Error Message:    " + ase.getMessage());
        logger.error("HTTP Status Code: " + ase.getStatusCode());
        logger.error("AWS Error Code:   " + ase.getErrorCode());
        logger.error("Error Type:       " + ase.getErrorType());
        logger.error("Request ID:       " + ase.getRequestId());

    } catch (AmazonClientException ace) {
        logger.error("Caught an AmazonClientException, " + "which means the client encountered "
                + "an internal error while trying to communicate" + " with S3, "
                + "such as not being able to access the network.");
        logger.error("Error Message: " + ace.getMessage());
    }
    return object == null ? null : object.getObjectContent();
}