Skip to main content

List Buckets

Get list of buckets

Components

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

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

namespace ListBuckets
{
// This example uses the AWS SDK for .NET to list the Amazon Simple Storage
// Service (Amazon S3) buckets belonging to the default account. This code
// was written using AWS SDK for .NET v3.5 and .NET Core 5.0.
class ListBuckets
{
private static IAmazonS3 _s3Client;

static async Task Main(string[] args)
{
var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
_s3Client = new AmazonS3Client(awsCredentials, config);
var response = await GetBuckets(_s3Client);
DisplayBucketList(response.Buckets);
}

/// <summary>
/// Get a list of the buckets owned by the default user.
/// </summary>
/// <param name="client">An initialized Amazon S3 client object.</param>
/// <returns>The response from the ListingBuckets call that contains a
/// list of the buckets owned by the default user.</returns>
static public async Task<ListBucketsResponse> GetBuckets(IAmazonS3 client)
{
return await client.ListBucketsAsync();
}

/// <summary>
/// This method lists the name and creation date for the buckets in
/// the passed List of S3 buckets.
/// </summary>
/// <param name="bucketList">A List of S3 bucket objects.</param>
static public void DisplayBucketList(List<S3Bucket> bucketList)
{
bucketList
.ForEach(b => Console.WriteLine($"Bucket name: {b.BucketName}, created on: {b.CreationDate}"));
}
}
}