Example usage for com.amazonaws.services.s3.model BucketVersioningConfiguration setStatus

List of usage examples for com.amazonaws.services.s3.model BucketVersioningConfiguration setStatus

Introduction

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

Prototype

public void setStatus(String status) 

Source Link

Document

Sets the desired status of versioning for this bucket versioning configuration object.

Usage

From source file:org.yardstickframework.spark.S3MasterUrlProvider.java

License:Apache License

/**
 * Method init amazon s3 API.// w w  w  . ja  v a  2  s.c om
 */
private void initAwsClient() {
    if (clientInit)
        return;

    String awsAccessKey = System.getenv("AWS_ACCESS_KEY");

    String awsSecretKey = System.getenv("AWS_SECRET_KEY");

    AWSCredentials cred;

    if (awsAccessKey == null || awsAccessKey.isEmpty() || awsSecretKey == null || awsSecretKey.isEmpty())
        throw new IllegalArgumentException("AWS credentials are not set.");
    else
        cred = new BasicAWSCredentials(awsAccessKey, awsSecretKey);

    if (bucketName == null || bucketName.isEmpty())
        throw new IllegalArgumentException("Bucket name is null or empty (provide bucket name and restart).");

    s3 = new AmazonS3Client(cred);

    if (!s3.doesBucketExist(bucketName)) {
        try {
            s3.createBucket(bucketName);

            BucketVersioningConfiguration verCfg = new BucketVersioningConfiguration();

            verCfg.setStatus(BucketVersioningConfiguration.ENABLED);

            s3.setBucketVersioningConfiguration(
                    new SetBucketVersioningConfigurationRequest(bucketName, verCfg));

            println("Created S3 bucket: " + bucketName);

            while (!s3.doesBucketExist(bucketName))
                try {
                    TimeUnit.MILLISECONDS.sleep(200L);
                } catch (Exception e) {
                    throw new RuntimeException("Thread has been interrupted.", e);
                }
        } catch (AmazonClientException e) {
            if (!s3.doesBucketExist(bucketName)) {
                s3 = null;

                throw new RuntimeException("Failed to create bucket: " + bucketName, e);
            }
        }
    }

    // Client init success.
    clientInit = true;
}