Hey @Anjali,
Creating a bucket using Java AWS-SDK is very easy all you need to do is follow the following steps:-
1. Create your credentials ready to use.
2. Check for the bucket whether it exists or not?
3. Create the bucket.
Here is the code you can use :-
package com.ec2application.ec2application;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.AmazonS3Exception;
import com.amazonaws.services.s3.model.Bucket;
public class CreateS3Bucket
{
// Your accesskey and secretkey
@SuppressWarnings("deprecation")
public static void main(String[] args)
{
AWSCredentials credentials = new BasicAWSCredentials(
"id",
"secret key");
AmazonS3 s3client = AmazonS3ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.US_EAST_1)
.build();
Bucket b = null;
String bucket_name = "priyajdm1";
if (s3client.doesBucketExistV2(bucket_name)) {
System.out.format("Bucket %s already exists.\n", bucket_name);
// b = getBucket(bucket_name);
} else {
try {
b = s3client.createBucket(bucket_name);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
}
}
System.out.println(b);
}
}
Hope this helps.