List Buckets
Get list of buckets
Components
- .NET
- PHP
- Python
- Javascript
- GO
// 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}"));
}
}
}
<?php
require('client.php');
$listResponse = $client->listBuckets();
$buckets = $listResponse['Buckets'];
foreach ($buckets as $bucket) {
echo $bucket['Name'] . "\t" . $bucket['CreationDate'] . "\n";
}
import boto3
import logging
from botocore.exceptions import ClientError
logging.basicConfig(level=logging.INFO)
try:
s3_resource = boto3.resource(
's3',
endpoint_url='endpoint_url',
aws_access_key_id='access_key',
aws_secret_access_key='secret_key'
)
except Exception as exc:
logging.error(exc)
else:
try:
for bucket in s3_resource.buckets.all():
logging.info(f'bucket_name: {bucket.name}')
except ClientError as exc:
logging.error(exc)
const { S3Client, ListBucketsCommand } = require('@aws-sdk/client-s3');
// Create an S3 client service object
const s3 = new S3Client({
region: 'default',
endpoint: 'endpoint_url',
credentials: {
accessKeyId: 'access_key',
secretAccessKey: 'secret_key',
},
});
const run = async () => {
try {
const data = await s3.send(new ListBucketsCommand({}));
console.log('Success', data.Buckets);
} catch (err) {
console.log('Error', err);
}
};
run();
package main
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() {
sess, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("<ACCESS_KEY>", "<SECRET_KEY>", ""),
})
svc := s3.New(sess, &aws.Config{
Region: aws.String("default"),
Endpoint: aws.String("<ENDPOINT_URL>"),
})
if err != nil {
exitErrorf("Unable to check for bucket %v", err)
} else {
listMyBuckets(svc)
}
}
// List all of your available buckets in this AWS Region.
func listMyBuckets(svc *s3.S3) {
result, err := svc.ListBuckets(nil)
if err != nil {
exitErrorf("Unable to list buckets, %v", err)
}
fmt.Println("My buckets now are:\n")
for _, b := range result.Buckets {
fmt.Printf(aws.StringValue(b.Name) + "\n")
}
fmt.Printf("\n")
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}
The following command can be used to execute the aforementioned code, presuming the code file is called s3_head_bucket.go:
go run s3_list_bucket.go