You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.9 KiB

// 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);
}
/// <summary>
/// Uses Amazon SDK for .NET PutBucketAsync to create a new
/// Amazon S3 bucket.
/// </summary>
/// <param name="client">The client object used to connect to Amazon S3.</param>
/// <param name="bucketName">The name of the bucket to create.</param>
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}'");
}
}
}
}