/// <summary> /// Lists all objects in a bucket /// </summary> /// <param name="client">The S3 client to use</param> /// <param name="bucketName">The bucket name</param> /// <param name="prefix">An optional prefix to filter with</param> /// <returns></returns> public static async Task <IEnumerable <ListObjectsV2Response> > ListAllObjectsAsync(this IAmazonS3 client, string bucketName, string prefix = "") { ParameterTests.NotNullOrEmpty(bucketName, "bucketName"); ParameterTests.NonNull <IAmazonS3>(client, "client"); ListObjectsV2Request request = new ListObjectsV2Request() { BucketName = bucketName, Prefix = prefix ?? String.Empty }; return(await ListAllObjectsAsync(client, request)); }
/// <summary> /// Tests if an S3 bucket exists by creating a pre-signed url and performing a /// HEAD operation on the url. This only tests for buckets that you have access to. /// </summary> /// <param name="client"></param> /// <param name="bucketName">The bucket to test for existence</param> /// <returns></returns> public static async Task <bool> BucketExistsAsync(this IAmazonS3 client, string bucketName) { ParameterTests.NotNullOrEmpty(bucketName, "bucketName"); GetPreSignedUrlRequest request = new GetPreSignedUrlRequest() { BucketName = bucketName, Expires = DateTime.Now.AddMinutes(5), Verb = HttpVerb.HEAD }; string response = client.GetPreSignedURL(request); HttpClient http = new HttpClient(); HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Head, response); HttpResponseMessage result = await http.SendAsync(message); return(result.IsSuccessStatusCode); }
/// <summary> /// Empties all contents of the specified bucket. /// </summary> /// <param name="client"></param> /// <param name="bucketName">The name of the bucket to empty</param> /// <param name="includeAllVersions">If true, in a bucket with versioning turned on, this will delete all versions of /// the objects in the bukcet, not just add a delete marker as the most recent version.</param> /// <returns></returns> public static async Task <IEnumerable <DeleteError> > EmptyBucketAsync(this IAmazonS3 client, string bucketName, bool includeAllVersions = false) { ParameterTests.NotNullOrEmpty(bucketName, "bucketName"); DeleteObjectsRequest delete = new DeleteObjectsRequest() { BucketName = bucketName }; List <DeleteError> errors = new List <DeleteError>(); if (includeAllVersions) { ListVersionsResponse response; ListVersionsRequest request = new ListVersionsRequest() { BucketName = bucketName }; do { response = await client.ListVersionsAsync(request); request.KeyMarker = response.NextKeyMarker; request.VersionIdMarker = response.VersionIdMarker; delete.Objects = response.Versions.Select(x => new KeyVersion() { Key = x.Key, VersionId = x.VersionId }).ToList(); DeleteObjectsResponse deleteResponse = await client.DeleteObjectsAsync(delete); errors.AddRange(deleteResponse.DeleteErrors); } while (response.IsTruncated); } else { ListObjectsV2Response response; ListObjectsV2Request request = new ListObjectsV2Request() { BucketName = bucketName }; do { response = await client.ListObjectsV2Async(request); request.ContinuationToken = response.NextContinuationToken; delete.Objects = response.S3Objects.Select(x => new KeyVersion() { Key = x.Key }).ToList(); DeleteObjectsResponse deleteResponse = await client.DeleteObjectsAsync(delete); errors.AddRange(deleteResponse.DeleteErrors); } while (response.IsTruncated); } return(errors); }