<commit_before>﻿// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0

// snippet-start:[S3.dotnet35.CreateBucket]

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Util;
using System;
using System.Threading.Tasks;

namespace CreateBucket
{
    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.

        // Specify your AWS Region (an example Region is shown).
        private static readonly RegionEndpoint BUCKET_REGION = RegionEndpoint.USWest2;
        private static IAmazonS3 _s3Client;

        // Specify the name of the new bucket.
        private const string NEW_BUCKET_NAME = "doc-example-bucket";

        static async Task Main()
        {
            _s3Client = new AmazonS3Client(BUCKET_REGION);
            Console.WriteLine($"\nCreating a new bucket, named: {NEW_BUCKET_NAME}.");

            await CreatingBucketAsync(_s3Client, NEW_BUCKET_NAME);

            var bucketLocation = FindBucketLocationAsync(_s3Client, NEW_BUCKET_NAME);

            if(!String.IsNullOrEmpty(bucketLocation.ToString()))
            {
                Console.WriteLine($"\n\nBucket {NEW_BUCKET_NAME} successfully created.");
            }
            else
            {
                Console.WriteLine("The bucket does not exist.");
            }
        }

        /// <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 bucketExists = AmazonS3Util.DoesS3BucketExistV2Async(client, bucketName).Result;
                if (!bucketExists)
                {
                    var putBucketRequest = new PutBucketRequest
                    {
                        BucketName = bucketName,
                        UseClientRegion = true
                    };

                    var putBucketResponse = await client.PutBucketAsync(putBucketRequest);
                }
                else
                {
                    Console.WriteLine($"A bucket with the name \"{bucketName}\" already exists.");
                    return;
                }

            } catch(AmazonS3Exception ex)
            {
                Console.WriteLine($"Error creating bucket: '{ex.Message}'");
            }
        }

        /// <summary>
        /// This method finds the location for a bucketName.
        /// </summary>
        /// <param name="client">The Amazon S3 client used to connect to
        /// Amazon S3.</param>
        /// <param name="bucketName">The name of the bucket for which
        /// we want to retrieve the location.</param>
        /// <returns>Returns a string representing the location of the
        /// bucket.</returns>
        static async Task<string> FindBucketLocationAsync(IAmazonS3 client, string bucketName)
        {
            string bucketLocation = null;
            try
            {
                var request = new GetBucketLocationRequest()
                {
                    BucketName = bucketName
                };
                GetBucketLocationResponse response = await client.GetBucketLocationAsync(request);
                bucketLocation = response.Location.ToString();
            } catch(AmazonS3Exception ex)
            {
                Console.WriteLine($"Error locating bucket: '{ex.Message}'");
            }
            return bucketLocation;
        }
    }
}
// snippet-end:[S3.dotnet35.CreateBucket]<commit_msg>Simplified code for CreateBucket.<commit_after>﻿// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0

// snippet-start:[S3.dotnet35.CreateBucket]

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.S3.Util;
using System;
using System.Threading.Tasks;

namespace CreateBucket
{
    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.

        // Specify your AWS Region (an example Region is shown).
        private static readonly RegionEndpoint BUCKET_REGION = RegionEndpoint.USEast1; // RegionEndpoint.USWest2;
        private static IAmazonS3 _s3Client;

        // Specify the name of the new bucket.
        private const string NEW_BUCKET_NAME = "igsmith-bucket2"; // "doc-example-bucket";

        static async Task Main()
        {

            _s3Client = new AmazonS3Client(BUCKET_REGION);
            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}'");
            }
        }
    }
}
// snippet-end:[S3.dotnet35.CreateBucket]
{'fraction_non_alphanumeric': 0.09140316205533597, 'fraction_numerical': 0.010540184453227932, 'mean_word_length': 1.7794050343249428, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6438480', 'n_tokens_mistral': 1580, 'n_tokens_neox': 1510, 'n_words': 451}