Articles → AWS SDK AND CSHARP → Get The List Of AWS Buckets Using C#

Get The List Of AWS Buckets Using C#






Steps




  1. Create a S3 bucket.
  2. Using Visual Studio, create a console application.
  3. Write code to get the list of buckets.
  4. Output.



Create A S3 Bucket




Picture showing the S3 bucket created in AWS console

Click to Enlarge


Using Visual Studio, Create A Console Application




Picture showing installing the AWSSDK.S3 component using the nuget packages

Click to Enlarge


Write Code To Get The List Of Buckets




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

namespace dotnetS3
{
    class Program
    {
        
        public static void Main(string[] args)
        {
            
            ListingObjectsAsync().Wait();
        }
        static async Task ListingObjectsAsync()
        {
            try
            {
                AmazonS3Client s3Client = new AmazonS3Client("<Access Id>", "<Secret key>");
                
                var response = await s3Client.ListBucketsAsync(System.Threading.CancellationToken.None);

                foreach (S3Bucket bucket in response.Buckets)
                {
                    Console.WriteLine("Bucket Name:" + bucket.BucketName);
                }
                Console.ReadLine();

            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                Console.WriteLine("S3 error occurred. Exception: " + amazonS3Exception.ToString());
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.ToString());
                Console.ReadKey();
            }
        }
    }
}



Output


Picture showing the list of s3 buckets using C# code

Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, February 3, 2022

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250