Search in sources :

Example 6 with CreateBucketRequest

use of software.amazon.awssdk.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.

the class CrossRegionReplication method createBucket.

private static void createBucket(AmazonS3 s3Client, Regions region, String bucketName) {
    CreateBucketRequest request = new CreateBucketRequest(bucketName, region.getName());
    s3Client.createBucket(request);
    BucketVersioningConfiguration configuration = new BucketVersioningConfiguration().withStatus(BucketVersioningConfiguration.ENABLED);
    SetBucketVersioningConfigurationRequest enableVersioningRequest = new SetBucketVersioningConfigurationRequest(bucketName, configuration);
    s3Client.setBucketVersioningConfiguration(enableVersioningRequest);
}
Also used : CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) SetBucketVersioningConfigurationRequest(com.amazonaws.services.s3.model.SetBucketVersioningConfigurationRequest) BucketVersioningConfiguration(com.amazonaws.services.s3.model.BucketVersioningConfiguration)

Example 7 with CreateBucketRequest

use of software.amazon.awssdk.services.s3.model.CreateBucketRequest in project camel by apache.

the class S3Endpoint method doStart.

@Override
public void doStart() throws Exception {
    super.doStart();
    s3Client = configuration.getAmazonS3Client() != null ? configuration.getAmazonS3Client() : createS3Client();
    if (ObjectHelper.isNotEmpty(configuration.getAmazonS3Endpoint())) {
        s3Client.setEndpoint(configuration.getAmazonS3Endpoint());
    }
    String fileName = getConfiguration().getFileName();
    if (fileName != null) {
        LOG.trace("File name [{}] requested, so skipping bucket check...", fileName);
        return;
    }
    String bucketName = getConfiguration().getBucketName();
    LOG.trace("Querying whether bucket [{}] already exists...", bucketName);
    String prefix = getConfiguration().getPrefix();
    try {
        s3Client.listObjects(new ListObjectsRequest(bucketName, prefix, null, null, 0));
        LOG.trace("Bucket [{}] already exists", bucketName);
        return;
    } catch (AmazonServiceException ase) {
        /* 404 means the bucket doesn't exist */
        if (ase.getStatusCode() != 404) {
            throw ase;
        }
    }
    LOG.trace("Bucket [{}] doesn't exist yet", bucketName);
    // creates the new bucket because it doesn't exist yet
    CreateBucketRequest createBucketRequest = new CreateBucketRequest(getConfiguration().getBucketName());
    if (getConfiguration().getRegion() != null) {
        createBucketRequest.setRegion(getConfiguration().getRegion());
    }
    LOG.trace("Creating bucket [{}] in region [{}] with request [{}]...", configuration.getBucketName(), configuration.getRegion(), createBucketRequest);
    s3Client.createBucket(createBucketRequest);
    LOG.trace("Bucket created");
    if (configuration.getPolicy() != null) {
        LOG.trace("Updating bucket [{}] with policy [{}]", bucketName, configuration.getPolicy());
        s3Client.setBucketPolicy(bucketName, configuration.getPolicy());
        LOG.trace("Bucket policy updated");
    }
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) AmazonServiceException(com.amazonaws.AmazonServiceException)

Example 8 with CreateBucketRequest

use of software.amazon.awssdk.services.s3.model.CreateBucketRequest in project nifi by apache.

the class AbstractS3IT method oneTimeSetup.

@BeforeClass
public static void oneTimeSetup() {
    // Creates a client and bucket for this test
    final FileInputStream fis;
    try {
        fis = new FileInputStream(CREDENTIALS_FILE);
    } catch (FileNotFoundException e1) {
        fail("Could not open credentials file " + CREDENTIALS_FILE + ": " + e1.getLocalizedMessage());
        return;
    }
    try {
        final PropertiesCredentials credentials = new PropertiesCredentials(fis);
        client = new AmazonS3Client(credentials);
        if (client.doesBucketExist(BUCKET_NAME)) {
            fail("Bucket " + BUCKET_NAME + " exists. Choose a different bucket name to continue test");
        }
        CreateBucketRequest request = REGION.contains("east") ? // See https://github.com/boto/boto3/issues/125
        new CreateBucketRequest(BUCKET_NAME) : new CreateBucketRequest(BUCKET_NAME, REGION);
        client.createBucket(request);
    } catch (final AmazonS3Exception e) {
        fail("Can't create the key " + BUCKET_NAME + ": " + e.getLocalizedMessage());
    } catch (final IOException e) {
        fail("Caught IOException preparing tests: " + e.getLocalizedMessage());
    } finally {
        FileUtils.closeQuietly(fis);
    }
    if (!client.doesBucketExist(BUCKET_NAME)) {
        fail("Setup incomplete, tests will fail");
    }
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) FileNotFoundException(java.io.FileNotFoundException) PropertiesCredentials(com.amazonaws.auth.PropertiesCredentials) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) BeforeClass(org.junit.BeforeClass)

Example 9 with CreateBucketRequest

use of software.amazon.awssdk.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.

the class CreateBucket2 method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        if (!s3Client.doesBucketExistV2(bucketName)) {
            // Because the CreateBucketRequest object doesn't specify a region, the
            // bucket is created in the region specified in the client.
            s3Client.createBucket(new CreateBucketRequest(bucketName));
            // Verify that the bucket was created by retrieving it and checking its location.
            String bucketLocation = s3Client.getBucketLocation(new GetBucketLocationRequest(bucketName));
            System.out.println("Bucket location: " + bucketLocation);
        }
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it and returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions) GetBucketLocationRequest(com.amazonaws.services.s3.model.GetBucketLocationRequest)

Example 10 with CreateBucketRequest

use of software.amazon.awssdk.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.

the class App method main.

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.format("Usage: <the bucket name> <the AWS Region to use>\n" + "Example: my-test-bucket us-east-2\n");
        return;
    }
    String bucket_name = args[0];
    String region = args[1];
    s3 = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(region).build();
    // List current buckets.
    ListMyBuckets();
    // Create the bucket.
    if (s3.doesBucketExistV2(bucket_name)) {
        System.out.format("\nCannot create the bucket. \n" + "A bucket named '%s' already exists.", bucket_name);
        return;
    } else {
        try {
            System.out.format("\nCreating a new bucket named '%s'...\n\n", bucket_name);
            s3.createBucket(new CreateBucketRequest(bucket_name, region));
        } catch (AmazonS3Exception e) {
            System.err.println(e.getErrorMessage());
        }
    }
    // Confirm that the bucket was created.
    ListMyBuckets();
    // Delete the bucket.
    try {
        System.out.format("\nDeleting the bucket named '%s'...\n\n", bucket_name);
        s3.deleteBucket(bucket_name);
    } catch (AmazonS3Exception e) {
        System.err.println(e.getErrorMessage());
    }
    // Confirm that the bucket was deleted.
    ListMyBuckets();
}
Also used : CreateBucketRequest(com.amazonaws.services.s3.model.CreateBucketRequest) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Aggregations

CreateBucketRequest (com.amazonaws.services.s3.model.CreateBucketRequest)5 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)5 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)5 HeadBucketRequest (software.amazon.awssdk.services.s3.model.HeadBucketRequest)4 HeadBucketResponse (software.amazon.awssdk.services.s3.model.HeadBucketResponse)3 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)3 AmazonServiceException (com.amazonaws.AmazonServiceException)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 IOException (java.io.IOException)2 SdkClientException (com.amazonaws.SdkClientException)1 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)1 Regions (com.amazonaws.regions.Regions)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)1 BucketVersioningConfiguration (com.amazonaws.services.s3.model.BucketVersioningConfiguration)1 GetBucketLocationRequest (com.amazonaws.services.s3.model.GetBucketLocationRequest)1 ListObjectsRequest (com.amazonaws.services.s3.model.ListObjectsRequest)1 SetBucketVersioningConfigurationRequest (com.amazonaws.services.s3.model.SetBucketVersioningConfigurationRequest)1 FileInputStream (java.io.FileInputStream)1