Java Code Examples for software.amazon.awssdk.services.s3.S3Client#createBucket()
The following examples show how to use
software.amazon.awssdk.services.s3.S3Client#createBucket() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: S3ObjectOperations.java From aws-doc-sdk-examples with Apache License 2.0 | 6 votes |
public static void createBucket( S3Client s3Client, String bucketName, Region region) { try{ s3Client.createBucket(CreateBucketRequest .builder() .bucket(bucketName) .createBucketConfiguration( CreateBucketConfiguration.builder() .locationConstraint(region.id()) .build()) .build()); System.out.println(bucketName); } catch (S3Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
Example 2
Source File: Synch.java From pegasus with Apache License 2.0 | 6 votes |
/** * Creates a S3 bucket with the given name * * @param name * @return boolean true in case a bucket was created, false if already exists, * @throws S3Exception in case unable to create bucket */ public boolean createS3Bucket(String name) { S3Client s3Client = S3Client.builder().region(mAWSRegion).build(); boolean created = true; try { CreateBucketResponse cbr = s3Client.createBucket( CreateBucketRequest.builder() .bucket(name) .createBucketConfiguration( CreateBucketConfiguration.builder() .locationConstraint(mAWSRegion.value()) .build()) .build()); } catch (S3Exception ex) { if (ex.getErrorCode().equals("BucketAlreadyOwnedByYou")) { created = false; } else { // rethrow the exception as is throw ex; } } return created; }
Example 3
Source File: S3Utils.java From quarkus with Apache License 2.0 | 5 votes |
public static boolean createBucket(final S3Client s3, final String bucket) { try { s3.createBucket(createBucketRequest(bucket)); return true; } catch (BucketAlreadyExistsException e) { LOG.info(("Bucket already exists.")); } return false; }
Example 4
Source File: S3TestUtils.java From aws-sdk-java-v2 with Apache License 2.0 | 5 votes |
private static String getBucketWithPrefix(S3Client s3, String bucketPrefix) { String testBucket = s3.listBuckets() .buckets() .stream() .map(Bucket::name) .filter(name -> name.startsWith(bucketPrefix)) .findAny() .orElse(null); if (testBucket == null) { String newTestBucket = bucketPrefix + UUID.randomUUID(); s3.createBucket(r -> r.bucket(newTestBucket)); Waiter.run(() -> s3.headBucket(r -> r.bucket(newTestBucket))) .ignoringException(NoSuchBucketException.class) .orFail(); testBucket = newTestBucket; } String finalTestBucket = testBucket; s3.putBucketLifecycleConfiguration(blc -> blc .bucket(finalTestBucket) .lifecycleConfiguration(lc -> lc .rules(r -> r.expiration(ex -> ex.days(1)) .status(ExpirationStatus.ENABLED) .filter(f -> f.prefix("")) .id("delete-old")))); return finalTestBucket; }
Example 5
Source File: S3BucketOps.java From aws-doc-sdk-examples with Apache License 2.0 | 5 votes |
public static void main(String[] args) { // snippet-start:[s3.java2.s3_bucket_ops.create_bucket] // snippet-start:[s3.java2.s3_bucket_ops.region] Region region = Region.US_WEST_2; S3Client s3 = S3Client.builder().region(region).build(); // snippet-end:[s3.java2.s3_bucket_ops.region] String bucket = "bucket" + System.currentTimeMillis(); System.out.println(bucket); // Create bucket CreateBucketRequest createBucketRequest = CreateBucketRequest .builder() .bucket(bucket) .createBucketConfiguration(CreateBucketConfiguration.builder() .locationConstraint(region.id()) .build()) .build(); s3.createBucket(createBucketRequest); // snippet-end:[s3.java2.s3_bucket_ops.create_bucket] // snippet-start:[s3.java2.s3_bucket_ops.list_bucket] // List buckets ListBucketsRequest listBucketsRequest = ListBucketsRequest.builder().build(); ListBucketsResponse listBucketsResponse = s3.listBuckets(listBucketsRequest); listBucketsResponse.buckets().stream().forEach(x -> System.out.println(x.name())); // snippet-end:[s3.java2.s3_bucket_ops.list_bucket] // Delete empty bucket // snippet-start:[s3.java2.s3_bucket_ops.delete_bucket] DeleteBucketRequest deleteBucketRequest = DeleteBucketRequest.builder().bucket(bucket).build(); s3.deleteBucket(deleteBucketRequest); // snippet-end:[s3.java2.s3_bucket_ops.delete_bucket] }
Example 6
Source File: LocalstackContainerTest.java From testcontainers-java with MIT License | 5 votes |
@Test public void s3TestUsingAwsSdkV2() { S3Client s3 = S3Client .builder() .endpointOverride(localstack.getEndpointOverride(LocalStackContainer.Service.S3)) .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create( localstack.getAccessKey(), localstack.getSecretKey() ))) .region(Region.of(localstack.getRegion())) .build(); final String bucketName = "foov2"; s3.createBucket(b -> b.bucket(bucketName)); assertTrue("New bucket was created", s3.listBuckets().buckets().stream().anyMatch(b -> b.name().equals(bucketName))); }