Skip to content

Commit

Permalink
reuse HttpClient, fix wrong TLS and Proxy settings during init() #641)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebozduman authored Jul 24, 2022
1 parent 70bc9bb commit 9a9d202
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 56 deletions.
18 changes: 10 additions & 8 deletions .github/workflows/minio-dotnet-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- name: Execute Functional Tests
env:
MINT_MODE: full
SERVER_ENDPOINT: 127.0.0.1:9000
SERVER_ENDPOINT: localhost:9000
ACCESS_KEY: minio
SECRET_KEY: minio123
ENABLE_HTTPS: 1
Expand All @@ -57,10 +57,12 @@ jobs:
MINIO_SECRET_KEY: minio123
MINIO_KMS_SECRET_KEY: my-minio-key:OSMM+vkKUTCvQs9YL/CVMIMt43HFhkUpqJxTmGl6rYw=
run: |
wget --quiet -O /tmp/minio https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x /tmp/minio
mkdir -p /tmp/minio-config/certs/
cp Minio.Functional.Tests/certs/* /tmp/minio-config/certs/
/tmp/minio -C /tmp/minio-config server /tmp/fs{1...4} &
dotnet build Minio.Functional.Tests --configuration Release
dotnet Minio.Functional.Tests/bin/Release/net6.0/Minio.Functional.Tests.dll
wget --quiet -O /tmp/minio https://dl.min.io/server/minio/release/linux-amd64/minio
chmod +x /tmp/minio
mkdir -p /tmp/minio-config/certs/
cp Minio.Functional.Tests/certs/* /tmp/minio-config/certs/
sudo cp /tmp/minio-config/certs/public.crt /etc/ssl/certs/
sudo cp /tmp/minio-config/certs/private.key /etc/ssl/private/
/tmp/minio -C /tmp/minio-config server /tmp/fs{1...4} &
dotnet build Minio.Functional.Tests --configuration Release
dotnet Minio.Functional.Tests/bin/Release/net6.0/Minio.Functional.Tests.dll
18 changes: 8 additions & 10 deletions Docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,14 @@ MinioClient minioClient = new MinioClient()
### AWS S3

```cs
// 1. public MinioClient(String endpoint, String accessKey, String secretKey)
MinioClient s3Client = new MinioClient("s3.amazonaws.com",
accessKey:"YOUR-ACCESSKEYID",
secretKey:"YOUR-SECRETACCESSKEY");
// 2. Using Builder with public MinioClient(), Endpoint, Credentials & Secure connection
MinioClient minioClient = new MinioClient()
.WithEndpoint("s3.amazonaws.com")
.WithCredentials("YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY")
.WithSSL()
.Build()
// 1. Using Builder with public MinioClient(), Endpoint, Credentials, Secure connection & proxy
MinioClient s3Client = new MinioClient()
.WithEndpoint("s3.amazonaws.com")
.WithCredentials("YOUR-AWS-ACCESSKEYID", "YOUR-AWS-SECRETACCESSKEY")
.WithSSL()
.WithProxy(proxy)
.Build();

```

## 2. Bucket operations
Expand Down
35 changes: 7 additions & 28 deletions Minio.Functional.Tests/FunctionalTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,16 +1187,9 @@ internal static async Task RemoveObjects_Test3(MinioClient minio)
}
}

internal static async Task DownloadObjectAsync(string url, string filePath)
internal static async Task DownloadObjectAsync(MinioClient minio, string url, string filePath)
{
var clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
{
return true;
};
var client = new HttpClient(clientHandler);

var response = await client.GetAsync(url);
var response = await minio.WrapperGetAsync(url).ConfigureAwait(false);
if (string.IsNullOrEmpty(Convert.ToString(response.Content)) || !HttpStatusCode.OK.Equals(response.StatusCode))
throw new ArgumentNullException("Unable to download via presigned URL");

Expand All @@ -1206,18 +1199,11 @@ internal static async Task DownloadObjectAsync(string url, string filePath)
}
}

internal static async Task UploadObjectAsync(string url, string filePath)
internal static async Task UploadObjectAsync(MinioClient minio, string url, string filePath)
{
var clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
{
return true;
};
var client = new HttpClient(clientHandler);

using (var strm = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read)))
{
await client.PutAsync(url, strm);
await minio.WrapperPutAsync(url, strm).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -4847,7 +4833,7 @@ internal static async Task PresignedGetObject_Test1(MinioClient minio)
.WithExpiry(expiresInt);
var presigned_url = await minio.PresignedGetObjectAsync(preArgs);

await DownloadObjectAsync(presigned_url, downloadFile);
await DownloadObjectAsync(minio, presigned_url, downloadFile).ConfigureAwait(false);
var writtenInfo = new FileInfo(downloadFile);
var file_read_size = writtenInfo.Length;
// Compare the size of the file downloaded using the generated
Expand Down Expand Up @@ -4985,14 +4971,7 @@ internal static async Task PresignedGetObject_Test3(MinioClient minio)
.WithRequestDate(reqDate);
var presigned_url = await minio.PresignedGetObjectAsync(preArgs);

var clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
{
return true;
};
var client = new HttpClient(clientHandler);

var response = await client.GetAsync(presigned_url);
var response = await minio.WrapperGetAsync(presigned_url).ConfigureAwait(false);
if (string.IsNullOrEmpty(Convert.ToString(response.Content)) ||
!HttpStatusCode.OK.Equals(response.StatusCode))
throw new ArgumentNullException("Unable to download via presigned URL");
Expand Down Expand Up @@ -5059,7 +5038,7 @@ internal static async Task PresignedPutObject_Test1(MinioClient minio)
.WithObject(objectName)
.WithExpiry(1000);
var presigned_url = await minio.PresignedPutObjectAsync(presignedPutObjectArgs);
await UploadObjectAsync(presigned_url, fileName);
await UploadObjectAsync(minio, presigned_url, fileName);
// Get stats for object from server
var statObjectArgs = new StatObjectArgs()
.WithBucket(bucketName)
Expand Down
10 changes: 0 additions & 10 deletions Minio.Functional.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

using System;
using System.Net;
using System.Net.Http;

namespace Minio.Functional.Tests;

Expand Down Expand Up @@ -51,21 +50,12 @@ public static void Main(string[] args)

MinioClient minioClient = null;

var clientHandler = new HttpClientHandler();
clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
{
return true;
};
clientHandler.UseProxy = false;
var httpClient = new HttpClient(clientHandler);

if (enableHttps == "1")
// WithSSL() enables SSL support in MinIO client
minioClient = new MinioClient()
.WithSSL()
.WithCredentials(accessKey, secretKey)
.WithEndpoint(endPoint)
.WithHttpClient(httpClient)
.Build();
else
minioClient = new MinioClient()
Expand Down
18 changes: 18 additions & 0 deletions Minio/MinioClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public MinioClient()
/// Creates and returns an MinIO Client with custom HTTP Client
/// </summary>
/// <returns>Client with no arguments to be used with other builder methods</returns>
[Obsolete("Use MinioClient() and Builder method .WithHttpClient(httpClient)")]
public MinioClient(HttpClient httpClient)
{
Region = "";
Expand Down Expand Up @@ -175,6 +176,23 @@ private static string SystemUserAgent
/// </summary>
private string FullUserAgent => $"{SystemUserAgent} {CustomUserAgent}";

/// <summary>
/// Runs httpClient's GetAsync method
/// </summary>
public async Task<HttpResponseMessage> WrapperGetAsync(string url)
{
var response = await HTTPClient.GetAsync(url).ConfigureAwait(false);
return response;
}

/// <summary>
/// Runs httpClient's PutObjectAsync method
/// </summary>
public async Task WrapperPutAsync(string url, StreamContent strm)
{
await Task.Run(async () => await HTTPClient.PutAsync(url, strm).ConfigureAwait(false)).ConfigureAwait(false);
}

/// <summary>
/// Resolve region of the bucket.
/// </summary>
Expand Down

0 comments on commit 9a9d202

Please sign in to comment.