// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
namespace S3Demo.Storage
{
public class CreateBucket
{
// This example shows how to use Amazon Simple Storage Service (Amazon S3)
// to create a new Amazon S3 bucket. The examples uses AWS SDK for .NET 3.5 and
// .NET 5.0.
private static IAmazonS3 _s3Client;
// Specify the name of the new bucket.
//private const string NEW_BUCKET_NAME = "doc-example-bucket";
public static async Task CreatingBucket(string NEW_BUCKET_NAME, BasicAWSCredentials credentials, AmazonS3Config conf)
{
_s3Client = new AmazonS3Client(credentials, conf);
Console.WriteLine($"\nCreating a new bucket, named: {NEW_BUCKET_NAME}.");
await CreatingBucketAsync(_s3Client, NEW_BUCKET_NAME);
}
///
/// Uses Amazon SDK for .NET PutBucketAsync to create a new
/// Amazon S3 bucket.
///
/// The client object used to connect to Amazon S3.
/// The name of the bucket to create.
static async Task CreatingBucketAsync(IAmazonS3 client, string bucketName)
{
try
{
var putBucketRequest = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true
};
var putBucketResponse = await client.PutBucketAsync(putBucketRequest);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error creating bucket: '{ex.Message}'");
}
}
}
}