How to get bucket by name in .Net S3 SDK?

I am trying to find a method using Amazon S3 SDK for .Net to get a bucket by its name. All I can find is ListAllBuckets (), but I really don't want to do this and then try to find it from the answer.

+2


a source to share


2 answers


This worked for me:



public static S3Bucket GetS3Bucket(string bucket)
{
    try
    {
        AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyID, secretAccessKeyID);
        return client.ListBuckets().Buckets.Where(b => b.BucketName == bucket).Single();
    }
    catch (AmazonS3Exception amazonS3Exception)
    {
        throw amazonS3Exception;
    }
}

      

0


a source


Use method ListObjects()



using (AmazonS3 client = Amazon.AWSClientFactory.CreateAmazonS3Client(AWSKey, AWSSecretKey)) {

    object req = new Model.ListObjectsRequest { BucketName = "MyBucket" };
    object resp = client.ListObjects(req);

}

      

+1


a source







All Articles