From d49910cb955ae1b83276edf8170be6f1301ad775 Mon Sep 17 00:00:00 2001 From: P R <25353498+BigUstad@users.noreply.github.com> Date: Tue, 8 Sep 2020 18:32:49 -0700 Subject: [PATCH] Builder refactoring including WithBucket. (#454) --- Minio.Examples/Cases/BucketExists.cs | 3 +- .../Cases/EnableSuspendVersioning.cs | 4 +- Minio.Examples/Cases/GetVersioning.cs | 5 +- Minio.Examples/Cases/MakeBucket.cs | 5 +- Minio.Examples/Cases/MakeBucketWithLock.cs | 46 +++++++++ Minio.Examples/Cases/RemoveBucket.cs | 3 +- Minio.Examples/Program.cs | 12 ++- Minio.Functional.Tests/FunctionalTest.cs | 97 ++++++++++++++----- Minio.Tests/NegativeTest.cs | 27 +++++- Minio/ApiEndpoints/BucketOperations.cs | 18 ++-- Minio/DataModel/Args.cs | 6 +- Minio/DataModel/BucketArgs.cs | 12 ++- Minio/DataModel/BucketOperationsArgs.cs | 31 ++++-- Minio/DataModel/VersioningArgs.cs | 15 ++- Minio/MinioClient.cs | 30 ++++-- 15 files changed, 238 insertions(+), 76 deletions(-) create mode 100644 Minio.Examples/Cases/MakeBucketWithLock.cs diff --git a/Minio.Examples/Cases/BucketExists.cs b/Minio.Examples/Cases/BucketExists.cs index 3f5dca201..51a9473a2 100644 --- a/Minio.Examples/Cases/BucketExists.cs +++ b/Minio.Examples/Cases/BucketExists.cs @@ -28,7 +28,8 @@ public async static Task Run(MinioClient minio, try { Console.WriteLine("Running example for API: BucketExistsAsync"); - BucketExistsArgs args = new BucketExistsArgs(bucketName); + BucketExistsArgs args = new BucketExistsArgs() + .WithBucket(bucketName); bool found = await minio.BucketExistsAsync(args); Console.WriteLine((found ? "Found" : "Couldn't find ") + "bucket " + bucketName); Console.WriteLine(); diff --git a/Minio.Examples/Cases/EnableSuspendVersioning.cs b/Minio.Examples/Cases/EnableSuspendVersioning.cs index ce99ab534..79504e6e2 100644 --- a/Minio.Examples/Cases/EnableSuspendVersioning.cs +++ b/Minio.Examples/Cases/EnableSuspendVersioning.cs @@ -16,7 +16,6 @@ using System; using System.Threading.Tasks; -using Minio.DataModel; namespace Minio.Examples.Cases { @@ -30,7 +29,8 @@ public async static Task Run(MinioClient minio, { Console.WriteLine("Running example for API: EnableSuspendVersioning, "); // First Enable the Versioning. - var setArgs = new SetVersioningArgs(bucketName) + var setArgs = new SetVersioningArgs() + .WithBucket(bucketName) .WithVersioningEnabled(); await minio.SetVersioningAsync(setArgs); Console.WriteLine("Versioning Enable operation called for bucket " + bucketName); diff --git a/Minio.Examples/Cases/GetVersioning.cs b/Minio.Examples/Cases/GetVersioning.cs index 3fbf22620..9aa7940ef 100644 --- a/Minio.Examples/Cases/GetVersioning.cs +++ b/Minio.Examples/Cases/GetVersioning.cs @@ -26,13 +26,14 @@ class GetVersioning public async static Task Run(MinioClient minio, string bucketName = "my-bucket-name") { - var args = new GetVersioningArgs(bucketName); + var args = new GetVersioningArgs() + .WithBucket(bucketName); try { Console.WriteLine("Running example for API: GetVersioning, "); VersioningConfiguration config = await minio.GetVersioningAsync(args); - if ( config == null ) + if (config == null) { Console.WriteLine("Versioning Configuration not available for bucket " + bucketName); Console.WriteLine(); diff --git a/Minio.Examples/Cases/MakeBucket.cs b/Minio.Examples/Cases/MakeBucket.cs index 8ad0ba7e6..d5e970e14 100644 --- a/Minio.Examples/Cases/MakeBucket.cs +++ b/Minio.Examples/Cases/MakeBucket.cs @@ -1,5 +1,5 @@ /* - * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017 MinIO, Inc. + * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017-2020 MinIO, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,7 +29,8 @@ public async static Task Run(MinioClient minio, { Console.WriteLine("Running example for API: MakeBucketAsync"); await minio.MakeBucketAsync( - new MakeBucketArgs(bucketName) + new MakeBucketArgs() + .WithBucket(bucketName) .WithLocation(loc) ); Console.WriteLine($"Created bucket {bucketName}"); diff --git a/Minio.Examples/Cases/MakeBucketWithLock.cs b/Minio.Examples/Cases/MakeBucketWithLock.cs new file mode 100644 index 000000000..a20744778 --- /dev/null +++ b/Minio.Examples/Cases/MakeBucketWithLock.cs @@ -0,0 +1,46 @@ +/* + * MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2020 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; +using System.Threading.Tasks; + +namespace Minio.Examples.Cases +{ + public class MakeBucketWithLock + { + // Make a bucket + public async static Task Run(MinioClient minio, + string bucketName = "my-bucket-name", string loc = "us-east-1") + { + try + { + Console.WriteLine("Running example for API: MakeBucketAsync"); + await minio.MakeBucketAsync( + new MakeBucketArgs() + .WithBucket(bucketName) + .WithLocation(loc) + .WithObjectLock() + ); + Console.WriteLine($"Created bucket {bucketName} with lock."); + Console.WriteLine(); + } + catch (Exception e) + { + Console.WriteLine($"[Bucket] Exception: {e}"); + } + } + } +} \ No newline at end of file diff --git a/Minio.Examples/Cases/RemoveBucket.cs b/Minio.Examples/Cases/RemoveBucket.cs index f67595f33..918f920a7 100644 --- a/Minio.Examples/Cases/RemoveBucket.cs +++ b/Minio.Examples/Cases/RemoveBucket.cs @@ -28,7 +28,8 @@ public async static Task Run(MinioClient minio, try { await minio.RemoveBucketAsync( - new RemoveBucketArgs(bucketName) + new RemoveBucketArgs() + .WithBucket(bucketName) ); Console.WriteLine($"Removed the bucket {bucketName} successfully"); } diff --git a/Minio.Examples/Program.cs b/Minio.Examples/Program.cs index 7968da17d..29cedacf7 100644 --- a/Minio.Examples/Program.cs +++ b/Minio.Examples/Program.cs @@ -66,7 +66,7 @@ public static void Main(string[] args) { endPoint = Environment.GetEnvironmentVariable("SERVER_ENDPOINT"); int posColon = endPoint.LastIndexOf(':'); - if ( posColon != -1 ) + if (posColon != -1) { port = Int32.Parse(endPoint.Substring(posColon+1, (endPoint.Length - posColon - 1))); endPoint = endPoint.Substring(0, posColon); @@ -76,7 +76,7 @@ public static void Main(string[] args) if (Environment.GetEnvironmentVariable("ENABLE_HTTPS") != null) { enableHTTPS = Environment.GetEnvironmentVariable("ENABLE_HTTPS").Equals("1"); - if ( enableHTTPS && port == 80 ) + if (enableHTTPS && port == 80) { port = 443; } @@ -96,7 +96,7 @@ public static void Main(string[] args) // WithSSL() enables SSL support in MinIO client MinioClient minioClient = null; - if ( enableHTTPS ) + if (enableHTTPS) { minioClient = new MinioClient() .WithEndpoint(endPoint, port) @@ -120,6 +120,7 @@ public static void Main(string[] args) string objectName = GetRandomName(); string destBucketName = GetRandomName(); string destObjectName = GetRandomName(); + string lockBucketName = GetRandomName(); List objectsList = new List(); for (int i = 0; i < 10; i++) { @@ -141,6 +142,11 @@ public static void Main(string[] args) Cases.MakeBucket.Run(minioClient, destBucketName).Wait(); + // Bucket with Lock tests + Cases.MakeBucketWithLock.Run(minioClient, lockBucketName).Wait(); + Cases.BucketExists.Run(minioClient, lockBucketName).Wait(); + Cases.RemoveBucket.Run(minioClient, lockBucketName).Wait(); + //Versioning tests Cases.GetVersioning.Run(minioClient, bucketName).Wait(); Cases.EnableSuspendVersioning.Run(minioClient, bucketName).Wait(); diff --git a/Minio.Functional.Tests/FunctionalTest.cs b/Minio.Functional.Tests/FunctionalTest.cs index 5bcc9feb0..820c2fd67 100644 --- a/Minio.Functional.Tests/FunctionalTest.cs +++ b/Minio.Functional.Tests/FunctionalTest.cs @@ -169,9 +169,12 @@ internal async static Task BucketExists_Test(MinioClient minio) { DateTime startTime = DateTime.Now; string bucketName = GetRandomName(); - MakeBucketArgs mbArgs = new MakeBucketArgs(bucketName); - BucketExistsArgs beArgs = new BucketExistsArgs(bucketName); - RemoveBucketArgs rbArgs = new RemoveBucketArgs(bucketName); + MakeBucketArgs mbArgs = new MakeBucketArgs() + .WithBucket(bucketName); + BucketExistsArgs beArgs = new BucketExistsArgs() + .WithBucket(bucketName); + RemoveBucketArgs rbArgs = new RemoveBucketArgs() + .WithBucket(bucketName); var args = new Dictionary { { "bucketName", bucketName }, @@ -197,9 +200,12 @@ internal async static Task MakeBucket_Test1(MinioClient minio) { DateTime startTime = DateTime.Now; string bucketName = GetRandomName(length: 60); - MakeBucketArgs mbArgs = new MakeBucketArgs(bucketName); - BucketExistsArgs beArgs = new BucketExistsArgs(bucketName); - RemoveBucketArgs rbArgs = new RemoveBucketArgs(bucketName); + MakeBucketArgs mbArgs = new MakeBucketArgs() + .WithBucket(bucketName); + BucketExistsArgs beArgs = new BucketExistsArgs() + .WithBucket(bucketName); + RemoveBucketArgs rbArgs = new RemoveBucketArgs() + .WithBucket(bucketName); var args = new Dictionary { { "bucketName", bucketName }, @@ -224,9 +230,12 @@ internal async static Task MakeBucket_Test2(MinioClient minio) { DateTime startTime = DateTime.Now; string bucketName = GetRandomName(length: 10) + ".withperiod"; - MakeBucketArgs mbArgs = new MakeBucketArgs(bucketName); - BucketExistsArgs beArgs = new BucketExistsArgs(bucketName); - RemoveBucketArgs rbArgs = new RemoveBucketArgs(bucketName); + MakeBucketArgs mbArgs = new MakeBucketArgs() + .WithBucket(bucketName); + BucketExistsArgs beArgs = new BucketExistsArgs() + .WithBucket(bucketName); + RemoveBucketArgs rbArgs = new RemoveBucketArgs() + .WithBucket(bucketName); var args = new Dictionary { { "bucketName", bucketName }, @@ -254,10 +263,13 @@ internal async static Task MakeBucket_Test3(MinioClient minio, bool aws = false) return; DateTime startTime = DateTime.Now; string bucketName = GetRandomName(length: 60); - MakeBucketArgs mbArgs = new MakeBucketArgs(bucketName) + MakeBucketArgs mbArgs = new MakeBucketArgs() + .WithBucket(bucketName) .WithLocation("eu-central-1"); - BucketExistsArgs beArgs = new BucketExistsArgs(bucketName); - RemoveBucketArgs rbArgs = new RemoveBucketArgs(bucketName); + BucketExistsArgs beArgs = new BucketExistsArgs() + .WithBucket(bucketName); + RemoveBucketArgs rbArgs = new RemoveBucketArgs() + .WithBucket(bucketName); var args = new Dictionary { { "bucketName", bucketName }, @@ -290,10 +302,13 @@ internal async static Task MakeBucket_Test4(MinioClient minio, bool aws = false) return; DateTime startTime = DateTime.Now; string bucketName = GetRandomName(length: 20) + ".withperiod"; - MakeBucketArgs mbArgs = new MakeBucketArgs(bucketName) + MakeBucketArgs mbArgs = new MakeBucketArgs() + .WithBucket(bucketName) .WithLocation("us-west-2"); - BucketExistsArgs beArgs = new BucketExistsArgs(bucketName); - RemoveBucketArgs rbArgs = new RemoveBucketArgs(bucketName); + BucketExistsArgs beArgs = new BucketExistsArgs() + .WithBucket(bucketName); + RemoveBucketArgs rbArgs = new RemoveBucketArgs() + .WithBucket(bucketName); var args = new Dictionary { { "bucketName", bucketName }, @@ -329,7 +344,8 @@ internal async static Task MakeBucket_Test5(MinioClient minio) try { await Assert.ThrowsExceptionAsync(() => - minio.MakeBucketAsync(new MakeBucketArgs(bucketName))); + minio.MakeBucketAsync(new MakeBucketArgs() + .WithBucket(bucketName))); new MintLogger(nameof(MakeBucket_Test5), makeBucketSignature, "Tests whether MakeBucket throws InvalidBucketNameException when bucketName is null", TestStatus.PASS, (DateTime.Now - startTime), args: args).Log(); } catch (MinioException ex) @@ -338,15 +354,49 @@ await Assert.ThrowsExceptionAsync(() => } } + internal async static Task MakeBucketLock_Test1(MinioClient minio) + { + DateTime startTime = DateTime.Now; + string bucketName = GetRandomName(length: 60); + MakeBucketArgs mbArgs = new MakeBucketArgs() + .WithBucket(bucketName) + .WithObjectLock(); + BucketExistsArgs beArgs = new BucketExistsArgs() + .WithBucket(bucketName); + RemoveBucketArgs rbArgs = new RemoveBucketArgs() + .WithBucket(bucketName); + var args = new Dictionary + { + { "bucketName", bucketName }, + { "region", "us-east-1" }, + }; + + try + { + await minio.MakeBucketAsync(mbArgs); + bool found = await minio.BucketExistsAsync(beArgs); + Assert.IsTrue(found); + await minio.RemoveBucketAsync(rbArgs); + new MintLogger(nameof(MakeBucket_Test1), makeBucketSignature, "Tests whether MakeBucket with Lock passes", TestStatus.PASS, (DateTime.Now - startTime), args:args).Log(); + } + catch (MinioException ex) + { + new MintLogger(nameof(MakeBucket_Test1), makeBucketSignature, "Tests whether MakeBucket with Lock passes", TestStatus.FAIL, (DateTime.Now - startTime), "", ex.Message, ex.ToString(), args).Log(); + } + } + #endregion internal async static Task RemoveBucket_Test1(MinioClient minio) { DateTime startTime = DateTime.Now; string bucketName = GetRandomName(length: 20); - MakeBucketArgs mbArgs = new MakeBucketArgs(bucketName); - BucketExistsArgs beArgs = new BucketExistsArgs(bucketName); - RemoveBucketArgs rbArgs = new RemoveBucketArgs(bucketName); + MakeBucketArgs mbArgs = new MakeBucketArgs() + .WithBucket(bucketName); + BucketExistsArgs beArgs = new BucketExistsArgs() + .WithBucket(bucketName); + RemoveBucketArgs rbArgs = new RemoveBucketArgs() + .WithBucket(bucketName); var args = new Dictionary { { "bucketName", bucketName }, @@ -390,8 +440,10 @@ internal async static Task ListBuckets_Test(MinioClient minio) internal async static Task Setup_Test(MinioClient minio, string bucketName) { - MakeBucketArgs mbArgs = new MakeBucketArgs(bucketName); - BucketExistsArgs beArgs = new BucketExistsArgs(bucketName); + MakeBucketArgs mbArgs = new MakeBucketArgs() + .WithBucket(bucketName); + BucketExistsArgs beArgs = new BucketExistsArgs() + .WithBucket(bucketName); await minio.MakeBucketAsync(mbArgs); bool found = await minio.BucketExistsAsync(beArgs); Assert.IsTrue(found); @@ -399,7 +451,8 @@ internal async static Task Setup_Test(MinioClient minio, string bucketName) internal async static Task TearDown(MinioClient minio, string bucketName) { - RemoveBucketArgs rbArgs = new RemoveBucketArgs(bucketName); + RemoveBucketArgs rbArgs = new RemoveBucketArgs() + .WithBucket(bucketName); await minio.RemoveBucketAsync(rbArgs); } diff --git a/Minio.Tests/NegativeTest.cs b/Minio.Tests/NegativeTest.cs index c82ce984e..a4285ddfe 100644 --- a/Minio.Tests/NegativeTest.cs +++ b/Minio.Tests/NegativeTest.cs @@ -16,6 +16,7 @@ */ using System; +using System.Net; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -35,8 +36,10 @@ public async Task TestNoConnectionError() .WithEndpoint("localhost", 12121) .WithCredentials("minio", "minio") .Build(); + var args = new BucketExistsArgs() + .WithBucket("test"); - var ex = await Assert.ThrowsExceptionAsync(() => minio.BucketExistsAsync(new BucketExistsArgs("test"))); + var ex = await Assert.ThrowsExceptionAsync(() => minio.BucketExistsAsync(args)); Assert.IsNotNull(ex.ServerResponse); } @@ -48,7 +51,9 @@ public async Task TestInvalidBucketNameError() .WithEndpoint("play.min.io") .WithCredentials("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG") .Build(); - await Assert.ThrowsExceptionAsync(() => minio.BucketExistsAsync(new BucketExistsArgs(badName))); + var args = new BucketExistsArgs() + .WithBucket(badName); + await Assert.ThrowsExceptionAsync(() => minio.BucketExistsAsync(args)); } [TestMethod] @@ -63,10 +68,22 @@ public async Task TestInvalidObjectNameError() try { - await minio.MakeBucketAsync(new MakeBucketArgs(bucketName)); + const int tryCount = 5; + var args = new MakeBucketArgs() + .WithBucket(bucketName); + await minio.MakeBucketAsync(args); var ex = await Assert.ThrowsExceptionAsync( () => minio.StatObjectAsync(bucketName, badName)); + for(int i=0; + i < tryCount && + (ex.ServerResponse != null && + ex.ServerResponse.StatusCode.Equals(HttpStatusCode.ServiceUnavailable)); ++i) + { + ex = await Assert.ThrowsExceptionAsync( + () => minio.StatObjectAsync(bucketName, badName)); + + } Assert.AreEqual(ex.Response.Code, "InvalidObjectName"); ex = await Assert.ThrowsExceptionAsync( @@ -75,7 +92,9 @@ public async Task TestInvalidObjectNameError() } finally { - await minio.RemoveBucketAsync(new RemoveBucketArgs(bucketName)); + var args = new RemoveBucketArgs() + .WithBucket(bucketName); + await minio.RemoveBucketAsync(args); } } } diff --git a/Minio/ApiEndpoints/BucketOperations.cs b/Minio/ApiEndpoints/BucketOperations.cs index 653499e8f..4b0cce362 100644 --- a/Minio/ApiEndpoints/BucketOperations.cs +++ b/Minio/ApiEndpoints/BucketOperations.cs @@ -60,7 +60,7 @@ public partial class MinioClient : IBucketOperations } catch (Exception ex) { - if ( ex.GetType() == typeof(BucketNotFoundException) ) + if (ex.GetType() == typeof(BucketNotFoundException)) { return false; } @@ -103,7 +103,7 @@ public partial class MinioClient : IBucketOperations Uri requestUrl = RequestUtil.MakeTargetURL(this.BaseUrl, this.Secure, args.Location); SetTargetURL(requestUrl); // Set Authenticator, if necessary. - if ( string.IsNullOrEmpty(this.Region) && !s3utils.IsAmazonEndPoint(this.BaseUrl) && args.Location != "us-east-1" && this.restClient != null ) + if (string.IsNullOrEmpty(this.Region) && !s3utils.IsAmazonEndPoint(this.BaseUrl) && args.Location != "us-east-1" && this.restClient != null) { this.restClient.Authenticator = new V4Authenticator(this.Secure, this.AccessKey, this.SecretKey, region: args.Location, sessionToken: this.SessionToken); } @@ -121,7 +121,7 @@ public partial class MinioClient : IBucketOperations public async Task GetVersioningAsync(GetVersioningArgs args, CancellationToken cancellationToken = default(CancellationToken)) { args.Validate(); - RestRequest request = await this.CreateRequest(args, Method.GET).ConfigureAwait(false); + RestRequest request = await this.CreateRequest(args).ConfigureAwait(false); IRestResponse response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false); GetVersioningResponse versioningResponse = new GetVersioningResponse(response.StatusCode, response.Content); return versioningResponse.VersioningConfig; @@ -138,7 +138,7 @@ public partial class MinioClient : IBucketOperations public async Task SetVersioningAsync(SetVersioningArgs args, CancellationToken cancellationToken = default(CancellationToken)) { args.Validate(); - RestRequest request = await this.CreateRequest(args, Method.PUT).ConfigureAwait(false); + RestRequest request = await this.CreateRequest(args).ConfigureAwait(false); await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false); } @@ -175,7 +175,8 @@ public partial class MinioClient : IBucketOperations [Obsolete("Use MakeBucketAsync method with MakeBucketArgs object. Refer MakeBucket example code.")] public async Task MakeBucketAsync(string bucketName, string location = "us-east-1", CancellationToken cancellationToken = default(CancellationToken)) { - MakeBucketArgs args = new MakeBucketArgs(bucketName) + MakeBucketArgs args = new MakeBucketArgs() + .WithBucket(bucketName) .WithLocation(location); await this.MakeBucketAsync(args, cancellationToken); } @@ -189,7 +190,8 @@ public partial class MinioClient : IBucketOperations [Obsolete("Use BucketExistsAsync method with BucketExistsArgs object. Refer BucketExists example code.")] public async Task BucketExistsAsync(string bucketName, CancellationToken cancellationToken = default(CancellationToken)) { - BucketExistsArgs args = new BucketExistsArgs(bucketName); + BucketExistsArgs args = new BucketExistsArgs() + .WithBucket(bucketName); return await BucketExistsAsync(args, cancellationToken); } @@ -202,7 +204,9 @@ public partial class MinioClient : IBucketOperations [Obsolete("Use RemoveBucketAsync method with RemoveBucketArgs object. Refer RemoveBucket example code.")] public async Task RemoveBucketAsync(string bucketName, CancellationToken cancellationToken = default(CancellationToken)) { - await RemoveBucketAsync(new RemoveBucketArgs(bucketName), cancellationToken); + RemoveBucketArgs args = new RemoveBucketArgs() + .WithBucket(bucketName); + await RemoveBucketAsync(args, cancellationToken); } /// diff --git a/Minio/DataModel/Args.cs b/Minio/DataModel/Args.cs index 9572e8fd6..8e2298962 100644 --- a/Minio/DataModel/Args.cs +++ b/Minio/DataModel/Args.cs @@ -20,11 +20,15 @@ namespace Minio { - public class Args + public abstract class Args { internal Hashtable Headers { get; set; } internal Hashtable QueryParams { get; set; } + // RequestMethod will be the HTTP Method for request variable which is of type RestRequest. + // Will be one of the type - HEAD, GET, PUT, DELETE. etc. + internal Method RequestMethod { get; set; } + public static Hashtable CloneHashTable(Hashtable h) { Hashtable ret = new Hashtable(); diff --git a/Minio/DataModel/BucketArgs.cs b/Minio/DataModel/BucketArgs.cs index e6e35be5e..8d7c21d01 100644 --- a/Minio/DataModel/BucketArgs.cs +++ b/Minio/DataModel/BucketArgs.cs @@ -16,15 +16,19 @@ namespace Minio { - public class BucketArgs : Args + public abstract class BucketArgs : Args + where T : BucketArgs { internal string BucketName { get; set; } - protected BucketArgs(string bucketName) + protected BucketArgs() { - BucketName = bucketName; } - + public T WithBucket(string bucket) + { + this.BucketName = bucket; + return (T)this; + } public virtual void Validate() { utils.ValidateBucketName(this.BucketName); diff --git a/Minio/DataModel/BucketOperationsArgs.cs b/Minio/DataModel/BucketOperationsArgs.cs index f575cfebb..30f701e30 100644 --- a/Minio/DataModel/BucketOperationsArgs.cs +++ b/Minio/DataModel/BucketOperationsArgs.cs @@ -19,36 +19,43 @@ namespace Minio { - public class BucketExistsArgs: BucketArgs + public class BucketExistsArgs: BucketArgs { - public BucketExistsArgs(string bucketName) - :base(bucketName) + public BucketExistsArgs() { + this.RequestMethod = Method.HEAD; } } - public class RemoveBucketArgs : BucketArgs + public class RemoveBucketArgs : BucketArgs { - public RemoveBucketArgs(string bucketName) - : base (bucketName) + public RemoveBucketArgs() { + this.RequestMethod = Method.DELETE; } } - public class MakeBucketArgs : BucketArgs + public class MakeBucketArgs : BucketArgs { - public MakeBucketArgs(string bucketName) - : base(bucketName) + internal string Location { get; set; } + internal bool ObjectLock { get; set; } + public MakeBucketArgs() { + this.RequestMethod = Method.PUT; } - internal string Location { get; set; } public MakeBucketArgs WithLocation(string loc) { this.Location = loc; return this; } + public MakeBucketArgs WithObjectLock() + { + this.ObjectLock = true; + return this; + } + public override RestRequest BuildRequest(RestRequest request) { request.XmlSerializer = new RestSharp.Serializers.DotNetXmlSerializer(); @@ -60,6 +67,10 @@ public override RestRequest BuildRequest(RestRequest request) string body = utils.MarshalXML(config, "http://s3.amazonaws.com/doc/2006-03-01/"); request.AddParameter(new Parameter("text/xml", body, ParameterType.RequestBody)); } + if (this.ObjectLock) + { + request.AddOrUpdateParameter("X-Amz-Bucket-Object-Lock-Enabled", "true", ParameterType.HttpHeader); + } return request; } } diff --git a/Minio/DataModel/VersioningArgs.cs b/Minio/DataModel/VersioningArgs.cs index e9915e679..f0adffe0d 100644 --- a/Minio/DataModel/VersioningArgs.cs +++ b/Minio/DataModel/VersioningArgs.cs @@ -14,20 +14,17 @@ * limitations under the License. */ -using System.IO; -using System.Net; -using System.Xml.Serialization; using Minio.DataModel; using Minio.Exceptions; using RestSharp; namespace Minio { - public class GetVersioningArgs : BucketArgs + public class GetVersioningArgs : BucketArgs { - public GetVersioningArgs(string bucketName) - : base(bucketName) + public GetVersioningArgs() { + this.RequestMethod = Method.GET; } public override RestRequest BuildRequest(RestRequest req) @@ -37,7 +34,7 @@ public override RestRequest BuildRequest(RestRequest req) } } - public class SetVersioningArgs : BucketArgs + public class SetVersioningArgs : BucketArgs { internal VersioningStatus CurrentVersioningStatus; internal enum VersioningStatus : ushort @@ -46,9 +43,9 @@ internal enum VersioningStatus : ushort Enabled = 1, Suspended = 2, } - public SetVersioningArgs(string bucketName) - : base(bucketName) + public SetVersioningArgs() { + this.RequestMethod = Method.PUT; this.CurrentVersioningStatus = VersioningStatus.Off; } public override void Validate() diff --git a/Minio/MinioClient.cs b/Minio/MinioClient.cs index 5c8c25f75..f0eb87381 100644 --- a/Minio/MinioClient.cs +++ b/Minio/MinioClient.cs @@ -142,16 +142,30 @@ private async Task GetRegion(string bucketName) return (region == string.Empty) ? "us-east-1" : region; } + /// - /// Constructs a RestRequest. For AWS, this function has the side-effect of overriding the baseUrl - /// in the RestClient with region specific host path or virtual style path. + /// Null Check for Args object. + /// Expected to be called from CreateRequest + /// + /// The child object of Args class + private void ArgsCheck(Args args) + { + if (args is null) + { + throw new ArgumentNullException(nameof(args), "Args object cannot be null. It needs to be assigned to an instantiated child object of Args."); + } + } + + /// + /// Constructs a RestRequest using bucket/object names from Args. + /// Calls overloaded CreateRequest method. /// - /// The child object of BucketArgs class, args with populated values. From Input - /// The Method needed to call with this request object + /// The child object of BucketArgs class, args with populated values from Input /// A RestRequest - internal async Task CreateRequest(BucketArgs args, Method method) + internal async Task CreateRequest(BucketArgs args) where T : BucketArgs { - RestRequest request = await this.CreateRequest(method, args.BucketName); + this.ArgsCheck(args); + RestRequest request = await this.CreateRequest(args.RequestMethod, args.BucketName); return args.BuildRequest(request); } @@ -271,7 +285,7 @@ internal void InitClient() { throw new InvalidEndpointException("Endpoint cannot be empty."); } - else if ( this.Secure && this.restClient != null && this.restClient.BaseUrl == null ) + else if (this.Secure && this.restClient != null && this.restClient.BaseUrl == null) { Uri secureUrl = RequestUtil.MakeTargetURL(this.BaseUrl, this.Secure); this.SetTargetURL(secureUrl); @@ -417,7 +431,7 @@ public MinioClient WithRetryPolicy(RetryPolicyHandlingDelegate retryPolicyHandle /// internal void SetTargetURL(Uri uri) { - if ( this.restClient == null ) + if (this.restClient == null) { restClient = new RestSharp.RestClient(uri) {