/// <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); }
/// <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); }
/// <summary>List all objects in a bucket</summary> /// <param name="client">The BucketClient</param> /// <param name="bucketName">The name of the bucket you want to list objects in.</param> /// <param name="getOwnerInfo">Set to true if you want to get object owner information as well.</param> /// <param name="token">A cancellation token</param> public static async IAsyncEnumerable <S3Object> ListAllObjectsAsync(this IObjectClient client, string bucketName, bool getOwnerInfo = false, Action <ListObjectsRequest> config = null, [EnumeratorCancellation] CancellationToken token = default) { Validator.RequireNotNull(client, nameof(client)); Validator.RequireNotNullOrEmpty(bucketName, nameof(bucketName)); string continuationToken = null; ListObjectsResponse response; do { if (token.IsCancellationRequested) { break; } string cToken = continuationToken; response = await client.ListObjectsAsync(bucketName, req => { req.ContinuationToken = cToken; if (getOwnerInfo) { req.FetchOwner = true; } config?.Invoke(req); }, token).ConfigureAwait(false); if (!response.IsSuccess) { throw new Exception("Request failed"); } foreach (S3Object responseObject in response.Objects) { yield return(responseObject); } continuationToken = response.NextContinuationToken; } while (response.IsTruncated); }
public Task <ListObjectsResponse> ListObjectsAsync(string bucketName, Action <ListObjectsRequest>?config = null, CancellationToken token = default) { return(_objectClient.ListObjectsAsync(bucketName, config, token)); }