/* * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2020, 2021 MinIO, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System.Globalization; using System.Text; using System.Xml.Linq; using Minio.Helper; namespace Minio.DataModel.Args; public class RemoveObjectsArgs : ObjectArgs { public RemoveObjectsArgs() { ObjectName = null; ObjectNames = new List(); ObjectNamesVersions = new List>(); RequestMethod = HttpMethod.Post; } internal IList ObjectNames { get; private set; } // Each element in the list is a Tuple. Each Tuple has an Object name & the version ID. internal List> ObjectNamesVersions { get; } public RemoveObjectsArgs WithObjectAndVersions(string objectName, IList versions) { if (versions is null) throw new ArgumentNullException(nameof(versions)); foreach (var vid in versions) ObjectNamesVersions.Add(new Tuple(objectName, vid)); return this; } // Tuple>. Tuple object name -> List of Version IDs. public RemoveObjectsArgs WithObjectsVersions(IList>> objectsVersionsList) { if (objectsVersionsList is null) throw new ArgumentNullException(nameof(objectsVersionsList)); foreach (var objVersions in objectsVersionsList) foreach (var vid in objVersions.Item2) ObjectNamesVersions.Add(new Tuple(objVersions.Item1, vid)); return this; } public RemoveObjectsArgs WithObjectsVersions(IList> objectVersions) { ObjectNamesVersions.AddRange(objectVersions); return this; } public RemoveObjectsArgs WithObjects(IList names) { ObjectNames = names; return this; } internal override void Validate() { // Skip object name validation. Utils.ValidateBucketName(BucketName); if (!string.IsNullOrEmpty(ObjectName)) throw new InvalidOperationException(nameof(ObjectName) + " is set. Please use " + nameof(WithObjects) + "or " + nameof(WithObjectsVersions) + " method to set objects to be deleted."); if ((ObjectNames is null && ObjectNamesVersions is null) || (ObjectNames.Count == 0 && ObjectNamesVersions.Count == 0)) throw new InvalidOperationException( "Please assign list of object names or object names and version IDs to remove using method(s) " + nameof(WithObjects) + " " + nameof(WithObjectsVersions)); } internal override HttpRequestMessageBuilder BuildRequest(HttpRequestMessageBuilder requestMessageBuilder) { var objects = new List(); requestMessageBuilder.AddQueryParameter("delete", ""); XElement deleteObjectsRequest; if (ObjectNamesVersions.Count > 0) { // Object(s) & multiple versions foreach (var objTuple in ObjectNamesVersions) objects.Add(new XElement("Object", new XElement("Key", objTuple.Item1), new XElement("VersionId", objTuple.Item2))); deleteObjectsRequest = new XElement("Delete", objects, new XElement("Quiet", true)); requestMessageBuilder.AddXmlBody(Convert.ToString(deleteObjectsRequest, CultureInfo.InvariantCulture)); } else { // Multiple Objects foreach (var obj in ObjectNames) objects.Add(new XElement("Object", new XElement("Key", obj))); deleteObjectsRequest = new XElement("Delete", objects, new XElement("Quiet", true)); requestMessageBuilder.AddXmlBody(Convert.ToString(deleteObjectsRequest, CultureInfo.InvariantCulture)); } requestMessageBuilder.AddOrUpdateHeaderParameter("Content-Md5", Utils.GetMD5SumStr( Encoding.UTF8.GetBytes(Convert.ToString(deleteObjectsRequest, CultureInfo.InvariantCulture)))); return requestMessageBuilder; } }