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;
}
}
精彩评论