Listing S3 buckets

We will extend our console application to list all the S3 buckets. We will add another function named listS3Buckets to the program and invoke that function after the createS3Bucket() function.

Add the following function to your application:

 static void listS3Buckets()
{
try
{
IAmazonS3 s3Client = new AmazonS3Client();
ListBucketsResponse list_bucket_response = s3Client.ListBuckets();
foreach (S3Bucket bucket_object in list_bucket_response.Buckets)
{
Console.WriteLine("Bucket Name: {0}", bucket_object.BucketName);
}
}
catch (AmazonS3Exception s3Exception)
{
Console.WriteLine("Error!!");
Console.WriteLine(s3Exception.ErrorCode);
}
}

Invoke this function by calling it from the main() function:

public static void Main(string[] args)
{
createS3Bucket(); // invoke the method to create an S3 bucket
listS3Buckets(); // invoke the method to list all S3 buckets
Console.ReadKey();
}

Execute the program again. You will notice that the creation of the S3 buckets raises an exception this time. This is because there is already a bucket with the same name packt-pub present, which got created during the first execution of the program. The invocation to the listS3Buckets() function lists the buckets in your AWS account.

In this new function, we invoked the ListBuckets() method of the s3Client object and captured the response in the object of type ListBucketsResponse. We then iterated over the Buckets property of the response object and printed all the buckets on the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.29.201