Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GetPresignedUrl Functionality to Minio .NET Client #1169

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2762,6 +2762,48 @@ catch(MinioException e)
}
```

<a name="getPresignedUrl"></a>
### GetPresignedUrlAsync(GetPresignedUrlArgs args)

`Task<string> GetPresignedUrlAsync(GetPresignedUrlArgs args)`

Generates a presigned URL for HTTP operation using the giving HTTP method (GET, PUT, DELETE). Browsers/Mobile clients may point to this URL to upload objects directly to a bucket even if it is private. This presigned URL can have an associated expiration time in seconds after which it is no longer operational. The default expiry is set to 7 days.

__Parameters__


| Param | Type | Description |
|:---------|:------------------------|:-------------------------------------------------------------------------------------|
| ``args`` | _GetPresignedUrlArgs_ | GetPresignedUrlArgs arguments object with HTTP method, bucket, object names & expiry |

| Return Type | Exceptions |
|:------------------------------------------------------------|:-------------------------------------------------------------------|
| ``Task<string>`` : string contains URL to upload the object | Listed Exceptions: |
| | ``InvalidBucketNameException`` : upon invalid bucket name |
| | ``InvalidKeyException`` : upon an invalid access key or secret key |
| | ``ConnectionException`` : upon connection error |
| | ``InvalidExpiryRangeException`` : upon invalid expiry range. |


__Example__

```cs
try
{
var httpMethod = GetPresignedUrlArgs.PresignedUrlHttpMethod.Put;
GetPresignedUrlArgs args = GetPresignedUrlArgs(httpMethod)
.WithBucket("mybucket")
.WithObject("myobject")
.WithExpiry(60 * 60 * 24);
String url = await minioClient.GetPresignedUrlAsync(args);
Console.WriteLine(url);
}
catch(MinioException e)
{
Console.WriteLine("Error occurred: " + e);
}
```

<a name="presignedPostPolicy"></a>
### PresignedPostPolicy(PresignedPostPolicyArgs args)

Expand Down
56 changes: 56 additions & 0 deletions Minio.Examples/Cases/GetPresignedUrl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 Minio.DataModel.Args;

namespace Minio.Examples.Cases;

public static class GetPresignedUrl
{
public static async Task Run(IMinioClient client,
string bucketName = "my-bucket-name",
string objectName = "my-object-name")
{
if (client is null) throw new ArgumentNullException(nameof(client));

try
{
await GetPresignedUrlByRequest(client,
GetPresignedUrlArgs.PresignedUrlHttpMethod.Get, bucketName, objectName).ConfigureAwait(false);
await GetPresignedUrlByRequest(client,
GetPresignedUrlArgs.PresignedUrlHttpMethod.Put, bucketName, objectName).ConfigureAwait(false);
await GetPresignedUrlByRequest(client,
GetPresignedUrlArgs.PresignedUrlHttpMethod.Delete, bucketName, objectName).ConfigureAwait(false);
}
catch (Exception e)
{
Console.WriteLine($"Exception {e.Message}");
}
}

private static async Task GetPresignedUrlByRequest(IMinioClient client,
GetPresignedUrlArgs.PresignedUrlHttpMethod requestMethod,
string bucketName = "my-bucket-name",
string objectName = "my-object-name")
{
var args = new GetPresignedUrlArgs(requestMethod)
.WithBucket(bucketName)
.WithObject(objectName)
.WithExpiry(1000);
var presignedUrl = await client.GetPresignedUrlAsync(args).ConfigureAwait(false);
Console.WriteLine($"Presigned '{requestMethod}' URL: {presignedUrl}");
}
}
4 changes: 4 additions & 0 deletions Minio.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Security.Cryptography;
using System.Text;
using Minio.DataModel;
using Minio.DataModel.Args;
using Minio.DataModel.Encryption;
using Minio.DataModel.Notification;
using Minio.DataModel.ObjectLock;
Expand Down Expand Up @@ -261,6 +262,9 @@ await SetBucketReplication.Run(minioClient, bucketName, destBucketName, replicat

// Get the presigned url for a PUT object request
await PresignedPutObject.Run(minioClient, bucketName, objectName).ConfigureAwait(false);

// Get the presigned url's of an object for HTTP methods
await GetPresignedUrl.Run(minioClient, bucketName, objectName).ConfigureAwait(false);

// Delete the list of objects
await RemoveObjects.Run(minioClient, bucketName, objectsList).ConfigureAwait(false);
Expand Down
Loading
Loading