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 22 in AWS by Anjali
• 2,910 points
41 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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 by Priyaj
• 56,100 points

Related Questions In AWS

+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
• 56,100 points
675 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
• 56,100 points
72 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
• 56,100 points
82 views
0 votes
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
• 56,100 points
176 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,520 points
95 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 in AWS by Priyaj
• 56,100 points
73 views
0 votes
1 answer

How to create a EMR Cluster using Java AWS SDK?

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

answered Feb 27 in AWS by Priyaj
• 56,100 points
31 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.