Example usage for com.amazonaws.services.s3 AmazonS3Client listBuckets

List of usage examples for com.amazonaws.services.s3 AmazonS3Client listBuckets

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Client listBuckets.

Prototype

@Override
    public List<Bucket> listBuckets() throws SdkClientException, AmazonServiceException 

Source Link

Usage

From source file:com.appdynamics.monitors.s3.AWSS3Monitor.java

License:Apache License

/**
 * This method calls Amazon WS to get required S3 statistics, set values
 * based on configured unit, and returns the result back
 * // w  ww. j a  va  2s .  c o m
 * @param buckets
 * @param amazonS3Client
 * @return Map<String, String>
 * @throws TaskExecutionException
 */
private Map<String, String> getS3Result(List<Bucket> buckets, AmazonS3Client amazonS3Client)
        throws TaskExecutionException {
    // Declaring result variables with default values
    long size = 0;
    long count = 0;
    Date lastModified = new Date(0);

    try {
        // Fetching all bucket names if passed buckets is null
        if (buckets == null) {
            logger.debug("Calling Webservice to list all buckets");
            buckets = amazonS3Client.listBuckets();
        }

        // Looping over all buckets
        for (Bucket bucket : buckets) {

            logger.debug("Getting data for bucket: " + bucket.getName());

            ObjectListing objectListing = null;

            do {
                // Getting objectListing while calling it for the first time
                if (objectListing == null) {
                    logger.debug("Calling Webservice to get objectlisting for first time");
                    objectListing = amazonS3Client.listObjects(bucket.getName());
                } else {
                    // Calling listNextBatchOfObjects if previous response
                    // is truncated
                    logger.debug("Calling Webservice to get objectlisting subsequent time");
                    objectListing = amazonS3Client.listNextBatchOfObjects(objectListing);
                }

                // Incrementing the count
                count += objectListing.getObjectSummaries().size();

                // Looping over all objects
                for (S3ObjectSummary s3ObjectSummary : objectListing.getObjectSummaries()) {
                    // Incrementing size
                    size += s3ObjectSummary.getSize();

                    // Setting last modified if lastModifiedDate is latest
                    if (lastModified.before(s3ObjectSummary.getLastModified())) {
                        lastModified = s3ObjectSummary.getLastModified();
                    }
                }
            }

            // Continuing till objectListing is complete
            while (objectListing.isTruncated());
        }

    } catch (AmazonS3Exception exception) {
        logger.error("AmazonS3Exception occurred", exception);
        throw new TaskExecutionException("Sending S3 metric failed due to AmazonS3Exception");
    }

    return getResultWithRequiredUnit(size, count, lastModified);
}

From source file:com.nike.cerberus.module.CerberusModule.java

License:Apache License

private Optional<String> findBucket(final String environmentName) {
    AmazonS3Client s3Client = new AmazonS3Client();
    List<Bucket> buckets = s3Client.listBuckets();

    String envBucket = null;//from  w  w  w .  j  a  v  a 2  s . co  m
    for (final Bucket bucket : buckets) {
        if (StringUtils.contains(bucket.getName(), ConfigConstants.CONFIG_BUCKET_KEY)) {
            String[] parts = bucket.getName().split("-");
            if (StringUtils.equalsIgnoreCase(environmentName, parts[0])) {
                envBucket = bucket.getName();
                break;
            }
        }
    }

    return Optional.ofNullable(envBucket);
}

From source file:com.optimalbi.AmazonAccount.java

License:Apache License

private void populateS3() throws AmazonClientException {
    AmazonS3Client client = new AmazonS3Client(credentials.getCredentials());
    client.setRegion(regions.get(0));//  w ww. j a v  a2s. com
    List<Bucket> buckets = client.listBuckets();
    for (Bucket bucket : buckets) {
        Service temp = new LocalS3Service(bucket.getName(), credentials, regions.get(0), bucket, logger);
        services.add(temp);
    }
}

From source file:org.kuali.maven.wagon.S3Wagon.java

License:Educational Community License

protected Bucket getOrCreateBucket(AmazonS3Client client, String bucketName) {
    List<Bucket> buckets = client.listBuckets();
    for (Bucket bucket : buckets) {
        if (bucket.getName().equals(bucketName)) {
            return bucket;
        }//from   w  w  w  .j a  va  2s.  co  m
    }
    return client.createBucket(bucketName);
}

From source file:org.lendingclub.mercator.aws.S3BucketScanner.java

License:Apache License

@Override
protected void doScan() {
    AmazonS3Client client = getClient();

    List<Bucket> bucketList = client.listBuckets();

    for (Bucket bucket : bucketList) {
        scanBucket(bucket);//from w ww  . j a va2s. co  m
    }

}

From source file:org.opendedup.sdfs.filestore.cloud.BatchAwsS3ChunkStore.java

License:Open Source License

public static boolean checkAuth(String awsAccessKey, String awsSecretKey) {
    BasicAWSCredentials creds = null;/*from   w  w w  . j  a  v  a2  s  .  c om*/
    try {
        creds = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
        AmazonS3Client s3Service = new AmazonS3Client(creds);
        s3Service.listBuckets();
        s3Service.shutdown();
        return true;
    } catch (Exception e) {
        SDFSLogger.getLog().fatal("Unable to authenticate to AWS", e);
        return false;
    }
}

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

License:Apache License

/**
 * Bucket  .//ww  w .  j ava 2  s .  c  o  m
 *
 * @param client     Amazon S3 Client
 * @param bucketName Bucket Name
 * @return Bucket?   Bucket ,  ?  <tt>null</tt>? 
 */
public static Bucket getBucket(AmazonS3Client client, String bucketName) {
    List<Bucket> buckets = client.listBuckets();
    for (Bucket bucket : buckets) {
        if (bucketName.equals(bucket.getName())) {
            return bucket;
        }
    }
    return null;
}

From source file:ubicrypt.ui.ctrl.S3ProviderController.java

License:Open Source License

public void listBuckets() {
    AmazonS3Client client = new AmazonS3Client(new AWSCredentials() {
        @Override//w  ww.  j  av a  2s  . c o  m
        public String getAWSAccessKeyId() {
            return accessKey.getText();
        }

        @Override
        public String getAWSSecretKey() {
            return secret.getText();
        }
    });
    try {
        client.listBuckets().forEach(bucket -> buckets.getItems().add(bucket.getName()));
        error.setVisible(false);
        error.setText("");
        buckets.requestFocus();
    } catch (SdkClientException e) {
        log.info("error accessing s3:{}", e.getMessage());
        error.setVisible(true);
        error.setText(e.getMessage());
    }
    buckets.setDisable(false);
    finish.setDisable(false);
    finish.setOnMouseClicked(addEvent -> {
        S3Provider provider = new S3Provider();
        S3Conf conf = new S3Conf();
        conf.setAccessKeyId(accessKey.getText());
        conf.setSecrectKey(secret.getText());
        conf.setRegion(Regions.fromName((String) regions.getSelectionModel().getSelectedItem()));
        conf.setBucket((String) buckets.getSelectionModel().getSelectedItem());
        provider.setConf(conf);
        providerCommander.register(provider).subscribe(result -> {
            log.info("provider s3:{} registered:{}", conf.getBucket(), result);
            clearInputs();
            navigator.popHome();
        }, err -> {
            log.error("error on adding s3 provider", err);
            Platform.runLater(() -> {
                error.setVisible(true);
                error.setText("Error: " + err.getMessage());
            });
        });
    });
}