Articles → AWS SDK AND CSHARP → Get The List Of AWS Buckets Using C#
Get The List Of AWS Buckets Using C#
Steps
- Create a S3 bucket.
- Using Visual Studio, create a console application.
- Write code to get the list of buckets.
- Output.
Create A S3 Bucket
Click to Enlarge
Using Visual Studio, Create A Console Application
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
Click to Enlarge