Skip to content

Commit

Permalink
Get/Set/Remove Bucket Policy with Args/Response object
Browse files Browse the repository at this point in the history
  • Loading branch information
BigUstad committed Sep 16, 2020
2 parents 4c30621 + dd1342c commit 0eef1bf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 55 deletions.
15 changes: 13 additions & 2 deletions Minio.Examples/Cases/DeleteBucketPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Threading.Tasks;
using Minio.Exceptions;

namespace Minio.Examples.Cases
{
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions Minio.Functional.Tests/FunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
47 changes: 12 additions & 35 deletions Minio/ApiEndpoints/BucketOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public partial class MinioClient : IBucketOperations
await this.ExecuteTaskAsync(this.NoErrorHandlers, request, cancellationToken).ConfigureAwait(false);
}


/// <summary>
/// Returns current policy stored on the server for this bucket
/// </summary>
Expand All @@ -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);
}


Expand All @@ -187,25 +185,19 @@ public partial class MinioClient : IBucketOperations

/// <summary>
/// List all objects in a bucket
/// List all the buckets for the current Endpoint URL
/// </summary>
/// <param name="cancellationToken">Optional cancellation token to cancel the operation</param>
/// <returns>Task with an iterator lazily populated with objects</returns>
public async Task<ListAllMyBucketsResult> ListBucketsAsync(CancellationToken cancellationToken = default(CancellationToken))
{
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;
}


/// <summary>
/// Create a private bucket with the given name.
/// </summary>
Expand Down Expand Up @@ -396,22 +388,9 @@ public partial class MinioClient : IBucketOperations
/// <returns>Task that returns the Bucket policy as a json string</returns>
public async Task<string> 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);
}


Expand All @@ -424,12 +403,10 @@ public partial class MinioClient : IBucketOperations
/// <returns>Task to set a policy</returns>
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);
}

/// <summary>
Expand Down
41 changes: 23 additions & 18 deletions Minio/DataModel/BucketOperationsReponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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)
{
}

}
}

0 comments on commit 0eef1bf

Please sign in to comment.