Listing S3 Buckets using ASP .NET Core Part-2


As we have already seen in the previous blog “Various operations on S3 Bucket using .NET core” all the prerequisites required to get started with Listing of S3 Buckets using ASP .NET Core.

In AWS S3 Bucket all the files and folders are treated as objects.

Let’s try to retrieve the Bucket names that are present in our S3 bucket, I am listing one bucket that is present in my S3 bucket called “rashmi-test” and gets its creation date.

public static async Task ListBuckets()
        {
            AmazonS3Client Client = new AmazonS3Client(PublicKey, SecretKey, Region);

            ListBucketsResponse listBucketsResponse = await Client.ListBucketsAsync();

            List<S3Bucket> S3Buckets = listBucketsResponse.Buckets;

            foreach (S3Bucket s3Bucket in S3Buckets)
            {
                Console.WriteLine(" Bucket Name is :" + s3Bucket.BucketName + " Creation Date is :" + s3Bucket.CreationDate);
            }

        }

We are initializing the AmazonS3Client and passing the PublicKey, SecretKey, and Region, the values for these are accessed from appsetting.json that we have seen in the previous blog. In the next step, we are creating a list of Bucket using the function ListBucketsAsync(). Then we are using a loop to access all the buckets. Below is the output attached which shows the Bucket name. If you have multiple buckets all of them will be there in the output window.

This is how we List Amazon S3 buckets using ASP .NET Core. In the next blog we will see how to List Items in a bucket using ASP .NET Core.

Leave A Comment

Your email address will not be published. Required fields are marked *