/// <summary> /// Determines whether an S3 bucket exists or not. /// This is done by: /// 1. Creating a PreSigned Url for the bucket (with an expiry date at the end of this decade) /// 2. Making a HEAD request to the Url /// </summary> /// <param name="bucketName">The name of the bucket to check.</param> /// <param name="s3Client">The Amazon S3 Client to use for S3 specific operations.</param> /// <returns></returns> public static bool DoesS3BucketExist(IAmazonS3 s3Client, string bucketName) { if (s3Client == null) { throw new ArgumentNullException("s3Client", "The s3Client cannot be null!"); } if (String.IsNullOrEmpty(bucketName)) { throw new ArgumentNullException("bucketName", "The bucketName cannot be null or the empty string!"); } GetPreSignedUrlRequest request = new GetPreSignedUrlRequest(); request.BucketName = bucketName; request.Expires = new DateTime(2019, 12, 31); request.Verb = HttpVerb.HEAD; request.Protocol = Protocol.HTTP; string url = s3Client.GetPreSignedURL(request); Uri uri = new Uri(url); HttpWebRequest httpRequest = WebRequest.Create(uri) as HttpWebRequest; httpRequest.Method = "HEAD"; AmazonS3Client concreteClient = s3Client as AmazonS3Client; if (concreteClient != null) { concreteClient.ConfigureProxy(httpRequest); } try { using (HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse) { // If all went well, the bucket was found! return(true); } } catch (WebException we) { using (HttpWebResponse errorResponse = we.Response as HttpWebResponse) { if (errorResponse != null) { HttpStatusCode code = errorResponse.StatusCode; return(code != HttpStatusCode.NotFound && code != HttpStatusCode.BadRequest); } // The Error Response is null which is indicative of either // a bad request or some other problem return(false); } } }