/// <summary>
    ///     private helper method to remove objects in iterations of 1000 each from bucket
    /// </summary>
    /// <param name="args">
    ///     RemoveObjectsArgs Arguments Object encapsulates information like - bucket name, List of objects,
    ///     optional list of versions (for each object) to be deleted
    /// </param>
    /// <param name="fullErrorsList">
    ///     Full List of DeleteError objects. The error list from this call will be added to the full
    ///     list.
    /// </param>
    /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
    /// <returns></returns>
    /// <exception cref="AuthorizationException">When access or secret key provided is invalid</exception>
    /// <exception cref="InvalidBucketNameException">When bucket name is invalid</exception>
    /// <exception cref="InvalidObjectNameException">When object name is invalid</exception>
    /// <exception cref="BucketNotFoundException">When bucket is not found</exception>
    /// <exception cref="ObjectNotFoundException">When object is not found</exception>
    /// <exception cref="MalFormedXMLException">When configuration XML provided is invalid</exception>
    private async Task <List <DeleteError> > removeObjectsHelper(RemoveObjectsArgs args, List <DeleteError> fullErrorsList,
                                                                 CancellationToken cancellationToken)
    {
        var iterObjects = new List <string>(1000);
        var i           = 0;

        foreach (var objName in args.ObjectNames)
        {
            utils.ValidateObjectName(objName);
            iterObjects.Insert(i, objName);
            if (++i == 1000)
            {
                fullErrorsList = await callRemoveObjects(args, iterObjects, fullErrorsList, cancellationToken);

                iterObjects.Clear();
                i = 0;
            }
        }

        if (iterObjects.Count > 0)
        {
            fullErrorsList = await callRemoveObjects(args, iterObjects, fullErrorsList, cancellationToken);
        }
        return(fullErrorsList);
    }
Exemplo n.º 2
0
        public async Task <bool> RemoveObjectAsync(string bucketName, List <string> objectNames)
        {
            if (string.IsNullOrEmpty(bucketName))
            {
                throw new ArgumentNullException(nameof(bucketName));
            }
            if (objectNames == null || objectNames.Count == 0)
            {
                throw new ArgumentNullException(nameof(objectNames));
            }
            List <string> delObjects = new List <string>();

            foreach (var item in objectNames)
            {
                delObjects.Add(FormatObjectName(item));
            }
            RemoveObjectsArgs args = new RemoveObjectsArgs()
                                     .WithBucket(bucketName)
                                     .WithObjects(delObjects);
            IObservable <Minio.Exceptions.DeleteError> observable = await _client.RemoveObjectsAsync(args);

            List <string> removeFailed = new List <string>();

            bool        isFinish     = false;
            IDisposable subscription = observable.Subscribe(
                err =>
            {
                removeFailed.Add(err.Key);
            },
                ex =>
            {
                isFinish = true;
                throw ex;
            },
                () =>
            {
                isFinish = true;
            });

            while (!isFinish)
            {
                Thread.Sleep(0);
            }
            if (removeFailed.Count > 0)
            {
                if (removeFailed.Count == delObjects.Count)
                {
                    throw new Exception("Remove all object failed.");
                }
                else
                {
                    throw new Exception($"Remove objects '{string.Join(",", removeFailed)}' from {bucketName} failed.");
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// private helper method to call function to remove objects/version items in iterations of 1000 each from bucket
        /// </summary>
        /// <param name="args">RemoveObjectsArgs Arguments Object encapsulates information like - bucket name, List of objects, optional list of versions (for each object) to be deleted</param>
        /// <param name="objNames">List of Object names to be deleted</param>
        /// <param name="fullErrorsList">Full List of DeleteError objects. The error list from this call will be added to the full list.</param>
        /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
        /// <returns></returns>
        private async Task <List <DeleteError> > callRemoveObjects(RemoveObjectsArgs args, List <string> objNames, List <DeleteError> fullErrorsList, CancellationToken cancellationToken)
        {
            RemoveObjectsArgs iterArgs = new RemoveObjectsArgs()
                                         .WithBucket(args.BucketName)
                                         .WithObjects(objNames);
            var errorsList = await removeObjectsAsync(iterArgs, cancellationToken).ConfigureAwait(false);

            fullErrorsList.AddRange(errorsList);
            return(fullErrorsList);
        }
Exemplo n.º 4
0
        /// <summary>
        /// private helper method to remove list of objects from bucket
        /// </summary>
        /// <param name="args">RemoveObjectsArgs Arguments Object encapsulates information like - bucket name, List of objects, optional list of versions (for each object) to be deleted</param>
        /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
        /// <returns></returns>
        /// <exception cref="AuthorizationException">When access or secret key provided is invalid</exception>
        /// <exception cref="InvalidBucketNameException">When bucket name is invalid</exception>
        /// <exception cref="InvalidObjectNameException">When object name is invalid</exception>
        /// <exception cref="BucketNotFoundException">When bucket is not found</exception>
        /// <exception cref="ObjectNotFoundException">When object is not found</exception>
        /// <exception cref="MalFormedXMLException">When configuration XML provided is invalid</exception>
        private async Task <List <DeleteError> > removeObjectsAsync(RemoveObjectsArgs args, CancellationToken cancellationToken)
        {
            var request = await this.CreateRequest(args).ConfigureAwait(false);

            var response = await this.ExecuteAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);

            RemoveObjectsResponse removeObjectsResponse = new RemoveObjectsResponse(response.StatusCode, response.Content);

            return(removeObjectsResponse.DeletedObjectsResult.errorList);
        }
    /// <summary>
    ///     private helper method to remove list of objects from bucket
    /// </summary>
    /// <param name="args">
    ///     RemoveObjectsArgs Arguments Object encapsulates information like - bucket name, List of objects,
    ///     optional list of versions (for each object) to be deleted
    /// </param>
    /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
    /// <returns></returns>
    /// <exception cref="AuthorizationException">When access or secret key provided is invalid</exception>
    /// <exception cref="InvalidBucketNameException">When bucket name is invalid</exception>
    /// <exception cref="InvalidObjectNameException">When object name is invalid</exception>
    /// <exception cref="BucketNotFoundException">When bucket is not found</exception>
    /// <exception cref="ObjectNotFoundException">When object is not found</exception>
    /// <exception cref="MalFormedXMLException">When configuration XML provided is invalid</exception>
    private async Task <List <DeleteError> > removeObjectsAsync(RemoveObjectsArgs args,
                                                                CancellationToken cancellationToken)
    {
        var requestMessageBuilder = await CreateRequest(args).ConfigureAwait(false);

        using var response = await ExecuteTaskAsync(NoErrorHandlers, requestMessageBuilder, cancellationToken)
                             .ConfigureAwait(false);

        var removeObjectsResponse = new RemoveObjectsResponse(response.StatusCode, response.Content);

        return(removeObjectsResponse.DeletedObjectsResult.errorList);
    }
Exemplo n.º 6
0
        // Remove a list of objects from a bucket
        public static async Task Run(MinioClient minio,
                                     string bucketName         = "my-bucket-name",
                                     List <string> objectsList = null,
                                     List <Tuple <string, string> > objectsVersionsList = null)
        {
            try
            {
                Console.WriteLine("Running example for API: RemoveObjectsAsync");
                if (objectsList != null)
                {
                    RemoveObjectsArgs objArgs = new RemoveObjectsArgs()
                                                .WithBucket(bucketName)
                                                .WithObjects(objectsList);
                    IObservable <DeleteError> objectsOservable = await minio.RemoveObjectsAsync(objArgs).ConfigureAwait(false);

                    IDisposable objectsSubscription = objectsOservable.Subscribe(
                        objDeleteError => Console.WriteLine($"Object: {objDeleteError.Key}"),
                        ex => Console.WriteLine($"OnError: {ex}"),
                        () =>
                    {
                        Console.WriteLine($"Removed objects in list from {bucketName}\n");
                    });
                    return;
                }
                RemoveObjectsArgs objVersionsArgs = new RemoveObjectsArgs()
                                                    .WithBucket(bucketName)
                                                    .WithObjectsVersions(objectsVersionsList);
                IObservable <DeleteError> observable = await minio.RemoveObjectsAsync(objVersionsArgs).ConfigureAwait(false);

                IDisposable subscription = observable.Subscribe(
                    objVerDeleteError => Console.WriteLine($"Object: {objVerDeleteError.Key} " +
                                                           $"Object Version: {objVerDeleteError.VersionId}"),
                    ex => Console.WriteLine($"OnError: {ex}"),
                    () =>
                {
                    Console.WriteLine($"Removed objects versions from {bucketName}\n");
                });
            }
            catch (Exception e)
            {
                Console.WriteLine($"[Bucket-Object]  Exception: {e}");
            }
        }
    /// <summary>
    ///     private helper method to remove objects/version items in iterations of 1000 each from bucket
    /// </summary>
    /// <param name="args">
    ///     RemoveObjectsArgs Arguments Object encapsulates information like - bucket name, List of objects,
    ///     optional list of versions (for each object) to be deleted
    /// </param>
    /// <param name="fullErrorsList">
    ///     Full List of DeleteError objects. The error list from this call will be added to the full
    ///     list.
    /// </param>
    /// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
    /// <returns></returns>
    /// <exception cref="AuthorizationException">When access or secret key provided is invalid</exception>
    /// <exception cref="InvalidBucketNameException">When bucket name is invalid</exception>
    /// <exception cref="InvalidObjectNameException">When object name is invalid</exception>
    /// <exception cref="BucketNotFoundException">When bucket is not found</exception>
    /// <exception cref="ObjectNotFoundException">When object is not found</exception>
    /// <exception cref="MalFormedXMLException">When configuration XML provided is invalid</exception>
    private async Task <List <DeleteError> > removeObjectVersionsHelper(RemoveObjectsArgs args,
                                                                        List <DeleteError> fullErrorsList, CancellationToken cancellationToken)
    {
        if (args.ObjectNamesVersions.Count <= 1000)
        {
            fullErrorsList.AddRange(await callRemoveObjectVersions(args, args.ObjectNamesVersions, fullErrorsList,
                                                                   cancellationToken));
            return(fullErrorsList);
        }

        var curItemList         = new List <Tuple <string, string> >(args.ObjectNamesVersions.GetRange(0, 1000));
        var delVersionNextIndex = curItemList.Count;
        var deletedCount        = 0;

        while (delVersionNextIndex <= args.ObjectNamesVersions.Count)
        {
            var errorList = await callRemoveObjectVersions(args, curItemList, fullErrorsList, cancellationToken)
                            .ConfigureAwait(false);

            if (delVersionNextIndex == args.ObjectNamesVersions.Count)
            {
                break;
            }
            deletedCount += curItemList.Count;
            fullErrorsList.AddRange(errorList);
            curItemList.Clear();
            if (args.ObjectNamesVersions.Count - delVersionNextIndex <= 1000)
            {
                curItemList.AddRange(args.ObjectNamesVersions.GetRange(delVersionNextIndex,
                                                                       args.ObjectNamesVersions.Count - delVersionNextIndex));
                delVersionNextIndex = args.ObjectNamesVersions.Count;
            }
            else
            {
                curItemList.AddRange(args.ObjectNamesVersions.GetRange(delVersionNextIndex, 1000));
                delVersionNextIndex += 1000;
            }
        }

        return(fullErrorsList);
    }