Created
September 17, 2019 07:59
-
-
Save sydseter/f7432c6268cefa3974c1fba05c398ddd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Amazon.S3; | |
using Amazon; | |
using NUnit.Framework; | |
using System; | |
using System.Diagnostics; | |
using Amazon.S3.Model; | |
using System.IO; | |
using System.Threading.Tasks; | |
using System.Net.Http; | |
using System.Security.Cryptography.X509Certificates; | |
using Amazon.Runtime; | |
namespace AwsS3MTLSNginxMinio | |
{ | |
public class MyAWSS3MTLSUploader | |
{ | |
static void Main(string[] args) | |
{ | |
Debug.WriteLine("testing that I can write a line"); | |
var accessKey = Environment.GetEnvironmentVariable("AccessKey"); | |
Assert.IsNotNull(accessKey, "AccessKey: {0}", accessKey, "Please set the Minio AccessKey as an environment variable before running the tests."); | |
var secretKey = Environment.GetEnvironmentVariable("SecretKey"); | |
Assert.IsNotNull(accessKey, "SecretKey: {0}", secretKey, "Please set the Minio SecretKey as an environment variable before running the tests."); | |
var config = new AmazonS3Config | |
{ | |
RegionEndpoint = RegionEndpoint.USEast1, // MUST set this before setting ServiceURL and it should match the `MINIO_REGION` enviroment variable. | |
ServiceURL = "https://localhost:8092", // replace http://localhost:8092 with URL of your MinIO server | |
ForcePathStyle = true, // MUST be true to work correctly with MinIO server | |
HttpClientFactory = new MyHttpClientFactory() | |
}; | |
var amazonS3Client = new AmazonS3Client(accessKey, secretKey, config); | |
String fileToUpload = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/healthTreatment.json")); | |
var putRequest = new PutObjectRequest | |
{ | |
BucketName = "test", | |
Key = "healthTreatment.json", | |
FilePath = fileToUpload, | |
ContentType = "text/plain" | |
}; | |
WritingAnObjectAsync(amazonS3Client, putRequest).Wait(); | |
} | |
static async Task WritingAnObjectAsync(AmazonS3Client amazonS3Client, PutObjectRequest putRequest) | |
{ | |
PutObjectResponse response = await amazonS3Client.PutObjectAsync(putRequest); | |
Assert.AreEqual("OK", response.HttpStatusCode.ToString().Trim()); | |
} | |
} | |
public class MyHttpClientFactory : Amazon.Runtime.HttpClientFactory | |
{ | |
public override HttpClient CreateHttpClient(IClientConfig clientConfig) | |
{ | |
var httpMessageHandler = CreateClientHandler(); | |
//Adds the client certificate to the Http message handler | |
httpMessageHandler.ClientCertificates.Add(LoadClientCertificateCredentials()); | |
if (clientConfig.MaxConnectionsPerServer.HasValue) | |
{ | |
httpMessageHandler.MaxConnectionsPerServer = clientConfig.MaxConnectionsPerServer.Value; | |
} | |
httpMessageHandler.AllowAutoRedirect = clientConfig.AllowAutoRedirect; | |
var proxy = clientConfig.GetWebProxy(); | |
if (proxy != null) | |
{ | |
httpMessageHandler.Proxy = proxy; | |
} | |
if (httpMessageHandler.Proxy != null && clientConfig.ProxyCredentials != null) | |
{ | |
httpMessageHandler.Proxy.Credentials = clientConfig.ProxyCredentials; | |
} | |
var httpClient = new HttpClient(httpMessageHandler); | |
if (clientConfig.Timeout.HasValue) | |
{ | |
httpClient.Timeout = clientConfig.Timeout.Value; | |
} | |
return httpClient; | |
} | |
protected virtual HttpClientHandler CreateClientHandler() => | |
new HttpClientHandler | |
{ | |
ClientCertificateOptions = ClientCertificateOption.Manual, | |
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }, | |
}; | |
private X509Certificate2 LoadClientCertificateCredentials() | |
{ | |
String clientCertToLoad = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/certs/client.p12")); | |
return new X509Certificate2(clientCertToLoad, "root"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment