diff --git a/Minio.Examples/Cases/DeleteBucketPolicy.cs b/Minio.Examples/Cases/DeleteBucketPolicy.cs index 1751c222c..feceff7a3 100644 --- a/Minio.Examples/Cases/DeleteBucketPolicy.cs +++ b/Minio.Examples/Cases/DeleteBucketPolicy.cs @@ -16,6 +16,7 @@ using System; using System.Threading.Tasks; +using Minio.Exceptions; namespace Minio.Examples.Cases { @@ -27,11 +28,21 @@ public async static Task Run(MinioClient minio, { try { - Console.WriteLine("Running example for API: SetPolicyAsync"); + Console.WriteLine("Running example for API: DeletePolicyAsync"); var args = new RemovePolicyArgs() .WithBucket(bucketName); await minio.RemovePolicyAsync(args); - Console.WriteLine($"Policy previously set for the bucket {bucketName} successfully"); + Console.WriteLine($"Policy previously set for the bucket {bucketName} removed."); + try + { + var getArgs = new GetPolicyArgs() + .WithBucket(bucketName); + string policy = await minio.GetPolicyAsync(getArgs); + } + catch(UnexpectedMinioException e) + { + Console.WriteLine($"GetPolicy operation for {bucketName} result: {e.ServerMessage}"); + } Console.WriteLine(); } catch (Exception e) diff --git a/Minio.Functional.Tests/FunctionalTest.cs b/Minio.Functional.Tests/FunctionalTest.cs index 668ae8841..b9cdea8e7 100644 --- a/Minio.Functional.Tests/FunctionalTest.cs +++ b/Minio.Functional.Tests/FunctionalTest.cs @@ -2717,8 +2717,11 @@ await minio.PutObjectAsync(bucketName, .WithPolicy(policyJson); var getPolicyArgs = new GetPolicyArgs() .WithBucket(bucketName); + var rmPolicyArgs = new RemovePolicyArgs() + .WithBucket(bucketName); await minio.SetPolicyAsync(setPolicyArgs); string policy = await minio.GetPolicyAsync(getPolicyArgs); + await minio.RemovePolicyAsync(rmPolicyArgs); await minio.RemoveObjectAsync(bucketName, objectName); await TearDown(minio, bucketName); diff --git a/Minio/ApiEndpoints/BucketOperations.cs b/Minio/ApiEndpoints/BucketOperations.cs index 4b9db4353..3fb96a78c 100644 --- a/Minio/ApiEndpoints/BucketOperations.cs +++ b/Minio/ApiEndpoints/BucketOperations.cs @@ -142,7 +142,6 @@ public partial class MinioClient : IBucketOperations await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false); } - /// /// Returns current policy stored on the server for this bucket /// @@ -167,8 +166,7 @@ public partial class MinioClient : IBucketOperations public async Task SetPolicyAsync(SetPolicyArgs args, CancellationToken cancellationToken = default(CancellationToken)) { RestRequest request = await this.CreateRequest(args).ConfigureAwait(false); - IRestResponse response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false); - SetPolicyResponse getPolicyResponse = new SetPolicyResponse(response.StatusCode, response.Content); + await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false); } @@ -187,6 +185,7 @@ public partial class MinioClient : IBucketOperations /// /// List all objects in a bucket + /// List all the buckets for the current Endpoint URL /// /// Optional cancellation token to cancel the operation /// Task with an iterator lazily populated with objects @@ -194,18 +193,11 @@ public partial class MinioClient : IBucketOperations { var request = await this.CreateRequest(Method.GET, resourcePath: "/").ConfigureAwait(false); var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false); - - ListAllMyBucketsResult bucketList = new ListAllMyBucketsResult(); - if (HttpStatusCode.OK.Equals(response.StatusCode)) - { - var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content); - using (var stream = new MemoryStream(contentBytes)) - bucketList = (ListAllMyBucketsResult)new XmlSerializer(typeof(ListAllMyBucketsResult)).Deserialize(stream); - return bucketList; - } - return bucketList; + ListBucketsResponse listBucketsResponse = new ListBucketsResponse(response.StatusCode, response.Content); + return listBucketsResponse.BucketsResult; } + /// /// Create a private bucket with the given name. /// @@ -396,22 +388,9 @@ public partial class MinioClient : IBucketOperations /// Task that returns the Bucket policy as a json string public async Task GetPolicyAsync(string bucketName, CancellationToken cancellationToken = default(CancellationToken)) { - IRestResponse response = null; - - var request = await this.CreateRequest(Method.GET, bucketName, - contentType: "application/json") - .ConfigureAwait(false); - request.AddQueryParameter("policy",""); - string policyString = null; - response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false); - var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content); - - using (var stream = new MemoryStream(contentBytes)) - using (var streamReader = new StreamReader(stream)) - { - policyString = await streamReader.ReadToEndAsync().ConfigureAwait(false); - } - return policyString; + GetPolicyArgs args = new GetPolicyArgs() + .WithBucket(bucketName); + return await this.GetPolicyAsync(args, cancellationToken); } @@ -424,12 +403,10 @@ public partial class MinioClient : IBucketOperations /// Task to set a policy public async Task SetPolicyAsync(string bucketName, string policyJson, CancellationToken cancellationToken = default(CancellationToken)) { - var request = await this.CreateRequest(Method.PUT, bucketName, - contentType: "application/json") - .ConfigureAwait(false); - request.AddQueryParameter("policy",""); - request.AddJsonBody(policyJson); - IRestResponse response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false); + SetPolicyArgs args = new SetPolicyArgs() + .WithBucket(bucketName) + .WithPolicy(policyJson); + await this.SetPolicyAsync(args, cancellationToken); } /// diff --git a/Minio/DataModel/BucketOperationsReponse.cs b/Minio/DataModel/BucketOperationsReponse.cs index eff009446..a16d964a2 100644 --- a/Minio/DataModel/BucketOperationsReponse.cs +++ b/Minio/DataModel/BucketOperationsReponse.cs @@ -25,18 +25,34 @@ internal class GetVersioningResponse : GenericResponse { internal VersioningConfiguration VersioningConfig { get; set; } internal GetVersioningResponse(HttpStatusCode statusCode, string responseContent) - : base(statusCode, responseContent) + : base(statusCode, responseContent) + { + if (string.IsNullOrEmpty(responseContent) || + !HttpStatusCode.OK.Equals(statusCode)) + { + return; + } + using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseContent))) + { + this.VersioningConfig = (VersioningConfiguration)new XmlSerializer(typeof(VersioningConfiguration)).Deserialize(stream); + } + } + } + + internal class ListBucketsResponse : GenericResponse + { + internal ListAllMyBucketsResult BucketsResult; + internal ListBucketsResponse(HttpStatusCode statusCode, string responseContent) + : base(statusCode, responseContent) { - if ( string.IsNullOrEmpty(responseContent) ) + if (string.IsNullOrEmpty(responseContent) || + !HttpStatusCode.OK.Equals(statusCode)) { return; } - if (HttpStatusCode.OK.Equals(statusCode)) + using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseContent))) { - using (var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseContent))) - { - this.VersioningConfig = (VersioningConfiguration)new XmlSerializer(typeof(VersioningConfiguration)).Deserialize(stream); - } + this.BucketsResult = (ListAllMyBucketsResult)new XmlSerializer(typeof(ListAllMyBucketsResult)).Deserialize(stream); } } } @@ -65,15 +81,4 @@ internal GetPolicyResponse(HttpStatusCode statusCode, string responseContent) Initialize(); } } - - internal class SetPolicyResponse : GenericResponse - { - internal string PolicyJsonString { get; private set; } - - internal SetPolicyResponse(HttpStatusCode statusCode, string responseContent) - : base(statusCode, responseContent) - { - } - - } } \ No newline at end of file