开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜