Skip to content

Commit

Permalink
refactor client and exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
poornas authored and harshavardhana committed Feb 17, 2017
1 parent 311a54d commit 32174d9
Show file tree
Hide file tree
Showing 195 changed files with 8,672 additions and 1,041 deletions.
395 changes: 227 additions & 168 deletions API.md

Large diffs are not rendered by default.

49 changes: 33 additions & 16 deletions FileUploader/FileUpload.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
using System;
using Minio;
using Minio.Exceptions;
using Minio.DataModel;
/*
* Minio .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017 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;
using System.Net;
using Minio;

namespace FileUploader
{
/// <summary>
///
/// This example creates a new bucket if it does not already exist, and uploads a file
/// to the bucket.
/// </summary>
class FileUpload
{
Expand All @@ -31,7 +46,12 @@ static void Main(string[] args)
}
Console.ReadLine();
}
//Check if a bucket exists

/// <summary>
/// Task that uploads a file to a bucket
/// </summary>
/// <param name="minio"></param>
/// <returns></returns>
private async static Task Run(MinioClient minio)
{
// Make a new bucket called mymusic.
Expand All @@ -44,20 +64,17 @@ private async static Task Run(MinioClient minio)

try
{
bool success = await minio.Api.MakeBucketAsync(bucketName, location);
if (!success) {
bool found = await minio.Api.BucketExistsAsync(bucketName);
Console.Out.WriteLine("bucket-name was " + ((found == true) ? "found" : "not found"));
}
else {
await minio.Api.PutObjectAsync(bucketName, objectName, filePath, contentType);
Console.Out.WriteLine("Successfully uploaded " + objectName );
bool found = await minio.BucketExistsAsync(bucketName);
if (!found)
{
await minio.MakeBucketAsync(bucketName, location);
}

await minio.PutObjectAsync(bucketName, objectName, filePath, contentType);
Console.Out.WriteLine("Successfully uploaded " + objectName );
}
catch (Exception e)
{
Console.WriteLine("[Bucket] Exception: {0}", e);
Console.Out.WriteLine(e);
}
}

Expand Down
3 changes: 3 additions & 0 deletions FileUploader/FileUploader.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Minio.Api\MinioApi.csproj">
<Project>{cc30cade-342a-4ced-858d-b60200c075b0}</Project>
Expand Down
137 changes: 52 additions & 85 deletions Minio.Api/ApiEndpoints/BucketOperations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Minio .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2015 Minio, Inc.
* Minio .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Net;
Expand All @@ -24,28 +25,27 @@
using System.Xml.Linq;
using System.Xml.Serialization;
using Minio.Exceptions;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Reactive.Linq;
using Minio.Helper;

namespace Minio
{
public partial class ClientApiOperations : IBucketOperations
public partial class MinioClient : IBucketOperations
{

/// <summary>
/// List all objects in a bucket
/// </summary>
/// <param name="bucketName">Bucket to list objects from</param>
/// <returns>An iterator lazily populated with objects</returns>
public async Task<ListAllMyBucketsResult> ListBucketsAsync()
{
// Initialize a new client
PrepareClient();

var request = new RestRequest("/", Method.GET);
var response = await this.client.ExecuteTaskAsync(this.client.NoErrorHandlers, request);
if (response.StatusCode != HttpStatusCode.OK)
{
this.client.ParseError(response);
}
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request);

ListAllMyBucketsResult bucketList = new ListAllMyBucketsResult();
if (HttpStatusCode.OK.Equals(response.StatusCode))
{
Expand All @@ -61,61 +61,48 @@ public async Task<ListAllMyBucketsResult> ListBucketsAsync()
}

/// <summary>
/// Create a private bucket with a give name.
/// Create a private bucket with the given name.
/// </summary>
/// <param name="bucketName">Name of the new bucket</param>
public async Task<bool> MakeBucketAsync(string bucketName, string location = "us-east-1")
public async Task MakeBucketAsync(string bucketName, string location = "us-east-1")
{
var request = new RestRequest("/" + bucketName, Method.PUT);
// Initialize a new client
PrepareClient();

var request = new RestRequest("/" + bucketName, Method.PUT);
// ``us-east-1`` is not a valid location constraint according to amazon, so we skip it.
if (location != "us-east-1")
{
CreateBucketConfiguration config = new CreateBucketConfiguration(location);
request.AddBody(config);
}

var response = await this.client.ExecuteTaskAsync(this.client.NoErrorHandlers, request);

if (response.StatusCode == HttpStatusCode.OK)
{
return true;
}
this.client.ParseError(response);
return false;
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request);

}


/// <summary>
/// Returns true if the specified bucketName exists, otherwise returns false.
/// </summary>
/// <param name="bucketName">Bucket to test existence of</param>
/// <returns>true if exists and user has access</returns>
public async Task<bool> BucketExistsAsync(string bucketName)
{

var request = await client.CreateRequest(Method.HEAD,
bucketName,
region: BucketRegionCache.Instance.Region(bucketName));

var response = await this.client.ExecuteTaskAsync(this.client.NoErrorHandlers, request);

if (response.StatusCode != HttpStatusCode.OK)
try
{
try
{
this.client.ParseError(response);
}
catch (Exception ex)
var request = await this.CreateRequest(Method.HEAD,
bucketName,region:"us-east-1");
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request);
}
catch (Exception ex)
{
if (ex.GetType() == typeof(BucketNotFoundException))
{
if (ex.GetType() == typeof(BucketNotFoundException))
{
return false;
}
throw ex;
return false;
}
throw ex;
}

return true;
}

Expand All @@ -125,16 +112,10 @@ public async Task<bool> BucketExistsAsync(string bucketName)
/// <param name="bucketName">Name of bucket to remove</param>
public async Task RemoveBucketAsync(string bucketName)
{
var request =await client.CreateRequest(Method.DELETE, bucketName,
region: BucketRegionCache.Instance.Region(bucketName),
resourcePath:"/");
var request = await this.CreateRequest(Method.DELETE, bucketName, resourcePath: "/");

var response = await this.client.ExecuteTaskAsync(this.client.NoErrorHandlers, request);
var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request);

if (!response.StatusCode.Equals(HttpStatusCode.NoContent))
{
this.client.ParseError(response);
}
}

/// <summary>
Expand Down Expand Up @@ -172,9 +153,9 @@ public IObservable<Item> ListObjectsAsync(string bucketName, string prefix = nul
}
});
}

/// <summary>
///
/// Gets the list of objects in the bucket filtered by prefix
/// </summary>
/// <param name="bucketName">Bucket to list objects from</param>
/// <param name="prefix">Filters all objects not beginning with a given prefix</param>
Expand Down Expand Up @@ -204,20 +185,15 @@ private async Task<Tuple<ListBucketResult, List<Item>>> GetObjectListAsync(strin
{
path += "?" + query;
}

var request = await client.CreateRequest(Method.GET,
bucketName,
region: BucketRegionCache.Instance.Region(bucketName),
resourcePath:"?" + query);


var request = await this.CreateRequest(Method.GET,
bucketName,
resourcePath: "?" + query);

var response = await this.client.ExecuteTaskAsync(this.client.NoErrorHandlers, request);

if (response.StatusCode != HttpStatusCode.OK)
{
this.client.ParseError(response);
}

var response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request);

var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
var stream = new MemoryStream(contentBytes);
ListBucketResult listBucketResult = (ListBucketResult)(new XmlSerializer(typeof(ListBucketResult)).Deserialize(stream));
Expand Down Expand Up @@ -245,6 +221,7 @@ private async Task<Tuple<ListBucketResult, List<Item>>> GetObjectListAsync(strin

return new Tuple<ListBucketResult, List<Item>>(listBucketResult, items.ToList());
}

/// <summary>
/// Returns current policy stored on the server for this bucket
/// </summary>
Expand All @@ -255,17 +232,13 @@ private async Task<BucketPolicy> GetPolicyAsync(string bucketName)
BucketPolicy policy = null;
IRestResponse response = null;

var path =bucketName + "?policy";
var path = bucketName + "?policy";

var request = await this.CreateRequest(Method.GET, bucketName,
contentType: "application/json",
resourcePath: "?policy");
response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request);

var request = await client.CreateRequest(Method.GET, bucketName,
region: BucketRegionCache.Instance.Region(bucketName),
contentType:"application/json",
resourcePath:"?policy");
response = await this.client.ExecuteTaskAsync(this.client.NoErrorHandlers, request);
if (response.StatusCode != HttpStatusCode.OK)
{
this.client.ParseError(response);
}
var contentBytes = System.Text.Encoding.UTF8.GetBytes(response.Content);
var stream = new MemoryStream(contentBytes);
policy = BucketPolicy.parseJson(stream, bucketName);
Expand All @@ -277,15 +250,14 @@ private async Task<BucketPolicy> GetPolicyAsync(string bucketName)
return policy;
}




/// <summary>
/// Get bucket policy at given objectPrefix
/// </summary>
/// <param name="bucketName">Bucket name.</param>
/// <param name="objectPrefix">Name of the object prefix</param>
/// <returns>Returns the PolicyType </returns>
public async Task<PolicyType> GetPolicyAsync(string bucketName, string objectPrefix ="")
public async Task<PolicyType> GetPolicyAsync(string bucketName, string objectPrefix = "")
{
BucketPolicy policy = await GetPolicyAsync(bucketName);
return policy.getPolicy(objectPrefix);
Expand All @@ -299,15 +271,14 @@ public async Task<PolicyType> GetPolicyAsync(string bucketName, string objectPre
/// <returns></returns>
private async Task setPolicyAsync(string bucketName, BucketPolicy policy)
{

string policyJson = policy.getJson();
var request = await client.CreateRequest(Method.PUT, bucketName,
resourcePath:"?policy",
region: BucketRegionCache.Instance.Region(bucketName),
var request = await this.CreateRequest(Method.PUT, bucketName,
resourcePath: "?policy",
contentType: "application/json",
body:policyJson);
body: policyJson);

IRestResponse response = await this.client.ExecuteTaskAsync(this.client.NoErrorHandlers, request);
IRestResponse response = await this.ExecuteTaskAsync(this.NoErrorHandlers, request);
}

/// <summary>
Expand All @@ -333,8 +304,4 @@ public async Task SetPolicyAsync(String bucketName, String objectPrefix, PolicyT
await setPolicyAsync(bucketName, policy);
}
}
}




}
Loading

0 comments on commit 32174d9

Please sign in to comment.