开发者

How do I get a bucket by name in the .Net S3 SDK?

开发者 https://www.devze.com 2022-12-28 15:45 出处:网络
I\'m trying to find a method using the Amazon S3 SDK for .Net to get a bu开发者_运维知识库cket by its name. All I can find is ListAllBuckets(), but I really don\'t want to have to do that then try to

I'm trying to find a method using the Amazon S3 SDK for .Net to get a bu开发者_运维知识库cket by its name. All I can find is ListAllBuckets(), but I really don't want to have to do that then try to find it from the response.


Use the ListObjects() method

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

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

}


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号