public static Task <DeleteObjectsResponse> DeleteObjectsAsync(this IObjectClient client, string bucketName, IEnumerable <string> objectKeys, Action <DeleteObjectsRequest> config = null, CancellationToken token = default) { Validator.RequireNotNull(client, nameof(client)); Validator.RequireNotNull(bucketName, nameof(bucketName)); Validator.RequireNotNull(objectKeys, nameof(objectKeys)); return(client.DeleteObjectsAsync(bucketName, objectKeys.Select(x => new S3DeleteInfo(x)), config, token)); }
/// <summary>Delete all objects within the bucket</summary> public static async IAsyncEnumerable <S3DeleteError> DeleteAllObjectsAsync(this IObjectClient client, string bucketName, string?prefix = null, [EnumeratorCancellation] CancellationToken token = default) { Validator.RequireNotNull(client, nameof(client)); Validator.RequireNotNullOrEmpty(bucketName, nameof(bucketName)); ListObjectsResponse response; Task <ListObjectsResponse> responseTask = client.ListObjectsAsync(bucketName, req => req.Prefix = prefix, token); ObjectPool <S3DeleteInfo> pool = ObjectPool <S3DeleteInfo> .Shared; do { if (token.IsCancellationRequested) { yield break; } response = await responseTask; if (!response.IsSuccess) { throw new S3RequestException(response, $"Unable to list objects in bucket '{bucketName}"); } if (response.Objects.Count == 0) { yield break; } if (response.IsTruncated) { string localToken = response.NextContinuationToken; responseTask = client.ListObjectsAsync(bucketName, req => { req.Prefix = prefix; req.ContinuationToken = localToken; }, token); } IList <S3DeleteInfo> delete = response.Objects.Select(x => pool.Rent(info => info.Initialize(x.ObjectKey))).ToList(); DeleteObjectsResponse multiDelResponse = await client.DeleteObjectsAsync(bucketName, delete, req => req.Quiet = false, token).ConfigureAwait(false); pool.Return(delete); if (!multiDelResponse.IsSuccess) { throw new S3RequestException(response, $"Unable to delete objects in bucket '{bucketName}"); } foreach (S3DeleteError error in multiDelResponse.Errors) { yield return(error); } } while (response.IsTruncated); }
public static async IAsyncEnumerable <S3DeleteError> DeleteAllObjectVersionsAsync(this IObjectClient client, string bucketName, string?prefix = null, [EnumeratorCancellation] CancellationToken token = default) { Validator.RequireNotNull(client, nameof(client)); Validator.RequireNotNullOrEmpty(bucketName, nameof(bucketName)); ListObjectVersionsResponse response; Task <ListObjectVersionsResponse> responseTask = client.ListObjectVersionsAsync(bucketName, req => req.Prefix = prefix, token); do { if (token.IsCancellationRequested) { break; } response = await responseTask; if (!response.IsSuccess) { throw new S3RequestException(response, $"Unable to list objects in bucket '{bucketName}"); } if (response.Versions.Count + response.DeleteMarkers.Count == 0) { yield break; } if (response.IsTruncated) { string keyMarker = response.NextKeyMarker; responseTask = client.ListObjectVersionsAsync(bucketName, req => { req.Prefix = prefix; req.KeyMarker = keyMarker; }, token); } IEnumerable <S3DeleteInfo> delete = response.Versions.Select(x => new S3DeleteInfo(x.ObjectKey, x.VersionId)) .Concat(response.DeleteMarkers.Select(x => new S3DeleteInfo(x.ObjectKey, x.VersionId))); DeleteObjectsResponse multiDelResponse = await client.DeleteObjectsAsync(bucketName, delete, req => req.Quiet = false, token).ConfigureAwait(false); if (!multiDelResponse.IsSuccess) { throw new S3RequestException(response, $"Unable to delete objects in bucket '{bucketName}"); } foreach (S3DeleteError error in multiDelResponse.Errors) { yield return(error); } } while (response.IsTruncated); }
/// <summary>Delete all objects within the bucket</summary> public static async Task <DeleteAllObjectsStatus> DeleteAllObjectsAsync(this IObjectClient client, string bucketName, CancellationToken token = default) { string continuationToken = null; ListObjectsResponse response; do { if (token.IsCancellationRequested) { break; } string cToken = continuationToken; response = await client.ListObjectsAsync(bucketName, req => req.ContinuationToken = cToken, token).ConfigureAwait(false); if (!response.IsSuccess) { return(DeleteAllObjectsStatus.RequestFailed); } if (response.KeyCount == 0) { break; } DeleteObjectsResponse multiDelResponse = await client.DeleteObjectsAsync(bucketName, response.Objects.Select(x => x.ObjectKey), req => req.Quiet = false, token).ConfigureAwait(false); if (!multiDelResponse.IsSuccess) { return(DeleteAllObjectsStatus.RequestFailed); } if (multiDelResponse.Errors.Count > 0) { return(DeleteAllObjectsStatus.ObjectDeleteFailed); } continuationToken = response.NextContinuationToken; } while (response.IsTruncated); return(DeleteAllObjectsStatus.Ok); }
public Task <DeleteObjectsResponse> DeleteObjectsAsync(string bucketName, IEnumerable <S3DeleteInfo> objectKeys, Action <DeleteObjectsRequest>?config = null, CancellationToken token = default) { return(_objectClient.DeleteObjectsAsync(bucketName, objectKeys, config, token)); }