Browse Source

add examples for presigned operations

pull/129/head
poornas 9 years ago
parent
commit
792642cbfa
  1. 6
      Minio.Api/ApiEndpoints/BucketOperations.cs
  2. 170
      Minio.Api/MinioRestClient.cs
  3. 40
      Minio.Examples/Cases/PresignedGetObject.cs
  4. 39
      Minio.Examples/Cases/PresignedPostPolicy.cs
  5. 25
      Minio.Examples/Cases/PresignedPutObject.cs
  6. 3
      Minio.Examples/Minio.Examples.csproj
  7. 64
      Minio.Examples/Program.cs
  8. 19
      README.md

6
Minio.Api/ApiEndpoints/BucketOperations.cs

@ -257,11 +257,7 @@ namespace Minio
var path =bucketName + "?policy";
var request = new RestRequest(path, Method.GET);
request.AddHeader("Content-Type", "application/json");
await this.client.ModifyTargetURL(request, bucketName);
request = await client.CreateRequest(Method.GET, bucketName,
var request = await client.CreateRequest(Method.GET, bucketName,
region: BucketRegionCache.Instance.Region(bucketName),
contentType:"application/json",
resourcePath:"?policy");

170
Minio.Api/MinioRestClient.cs

@ -39,11 +39,15 @@ namespace Minio
internal string BaseUrl { get; private set; }
internal bool Secure { get; private set; }
internal bool Anonymous { get; }
internal Uri uri;
internal string s3AccelerateEndpoint;
internal RestClient restClient;
internal V4Authenticator authenticator;
internal BucketRegionCache regionCache;
private bool trace = false;
public ClientApiOperations Api;
internal readonly IEnumerable<ApiResponseErrorHandlingDelegate> NoErrorHandlers = Enumerable.Empty<ApiResponseErrorHandlingDelegate>();
@ -221,108 +225,12 @@ namespace Minio
return request;
}
internal void ModifyAWSEndpointFor(string region, string bucketName = null)
{
if (region != null)
{
_constructUri(region, bucketName);
this.restClient.BaseUrl = this.uri;
}
}
internal async Task<string> ModifyTargetURL(IRestRequest request, string bucketName,bool usePathStyle=false)
{
var resource_url = this.Endpoint;
if (s3utils.IsAmazonEndPoint(this.BaseUrl))
{
// ``us-east-1`` is not a valid location constraint according to amazon, so we skip it.
string location = await BucketRegionCache.Instance.Update(this,bucketName);
// if (location != "us-east-1")
{
ModifyAWSEndpointFor(location, bucketName);
resource_url = MakeTargetURL(location, bucketName,usePathStyle);
}
//else
//{ // use default request
// resource_url = "";
// }
}
return resource_url;
}
internal string MakeTargetURL(string region = null, string bucketName = null,bool usePathStyle=false)
{
string targetUrl = null;
string host = this.BaseUrl;
// For Amazon S3 endpoint, try to fetch location based endpoint.
if (s3utils.IsAmazonEndPoint(this.BaseUrl))
{
if (this.s3AccelerateEndpoint != null && bucketName != null)
{
// http://docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html
// Disable transfer acceleration for non-compliant bucket names.
if (bucketName.Contains("."))
{
throw new InvalidTransferAccelerationBucketException(bucketName);
}
// If transfer acceleration is requested set new host.
// For more details about enabling transfer acceleration read here.
// http://docs.aws.amazon.com/AmazonS3/latest/dev/transfer-acceleration.html
host = s3AccelerateEndpoint;
}
else
{
// Fetch new host based on the bucket location.
host = AWSS3Endpoints.Instance.endpoint(region);
}
}
var scheme = this.Secure ? Uri.UriSchemeHttps : Uri.UriSchemeHttp;
// Make URL only if bucketName is available, otherwise use the
// endpoint URL.
if (bucketName != null && s3utils.IsAmazonEndPoint(this.BaseUrl))
{
// Save if target url will have buckets which suppport virtual host.
bool isVirtualHostStyle = s3utils.IsVirtualHostSupported(uri, bucketName);
if (bucketName.Contains(".") && this.Secure)
{
// use path style where '.' in bucketName causes SSL certificate validation error
usePathStyle = true;
}
// If endpoint supports virtual host style use that always.
// Currently only S3 and Google Cloud Storage would support
// virtual host style.
string urlStr = null;
if (isVirtualHostStyle || !usePathStyle)
{
targetUrl = scheme + "://" + bucketName + "." + host + "/";
}
else
{
// If not fall back to using path style.
targetUrl = urlStr + bucketName + "/";
}
}
else
{
targetUrl = string.Format("{0}://{1}", scheme, this.BaseUrl);
}
return targetUrl;
}
/// <summary>
/// helper to construct uri and validate it.
/// </summary>
/// <summary>
/// Helper function to construct URI and validate it
/// </summary>
/// <param name="region">Region - applies only to AWS</param>
/// <param name="bucketName">Bucket Name</param>
private void _constructUri(string region = null, string bucketName = null)
{
if (string.IsNullOrEmpty(this.BaseUrl))
@ -342,36 +250,7 @@ namespace Minio
this.Endpoint = string.Format("{0}://{1}", scheme, host);
this.uri = new Uri(this.Endpoint);
}
/*
// Make URL only if bucketName is available, otherwise use the
// endpoint URL.
if (bucketName != null && s3utils.IsAmazonEndPoint(this.BaseUrl))
{
// Save if target url will have buckets which suppport virtual host.
bool isVirtualHostStyle = s3utils.IsVirtualHostSupported(this.uri, bucketName);
// If endpoint supports virtual host style use that always.
// Currently only S3 and Google Cloud Storage would support
// virtual host style.
string urlStr = null;
if (isVirtualHostStyle)
{
this.Endpoint = scheme + "://" + bucketName + "." + host + "/";
}
else
{
// If not fall back to using path style.
this.Endpoint = urlStr + bucketName + "/";
}
}
else
{
this.Endpoint = string.Format("{0}://{1}", scheme, this.BaseUrl);
}
*/
// this.uri = new Uri(this.Endpoint);
/// <summary>
/// validates URI
@ -401,10 +280,9 @@ namespace Minio
&& !(amzHost.Equals("s3.amazonaws.com", StringComparison.CurrentCultureIgnoreCase)))
{
throw new InvalidEndpointException(this.Endpoint, "For Amazon S3, host should be \'s3.amazonaws.com\' in endpoint.");
}
}
}
/// <summary>
/// Validate Url endpoint
/// </summary>
@ -438,6 +316,7 @@ namespace Minio
return true;
}
/// <summary>
///Sets app version and name
/// </summary>
@ -458,6 +337,7 @@ namespace Minio
this.restClient.UserAgent = this.FullUserAgent;
}
/// <summary>
/// Creates and returns an Cloud Storage client
/// </summary>
@ -497,6 +377,7 @@ namespace Minio
return;
}
/// <summary>
/// Connects to Cloud Storage with HTTPS if this method is invoked on client object
/// </summary>
@ -525,16 +406,10 @@ namespace Minio
return response;
}
/// <summary>
/// Parse response errors if any and return relevant error messages
/// </summary>
/// <param name="response"></param>
internal void ParseError(IRestResponse response)
{
if (response == null)
@ -633,7 +508,20 @@ namespace Minio
_defaultErrorHandlingDelegate(response);
}
/// <summary>
/// Sets HTTP tracing On.Writes output to Console
/// </summary>
public void SetTraceOn()
{
this.trace = true;
}
/// <summary>
/// Sets HTTP tracing Off.
/// </summary>
public void SetTraceOff()
{
this.trace = false;
}
private void LogRequest(IRestRequest request, IRestResponse response, long durationMs)
{
var requestToLog = new

40
Minio.Examples/Cases/PresignedGetObject.cs

@ -0,0 +1,40 @@
/*
* Minio .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2015 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 Minio;
namespace Minio.Examples.Cases
{
public class PresignedGetObject
{
public static int Run()
{
/// Note: s3 AccessKey and SecretKey needs to be added in App.config file
/// See instructions in README.md on running examples for more information.
var client = new MinioRestClient(
Environment.GetEnvironmentVariable("AWS_ENDPOINT"),
Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"),
Environment.GetEnvironmentVariable("AWS_SECRET_KEY")
).WithSSL();
Console.Out.WriteLine(client.Api.PresignedGetObject("my-bucketname", "my-objectname", 1000));
return 0;
}
}
}

39
Minio.Examples/Cases/PresignedPostPolicy.cs

@ -0,0 +1,39 @@
using Minio.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Minio.Examples.Cases
{
public class PresignedPostPolicy
{
public static int Run()
{
/// Note: s3 AccessKey and SecretKey needs to be added in App.config file
/// See instructions in README.md on running examples for more information.
var client = new MinioRestClient(
Environment.GetEnvironmentVariable("AWS_ENDPOINT"),
Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"),
Environment.GetEnvironmentVariable("AWS_SECRET_KEY")
).WithSSL();
PostPolicy form = new PostPolicy();
DateTime expiration = DateTime.UtcNow;
form.SetExpires(expiration.AddDays(10));
form.SetKey("my-objectname");
form.SetBucket("my-bucketname");
Dictionary<string, string> formData = client.Api.PresignedPostPolicy(form);
string curlCommand = "curl ";
foreach (KeyValuePair<string, string> pair in formData)
{
curlCommand = curlCommand + " -F " + pair.Key + "=" + pair.Value;
}
curlCommand = curlCommand + " -F file=@/etc/bashrc https://s3.amazonaws.com/my-bucketname";
Console.Out.WriteLine(curlCommand);
return 0;
}
}
}

25
Minio.Examples/Cases/PresignedPutObject.cs

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Minio.Examples.Cases
{
public class PresignedPutObject
{
public static int Run()
{
/// Note: s3 AccessKey and SecretKey needs to be added in App.config file
/// See instructions in README.md on running examples for more information.
var client = new MinioRestClient(
Environment.GetEnvironmentVariable("AWS_ENDPOINT"),
Environment.GetEnvironmentVariable("AWS_ACCESS_KEY"),
Environment.GetEnvironmentVariable("AWS_SECRET_KEY")
).WithSSL();
Console.Out.WriteLine(client.Api.PresignedPutObject("my-bucketname", "my-objectname", 1000));
return 0;
}
}
}

3
Minio.Examples/Minio.Examples.csproj

@ -69,6 +69,9 @@
<Compile Include="Cases\ListIncompleteUploads.cs" />
<Compile Include="Cases\ListObjects.cs" />
<Compile Include="Cases\MakeBucket.cs" />
<Compile Include="Cases\PresignedGetObject.cs" />
<Compile Include="Cases\PresignedPostPolicy.cs" />
<Compile Include="Cases\PresignedPutObject.cs" />
<Compile Include="Cases\PutObject.cs" />
<Compile Include="Cases\RemoveBucket.cs" />
<Compile Include="Cases\RemoveIncompleteUpload.cs" />

64
Minio.Examples/Program.cs

@ -1,12 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Minio;
using System.Net;
using Minio.Exceptions;
using System.Text.RegularExpressions;
namespace Minio.Examples
{
@ -26,46 +20,52 @@ namespace Minio.Examples
| SecurityProtocolType.Tls12;
var endPoint = Environment.GetEnvironmentVariable("AWS_ENDPOINT");
var accessKey = Environment.GetEnvironmentVariable("MY_AWS_ACCESS_KEY");
var secretKey = Environment.GetEnvironmentVariable("MY_AWS_SECRET_KEY");
var accessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY");
var secretKey = Environment.GetEnvironmentVariable("AWS_SECRET_KEY");
var minioClient = new MinioRestClient(endPoint,
accessKey: accessKey,
secretKey: secretKey).WithSSL();
try
{
string bucketName = "testminiopolicyzzz";
string objectName ="testobject";
string prefix = "mult";
string smallFilePath = "C:\\Users\\vagrant\\Downloads\\hellotext";
// Change these parameters before running examples
string bucketName = "sanfrancisco";
string objectName ="goldengate_pic";
string objectPrefix = "gold";
string smallFilePath = "C:\\Users\\vagrant\\Downloads\\hello.txt";
string uploadFilePath = "C:\\Users\\vagrant\\Downloads\\go1.7.4.windows-amd64.msi";
string downloadFilePath = "C:\\Users\\vagrant\\Downloads\\downloaded-object";
string destBucketName = "mtky2";
string destObjectName = "copyrighted_copy.txt";
string removeObject = "newmulti-406";
string destBucketName = "backup_folder";
string destObjectName = "goldengate_copy";
string removeObject = "goldengate_pic";
//
//Set app Info
minioClient.SetAppInfo("app-name", "app-version");
Cases.MakeBucket.Run(minioClient, bucketName).Wait();
Cases.BucketExists.Run(minioClient, bucketName).Wait();
Cases.ListBuckets.Run(minioClient).Wait();
Cases.ListObjects.Run(minioClient, bucketName);
Cases.PutObject.Run(minioClient, bucketName, objectName, smallFilePath).Wait();
Cases.GetObject.Run(minioClient, bucketName, objectName).Wait();
Cases.FPutObject.Run(minioClient, bucketName, objectName,uploadFilePath).Wait();
//* UNCOMMENT CASE TO RUN A TEST
//Cases.MakeBucket.Run(minioClient, bucketName).Wait();
//Cases.BucketExists.Run(minioClient, bucketName).Wait();
//Cases.ListBuckets.Run(minioClient).Wait();
//Cases.ListObjects.Run(minioClient, bucketName);
//Cases.PutObject.Run(minioClient, bucketName, objectName, smallFilePath).Wait();
//Cases.GetObject.Run(minioClient, bucketName, objectName).Wait();
//Cases.FPutObject.Run(minioClient, bucketName, objectName,uploadFilePath).Wait();
Cases.FGetObject.Run(minioClient, bucketName, objectName,downloadFilePath).Wait();
//Cases.FGetObject.Run(minioClient, bucketName, objectName,downloadFilePath).Wait();
Cases.RemoveBucket.Run(minioClient, bucketName).Wait();
Cases.ListIncompleteUploads.Run(minioClient, bucketName, prefix:prefix);
Cases.RemoveIncompleteUpload.Run(minioClient, bucketName, removeObject).Wait();
//Cases.RemoveBucket.Run(minioClient, bucketName).Wait();
//Cases.ListIncompleteUploads.Run(minioClient, bucketName, prefix:objectPrefix);
//Cases.RemoveIncompleteUpload.Run(minioClient, bucketName, removeObject).Wait();
Cases.GetBucketPolicy.Run(minioClient, bucketName).Wait();
//Cases.GetBucketPolicy.Run(minioClient, bucketName).Wait();
Cases.SetBucketPolicy.Run(minioClient, bucketName).Wait();
Cases.StatObject.Run(minioClient, bucketName, objectName).Wait();
Cases.CopyObject.Run(minioClient, bucketName, objectName, destBucketName, destObjectName).Wait();
//Cases.SetBucketPolicy.Run(minioClient, bucketName).Wait();
//Cases.StatObject.Run(minioClient, bucketName, objectName).Wait();
//Cases.CopyObject.Run(minioClient, bucketName, objectName, destBucketName, destObjectName).Wait();
//Cases.PresignedGetObject.Run();
//Cases.PresignedPostPolicy.Run();
//Cases.PresignedPutObject.Run();
Console.ReadLine();
}
catch(ClientException ex)

19
README.md

@ -81,13 +81,13 @@ namespace FileUploader
try
{
bool success = await minio.Buckets.MakeBucketAsync(bucketName, location);
bool success = await minio.Api.MakeBucketAsync(bucketName, location);
if (!success) {
bool found = await minio.Buckets.BucketExistsAsync(bucketName);
bool found = await minio.Api.BucketExistsAsync(bucketName);
Console.Out.WriteLine("bucket-name was " + ((found == true) ? "found" : "not found"));
}
else {
await minio.Objects.PutObjectAsync(bucketName, objectName, filePath, contentType);
await minio.Api.PutObjectAsync(bucketName, objectName, filePath, contentType);
Console.Out.WriteLine("Successfully uploaded " + objectName);
}
@ -114,15 +114,13 @@ Click on FileUploader project and Start
* [ListBuckets.cs](./Minio.Examples/Cases/ListBuckets.cs)
* [BucketExists.cs](./Minio.Examples/Cases/BucketExists.cs)
* [RemoveBucket.cs](./Minio.Examples/Cases/RemoveBucket.cs)
* [ListIncompleteUploads.cs](./Minio.Examples/Cases/ListIncompleteUploads.cs)
* [Listobjects.cs](./Minio.Examples/Cases/Listobjects.cs)
* [ListIncompleteUploads.cs](./Minio.Examples/Cases/ListIncompleteUploads.cs)
#### Full Examples : Bucket policy Operations
* [GetPolicy.cs] (./Minio.Examples/Cases/GetPolicy.cs)
* [SetPolicy.cs] (./Minio.Examples/Cases/SetPolicy.cs)
#### Full Examples : Bucket notification Operations
#### Full Examples : File Object Operations
* [FGetObject.cs] (./Minio.Examples/Cases/FGetObject.cs)
* [FPutObject.cs] (./Minio.Examples/Cases/FPutObject.cs)
@ -132,21 +130,24 @@ Click on FileUploader project and Start
* [PutObject.cs] (./Minio.Examples/Cases/PutObject.cs)
* [StatObject.cs](./Minio.Examples/Cases/StatObject.cs)
* [RemoveObject.cs](./Minio.Examples/Cases/RemoveObject.cs)
* [RemoveIncompleteUpload.cs](./Minio.Examples/Cases/RemoveIncompleteUpload.cs)
* [CopyObject.cs] (./Minio.Examples/Cases/CopyObject.cs)
* [RemoveIncompleteUpload.cs](./Minio.Examples/Cases/RemoveIncompleteUpload.cs)
#### Full Examples : Presigned Operations
* [PresignedGetObject.cs] (./Minio.Examples/Cases/PresignedGetObject.cs)
* [PresignedPutObject.cs] (./Minio.Examples/Cases/PresignedPutObject.cs)
* [PresignedPostPolicy.cs] (./Minio.Examples/Cases/PresignedPostPolicy.cs)
#### Full Examples : Client Custom Settings
* [SetAppInfo] (./Minio.Examples/Program.cs)
* [SetTraceOn] (./Minio.Examples/Program.cs)
* [SetTraceOff] (./Minio.Examples/Program.cs)
### How to run these examples?
### On Windows
•Build Minio solution
•Move into Minio.Examples directory and run the project. Uncomment cases that you want to run
to play with it.

Loading…
Cancel
Save