use of com.amazonaws.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.
the class S3ObjectOperations method createBucket.
// Create a bucket by using a S3Waiter object
public static void createBucket(S3Client s3Client, String bucketName, Region region) {
S3Waiter s3Waiter = s3Client.waiter();
try {
CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).createBucketConfiguration(CreateBucketConfiguration.builder().locationConstraint(region.id()).build()).build();
s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
// Wait until the bucket is created and print out the response
WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println(bucketName + " is ready");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of com.amazonaws.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.
the class CreateBucket method createBucket.
// snippet-start:[s3.java2.create_bucket_waiters.main]
public static void createBucket(S3Client s3Client, String bucketName) {
try {
S3Waiter s3Waiter = s3Client.waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
// Wait until the bucket is created and print out the response.
WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println(bucketName + " is ready");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of com.amazonaws.services.s3.model.CreateBucketRequest in project aws-doc-sdk-examples by awsdocs.
the class MovieLensDatasetProvider method createBucket.
// Create a bucket by using a S3Waiter object
public static void createBucket(S3Client s3Client, String bucketName) {
try {
S3Waiter s3Waiter = s3Client.waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
s3Client.createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
// Wait until the bucket is created and print out the response
WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
waiterResponse.matched().response().ifPresent(System.out::println);
System.out.println(bucketName + " is ready");
} catch (S3Exception e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
}
use of com.amazonaws.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);
}
use of com.amazonaws.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");
}
}
Aggregations