CreateBucket
with an AWS SDK or CLI
The following code examples show how to use CreateBucket
.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code examples:
/// <summary>
/// Shows how to create a new Amazon S3 bucket.
/// </summary>
/// <param name="client">An initialized Amazon S3 client object.</param>
/// <param name="bucketName">The name of the bucket to create.</param>
/// <returns>A boolean value representing the success or failure of
/// the bucket creation process.</returns>
public static async Task<bool> CreateBucketAsync(IAmazonS3 client, string bucketName)
{
try
{
var request = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true,
};
var response = await client.PutBucketAsync(request);
return response.HttpStatusCode == System.Net.HttpStatusCode.OK;
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
return false;
}
}
Create a bucket with object lock enabled.
/// <summary>
/// Create a new Amazon S3 bucket with object lock actions.
/// </summary>
/// <param name="bucketName">The name of the bucket to create.</param>
/// <param name="enableObjectLock">True to enable object lock on the bucket.</param>
/// <returns>True if successful.</returns>
public async Task<bool> CreateBucketWithObjectLock(string bucketName, bool enableObjectLock)
{
Console.WriteLine($"\tCreating bucket {bucketName} with object lock {enableObjectLock}.");
try
{
var request = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true,
ObjectLockEnabledForBucket = enableObjectLock,
};
var response = await _amazonS3.PutBucketAsync(request);
return response.HttpStatusCode == System.Net.HttpStatusCode.OK;
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
return false;
}
}
For API details, see CreateBucket in AWS SDK for .NET API Reference.
###############################################################################
# function iecho
#
# This function enables the script to display the specified text only if
# the global variable $VERBOSE is set to true.
###############################################################################
function iecho() {
if [[ $VERBOSE == true ]]; then
echo "$@"
fi
}
###############################################################################
# function errecho
#
# This function outputs everything sent to it to STDERR (standard error output).
###############################################################################
function errecho() {
printf "%s\n" "$*" 1>&2
}
###############################################################################
# function create-bucket
#
# This function creates the specified bucket in the specified AWS Region, unless
# it already exists.
#
# Parameters:
# -b bucket_name -- The name of the bucket to create.
# -r region_code -- The code for an AWS Region in which to
# create the bucket.
#
# Returns:
# The URL of the bucket that was created.
# And:
# 0 - If successful.
# 1 - If it fails.
###############################################################################
function create_bucket() {
local bucket_name region_code response
local option OPTARG # Required to use getopts command in a function.
# bashsupport disable=BP5008
function usage() {
echo "function create_bucket"
echo "Creates an Amazon S3 bucket. You must supply a bucket name:"
echo " -b bucket_name The name of the bucket. It must be globally unique."
echo " [-r region_code] The code for an AWS Region in which the bucket is created."
echo ""
}
# Retrieve the calling parameters.
while getopts "b:r:h" option; do
case "${option}" in
b) bucket_name="${OPTARG}" ;;
r) region_code="${OPTARG}" ;;
h)
usage
return 0
;;
\?)
echo "Invalid parameter"
usage
return 1
;;
esac
done
if [[ -z "$bucket_name" ]]; then
errecho "ERROR: You must provide a bucket name with the -b parameter."
usage
return 1
fi
local bucket_config_arg
# A location constraint for "us-east-1" returns an error.
if [[ -n "$region_code" ]] && [[ "$region_code" != "us-east-1" ]]; then
bucket_config_arg="--create-bucket-configuration LocationConstraint=$region_code"
fi
iecho "Parameters:\n"
iecho " Bucket name: $bucket_name"
iecho " Region code: $region_code"
iecho ""
# If the bucket already exists, we don't want to try to create it.
if (bucket_exists "$bucket_name"); then
errecho "ERROR: A bucket with that name already exists. Try again."
return 1
fi
# shellcheck disable=SC2086
response=$(aws s3api create-bucket \
--bucket "$bucket_name" \
$bucket_config_arg)
# shellcheck disable=SC2181
if [[ ${?} -ne 0 ]]; then
errecho "ERROR: AWS reports create-bucket operation failed.\n$response"
return 1
fi
}
For API details, see CreateBucket in AWS CLI Command Reference.
bool AwsDoc::S3::createBucket(const Aws::String &bucketName,
const Aws::S3::S3ClientConfiguration &clientConfig) {
Aws::S3::S3Client client(clientConfig);
Aws::S3::Model::CreateBucketRequest request;
request.SetBucket(bucketName);
if (clientConfig.region != "us-east-1") {
Aws::S3::Model::CreateBucketConfiguration createBucketConfig;
createBucketConfig.SetLocationConstraint(
Aws::S3::Model::BucketLocationConstraintMapper::GetBucketLocationConstraintForName(
clientConfig.region));
request.SetCreateBucketConfiguration(createBucketConfig);
}
Aws::S3::Model::CreateBucketOutcome outcome = client.CreateBucket(request);
if (!outcome.IsSuccess()) {
auto err = outcome.GetError();
std::cerr << "Error: createBucket: " <<
err.GetExceptionName() << ": " << err.GetMessage() << std::endl;
} else {
std::cout << "Created bucket " << bucketName <<
" in the specified AWS Region." << std::endl;
}
return outcome.IsSuccess();
}
For API details, see CreateBucket in AWS SDK for C++ API Reference.
Example 1: To create a bucket
The following create-bucket
example creates a bucket named amzn-s3-demo-bucket
:
aws s3api create-bucket \
--bucket amzn-s3-demo-bucket
\
--region us-east-1
Output:
{
"Location": "/amzn-s3-demo-bucket"
}
For more information, see Creating a bucket in the Amazon S3 User Guide.
Example 2: To create a bucket with owner enforced
The following create-bucket
example creates a bucket named amzn-s3-demo-bucket
that uses the bucket owner enforced setting for S3 Object Ownership.
aws s3api create-bucket \
--bucket amzn-s3-demo-bucket
\
--region us-east-1
\
--object-ownership BucketOwnerEnforced
Output:
{
"Location": "/amzn-s3-demo-bucket"
}
For more information, see Controlling ownership of objects and disabling ACLs in the Amazon S3 User Guide.
Example 3: To create a bucket outside of the ``us-east-1`` region
The following create-bucket
example creates a bucket named amzn-s3-demo-bucket
in the eu-west-1
region. Regions outside of us-east-1
require the appropriate LocationConstraint
to be specified in order to create the bucket in the desired region.
aws s3api create-bucket \
--bucket amzn-s3-demo-bucket
\
--region eu-west-1
\
--create-bucket-configuration LocationConstraint=eu-west-1
Output:
{
"Location": "http://amzn-s3-demo-bucket.s3.amazonaws.com/"
}
For more information, see Creating a bucket in the Amazon S3 User Guide.
For API details, see CreateBucket in AWS CLI Command Reference.
Create a bucket with default configuration.
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"log"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go"
)
// BucketBasics encapsulates the Amazon Simple Storage Service (Amazon S3) actions
// used in the examples.
// It contains S3Client, an Amazon S3 service client that is used to perform bucket
// and object actions.
type BucketBasics struct {
S3Client *s3.Client
}
// CreateBucket creates a bucket with the specified name in the specified Region.
func (basics BucketBasics) CreateBucket(ctx context.Context, name string, region string) error {
_, err := basics.S3Client.CreateBucket(ctx, &s3.CreateBucketInput{
Bucket: aws.String(name),
CreateBucketConfiguration: &types.CreateBucketConfiguration{
LocationConstraint: types.BucketLocationConstraint(region),
},
})
if err != nil {
var owned *types.BucketAlreadyOwnedByYou
var exists *types.BucketAlreadyExists
if errors.As(err, &owned) {
log.Printf("You already own bucket %s.\n", name)
err = owned
} else if errors.As(err, &exists) {
log.Printf("Bucket %s already exists.\n", name)
err = exists
}
} else {
err = s3.NewBucketExistsWaiter(basics.S3Client).Wait(
ctx, &s3.HeadBucketInput{Bucket: aws.String(name)}, time.Minute)
if err != nil {
log.Printf("Failed attempt to wait for bucket %s to exist.\n", name)
}
}
return err
}
Create a bucket with object locking and wait for it to exist.
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go"
)
// S3Actions wraps S3 service actions.
type S3Actions struct {
S3Client *s3.Client
S3Manager *manager.Uploader
}
// CreateBucketWithLock creates a new S3 bucket with optional object locking enabled
// and waits for the bucket to exist before returning.
func (actor S3Actions) CreateBucketWithLock(ctx context.Context, bucket string, region string, enableObjectLock bool) (string, error) {
input := &s3.CreateBucketInput{
Bucket: aws.String(bucket),
CreateBucketConfiguration: &types.CreateBucketConfiguration{
LocationConstraint: types.BucketLocationConstraint(region),
},
}
if enableObjectLock {
input.ObjectLockEnabledForBucket = aws.Bool(true)
}
_, err := actor.S3Client.CreateBucket(ctx, input)
if err != nil {
var owned *types.BucketAlreadyOwnedByYou
var exists *types.BucketAlreadyExists
if errors.As(err, &owned) {
log.Printf("You already own bucket %s.\n", bucket)
err = owned
} else if errors.As(err, &exists) {
log.Printf("Bucket %s already exists.\n", bucket)
err = exists
}
} else {
err = s3.NewBucketExistsWaiter(actor.S3Client).Wait(
ctx, &s3.HeadBucketInput{Bucket: aws.String(bucket)}, time.Minute)
if err != nil {
log.Printf("Failed attempt to wait for bucket %s to exist.\n", bucket)
}
}
return bucket, err
}
For API details, see CreateBucket in AWS SDK for Go API Reference.
Create a bucket.
/**
* Creates an S3 bucket asynchronously.
*
* @param bucketName the name of the S3 bucket to create
* @return a {@link CompletableFuture} that completes when the bucket is created and ready
* @throws RuntimeException if there is a failure while creating the bucket
*/
public CompletableFuture<Void> createBucketAsync(String bucketName) {
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
.bucket(bucketName)
.build();
CompletableFuture<CreateBucketResponse> response = getAsyncClient().createBucket(bucketRequest);
return response.thenCompose(resp -> {
S3AsyncWaiter s3Waiter = getAsyncClient().waiter();
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder()
.bucket(bucketName)
.build();
CompletableFuture<WaiterResponse<HeadBucketResponse>> waiterResponseFuture =
s3Waiter.waitUntilBucketExists(bucketRequestWait);
return waiterResponseFuture.thenAccept(waiterResponse -> {
waiterResponse.matched().response().ifPresent(headBucketResponse -> {
logger.info(bucketName + " is ready");
});
});
}).whenComplete((resp, ex) -> {
if (ex != null) {
throw new RuntimeException("Failed to create bucket", ex);
}
});
}
Create a bucket with object lock enabled.
// Create a new Amazon S3 bucket with object lock options.
public void createBucketWithLockOptions(boolean enableObjectLock, String bucketName) {
S3Waiter s3Waiter = getClient().waiter();
CreateBucketRequest bucketRequest = CreateBucketRequest.builder()
.bucket(bucketName)
.objectLockEnabledForBucket(enableObjectLock)
.build();
getClient().createBucket(bucketRequest);
HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder()
.bucket(bucketName)
.build();
// Wait until the bucket is created and print out the response.
s3Waiter.waitUntilBucketExists(bucketRequestWait);
System.out.println(bucketName + " is ready");
}
For API details, see CreateBucket in AWS SDK for Java 2.x API Reference.
Create the bucket.
import {
BucketAlreadyExists,
BucketAlreadyOwnedByYou,
CreateBucketCommand,
S3Client,
waitUntilBucketExists,
} from "@aws-sdk/client-s3";
/**
* Create an Amazon S3 bucket.
* @param {{ bucketName: string }} config
*/
export const main = async ({ bucketName }) => {
const client = new S3Client({});
try {
const { Location } = await client.send(
new CreateBucketCommand({
// The name of the bucket. Bucket names are unique and have several other constraints.
// See https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
Bucket: bucketName,
}),
);
await waitUntilBucketExists({ client }, { Bucket: bucketName });
console.log(`Bucket created with location ${Location}`);
} catch (caught) {
if (caught instanceof BucketAlreadyExists) {
console.error(
`The bucket "${bucketName}" already exists in another AWS account. Bucket names must be globally unique.`,
);
}
// WARNING: If you try to create a bucket in the North Virginia region,
// and you already own a bucket in that region with the same name, this
// error will not be thrown. Instead, the call will return successfully
// and the ACL on that bucket will be reset.
else if (caught instanceof BucketAlreadyOwnedByYou) {
console.error(
`The bucket "${bucketName}" already exists in this AWS account.`,
);
} else {
throw caught;
}
}
};
suspend fun createNewBucket(bucketName: String) {
val request =
CreateBucketRequest {
bucket = bucketName
}
S3Client.fromEnvironment { region = "us-east-1" }.use { s3 ->
s3.createBucket(request)
println("$bucketName is ready")
}
}
For API details, see CreateBucket in AWS SDK for Kotlin API reference.
Create a bucket.
$s3client = new Aws\S3\S3Client(['region' => 'us-west-2']);
try {
$this->s3client->createBucket([
'Bucket' => $this->bucketName,
'CreateBucketConfiguration' => ['LocationConstraint' => $region],
]);
echo "Created bucket named: $this->bucketName \n";
} catch (Exception $exception) {
echo "Failed to create bucket $this->bucketName with error: " . $exception->getMessage();
exit("Please fix error with bucket creation before continuing.");
}
For API details, see CreateBucket in AWS SDK for PHP API Reference.
Create a bucket with default settings.
class BucketWrapper:
"""Encapsulates S3 bucket actions."""
def __init__(self, bucket):
"""
:param bucket: A Boto3 Bucket resource. This is a high-level resource in Boto3
that wraps bucket actions in a class-like structure.
"""
self.bucket = bucket
self.name = bucket.name
def create(self, region_override=None):
"""
Create an Amazon S3 bucket in the default Region for the account or in the
specified Region.
:param region_override: The Region in which to create the bucket. If this is
not specified, the Region configured in your shared
credentials is used.
"""
if region_override is not None:
region = region_override
else:
region = self.bucket.meta.client.meta.region_name
try:
self.bucket.create(CreateBucketConfiguration={"LocationConstraint": region})
self.bucket.wait_until_exists()
logger.info("Created bucket '%s' in region=%s", self.bucket.name, region)
except ClientError as error:
logger.exception(
"Couldn't create bucket named '%s' in region=%s.",
self.bucket.name,
region,
)
raise error
Create a versioned bucket with a lifecycle configuration.
def create_versioned_bucket(bucket_name, prefix):
"""
Creates an Amazon S3 bucket, enables it for versioning, and configures a lifecycle
that expires noncurrent object versions after 7 days.
Adding a lifecycle configuration to a versioned bucket is a best practice.
It helps prevent objects in the bucket from accumulating a large number of
noncurrent versions, which can slow down request performance.
Usage is shown in the usage_demo_single_object function at the end of this module.
:param bucket_name: The name of the bucket to create.
:param prefix: Identifies which objects are automatically expired under the
configured lifecycle rules.
:return: The newly created bucket.
"""
try:
bucket = s3.create_bucket(
Bucket=bucket_name,
CreateBucketConfiguration={
"LocationConstraint": s3.meta.client.meta.region_name
},
)
logger.info("Created bucket %s.", bucket.name)
except ClientError as error:
if error.response["Error"]["Code"] == "BucketAlreadyOwnedByYou":
logger.warning("Bucket %s already exists! Using it.", bucket_name)
bucket = s3.Bucket(bucket_name)
else:
logger.exception("Couldn't create bucket %s.", bucket_name)
raise
try:
bucket.Versioning().enable()
logger.info("Enabled versioning on bucket %s.", bucket.name)
except ClientError:
logger.exception("Couldn't enable versioning on bucket %s.", bucket.name)
raise
try:
expiration = 7
bucket.LifecycleConfiguration().put(
LifecycleConfiguration={
"Rules": [
{
"Status": "Enabled",
"Prefix": prefix,
"NoncurrentVersionExpiration": {"NoncurrentDays": expiration},
}
]
}
)
logger.info(
"Configured lifecycle to expire noncurrent versions after %s days "
"on bucket %s.",
expiration,
bucket.name,
)
except ClientError as error:
logger.warning(
"Couldn't configure lifecycle on bucket %s because %s. "
"Continuing anyway.",
bucket.name,
error,
)
return bucket
For API details, see CreateBucket in AWS SDK for Python (Boto3) API Reference.
require 'aws-sdk-s3'
# Wraps Amazon S3 bucket actions.
class BucketCreateWrapper
attr_reader :bucket
# @param bucket [Aws::S3::Bucket] An Amazon S3 bucket initialized with a name. This is a client-side object until
# create is called.
def initialize(bucket)
@bucket = bucket
end
# Creates an Amazon S3 bucket in the specified AWS Region.
#
# @param region [String] The Region where the bucket is created.
# @return [Boolean] True when the bucket is created; otherwise, false.
def create?(region)
@bucket.create(create_bucket_configuration: { location_constraint: region })
true
rescue Aws::Errors::ServiceError => e
puts "Couldn't create bucket. Here's why: #{e.message}"
false
end
# Gets the Region where the bucket is located.
#
# @return [String] The location of the bucket.
def location
if @bucket.nil?
'None. You must create a bucket before you can get its location!'
else
@bucket.client.get_bucket_location(bucket: @bucket.name).location_constraint
end
rescue Aws::Errors::ServiceError => e
"Couldn't get the location of #{@bucket.name}. Here's why: #{e.message}"
end
end
# Example usage:
def run_demo
region = "us-west-2"
wrapper = BucketCreateWrapper.new(Aws::S3::Bucket.new("amzn-s3-demo-bucket-#{Random.uuid}"))
return unless wrapper.create?(region)
puts "Created bucket #{wrapper.bucket.name}."
puts "Your bucket's region is: #{wrapper.location}"
end
run_demo if $PROGRAM_NAME == __FILE__
For API details, see CreateBucket in AWS SDK for Ruby API Reference.
pub async fn create_bucket(
client: &aws_sdk_s3::Client,
bucket_name: &str,
region: &aws_config::Region,
) -> Result<Option<aws_sdk_s3::operation::create_bucket::CreateBucketOutput>, S3ExampleError> {
let constraint = aws_sdk_s3::types::BucketLocationConstraint::from(region.to_string().as_str());
let cfg = aws_sdk_s3::types::CreateBucketConfiguration::builder()
.location_constraint(constraint)
.build();
let create = client
.create_bucket()
.create_bucket_configuration(cfg)
.bucket(bucket_name)
.send()
.await;
// BucketAlreadyExists and BucketAlreadyOwnedByYou are not problems for this task.
create.map(Some).or_else(|err| {
if err
.as_service_error()
.map(|se| se.is_bucket_already_exists() || se.is_bucket_already_owned_by_you())
== Some(true)
{
Ok(None)
} else {
Err(S3ExampleError::from(err))
}
})
}
For API details, see CreateBucket in AWS SDK for Rust API reference.
TRY.
" determine our region from our session
DATA(lv_region) = CONV /aws1/s3_bucketlocationcnstrnt( lo_session->get_region( ) ).
DATA lo_constraint TYPE REF TO /aws1/cl_s3_createbucketconf.
" When in the us-east-1 region, you must not specify a constraint
" In all other regions, specify the region as the constraint
IF lv_region = 'us-east-1'.
CLEAR lo_constraint.
ELSE.
lo_constraint = NEW /aws1/cl_s3_createbucketconf( lv_region ).
ENDIF.
lo_s3->createbucket(
iv_bucket = iv_bucket_name
io_createbucketconfiguration = lo_constraint ).
MESSAGE 'S3 bucket created.' TYPE 'I'.
CATCH /aws1/cx_s3_bucketalrdyexists.
MESSAGE 'Bucket name already exists.' TYPE 'E'.
CATCH /aws1/cx_s3_bktalrdyownedbyyou.
MESSAGE 'Bucket already exists and is owned by you.' TYPE 'E'.
ENDTRY.
For API details, see CreateBucket in AWS SDK for SAP ABAP API reference.
import AWSS3
public func createBucket(name: String) async throws {
var input = CreateBucketInput(
bucket: name
)
// For regions other than "us-east-1", you must set the locationConstraint in the createBucketConfiguration.
// For more information, see LocationConstraint in the S3 API guide.
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateBucket.html#API_CreateBucket_RequestBody
if let region = configuration.region {
if region != "us-east-1" {
input.createBucketConfiguration = S3ClientTypes.CreateBucketConfiguration(locationConstraint: S3ClientTypes.BucketLocationConstraint(rawValue: region))
}
}
do {
_ = try await client.createBucket(input: input)
}
catch let error as BucketAlreadyOwnedByYou {
print("The bucket '\(name)' already exists and is owned by you. You may wish to ignore this exception.")
throw error
}
catch {
print("ERROR: ", dump(error, name: "Creating a bucket"))
throw error
}
}
For API details, see CreateBucket in AWS SDK for Swift API reference.
For a complete list of AWS SDK developer guides and code examples, see Developing with Amazon S3 using the AWS SDKs. This topic also includes information about getting started and details about previous SDK versions.
CopyObject
CreateMultiRegionAccessPoint
Did this page help you? - Yes
Thanks for letting us know we're doing a good job!
If you've got a moment, please tell us what we did right so we can do more of it.
Did this page help you? - No
Thanks for letting us know this page needs work. We're sorry we let you down.
If you've got a moment, please tell us how we can make the documentation better.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4