Create S3 bucket using Java AWS SDK

+1 vote
Can someone help me with the code to create S3 bucket using Java AWS SDK? Thank you
Jan 23, 2019 in AWS by Anjali
• 2,950 points
2,573 views

1 answer to this question.

0 votes

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.

answered Jan 23, 2019 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
2 answers

Create an S3 bucket using CloudFormation - AWS

Hey you can create an S3 bucket ...READ MORE

answered Aug 19, 2020 in AWS by Preeti

edited Aug 19, 2020 15,854 views
+1 vote
3 answers

How to get ARN for s3 Bucket using aws cli .

An ARN is a non-opaque, constructible identifier, ...READ MORE

answered Aug 16, 2018 in AWS by Priyaj
• 58,020 points
5,646 views
0 votes
1 answer
+1 vote
1 answer

AWS billing information using AWS java sdk

There are no APIs to get AWS ...READ MORE

answered Jul 26, 2018 in AWS by Priyaj
• 58,020 points
799 views
0 votes
1 answer

AWS S3 uploading hidden files by default

versioning is enabled in your bucket. docs.aws.amazon.com/AmazonS3/latest/user-guide/….... the ...READ MORE

answered Oct 4, 2018 in AWS by Priyaj
• 58,020 points
5,958 views
–1 vote
1 answer

How to decrypt the encrypted S3 file using aws-encryption-cli --decrypt

Use command : aws s3 presign s3://mybucket/abc_count.png you get ...READ MORE

answered Oct 22, 2018 in AWS by Priyaj
• 58,020 points
5,204 views
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,820 points
2,868 views
0 votes
2 answers
0 votes
1 answer

How to create a S3 bucket using AWS CLI?

You can use the following command:- C:\Users\priyj_kumar>aws s3api ...READ MORE

answered Feb 15, 2019 in AWS by Priyaj
• 58,020 points
6,128 views
0 votes
1 answer

How to create a EMR Cluster using Java AWS SDK?

The Java code for creating an AWS ...READ MORE

answered Feb 27, 2019 in AWS by Priyaj
• 58,020 points
1,593 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP