-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathProgram.cs
290 lines (236 loc) · 12.7 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage, (C) 2017, 2020 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.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using Minio.DataModel;
using Minio.DataModel.Encryption;
using Minio.DataModel.Notification;
using Minio.DataModel.ObjectLock;
using Minio.Examples.Cases;
using Minio.Helper;
namespace Minio.Examples;
public static class Program
{
private const int UNIT_MB = 1024 * 1024;
private static readonly Random rnd = new();
// Create a file of given size from random byte array
private static string CreateFile(int size)
{
var fileName = GetRandomName();
var data = new byte[size];
rnd.NextBytes(data);
File.WriteAllBytes(fileName, data);
return fileName;
}
// Generate a random string
public static string GetRandomName()
{
var characters = "0123456789abcdefghijklmnopqrstuvwxyz";
var result = new StringBuilder(5);
for (var i = 0; i < 5; i++) _ = result.Append(characters[rnd.Next(characters.Length)]);
return "minio-dotnet-example-" + result;
}
[SuppressMessage("Design", "MA0051:Method is too long", Justification = "Needs to run all tests")]
public static async Task Main()
{
string endPoint = null;
string accessKey = null;
string secretKey = null;
var isSecure = false;
var port = 80;
if (Environment.GetEnvironmentVariable("SERVER_ENDPOINT") is not null)
{
endPoint = Environment.GetEnvironmentVariable("SERVER_ENDPOINT");
var posColon = endPoint.LastIndexOf(':');
if (posColon != -1)
{
port = int.Parse(endPoint.Substring(posColon + 1, endPoint.Length - posColon - 1), NumberStyles.Integer,
CultureInfo.InvariantCulture);
endPoint = endPoint[..posColon];
}
accessKey = Environment.GetEnvironmentVariable("ACCESS_KEY");
secretKey = Environment.GetEnvironmentVariable("SECRET_KEY");
if (Environment.GetEnvironmentVariable("ENABLE_HTTPS") is not null)
{
isSecure = Environment.GetEnvironmentVariable("ENABLE_HTTPS")
.Equals("1", StringComparison.OrdinalIgnoreCase);
if (isSecure && port == 80) port = 443;
}
}
else
{
endPoint = "play.min.io";
accessKey = "Q3AM3UQ867SPQQA43P2F";
secretKey = "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG";
isSecure = true;
port = 443;
}
#pragma warning disable MA0039 // Do not write your own certificate validation method
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => true;
#pragma warning restore MA0039 // Do not write your own certificate validation method
using var minioClient = new MinioClient()
.WithEndpoint(endPoint, port)
.WithCredentials(accessKey, secretKey)
.WithSSL(isSecure)
.Build();
// Assign parameters before starting the test
var bucketName = GetRandomName();
var smallFileName = CreateFile(1 * UNIT_MB);
var bigFileName = CreateFile(6 * UNIT_MB);
var objectName = GetRandomName();
var destBucketName = GetRandomName();
var destObjectName = GetRandomName();
var lockBucketName = GetRandomName();
var progress = new SyncProgress<ProgressReport>(progressReport =>
{
Console.WriteLine(
$"Percentage: {progressReport.Percentage}% TotalBytesTransferred: {progressReport.TotalBytesTransferred} bytes");
if (progressReport.Percentage != 100)
Console.SetCursorPosition(0, Console.CursorTop - 1);
else Console.WriteLine();
});
var objectsList = new List<string>();
for (var i = 0; i < 10; i++) objectsList.Add(objectName + i);
// Set app Info
_ = minioClient.SetAppInfo("app-name", "app-version");
// Set HTTP Tracing On
// minioClient.SetTraceOn();
// Set HTTP Tracing Off
// minioClient.SetTraceOff();
// Check if bucket exists
await BucketExists.Run(minioClient, bucketName).ConfigureAwait(false);
// Create a new bucket
await MakeBucket.Run(minioClient, bucketName).ConfigureAwait(false);
await MakeBucket.Run(minioClient, destBucketName).ConfigureAwait(false);
// Bucket with Lock tests
await MakeBucketWithLock.Run(minioClient, lockBucketName).ConfigureAwait(false);
await BucketExists.Run(minioClient, lockBucketName).ConfigureAwait(false);
await RemoveBucket.Run(minioClient, lockBucketName).ConfigureAwait(false);
// Versioning tests
await GetVersioning.Run(minioClient, bucketName).ConfigureAwait(false);
await EnableSuspendVersioning.Run(minioClient, bucketName).ConfigureAwait(false);
await GetVersioning.Run(minioClient, bucketName).ConfigureAwait(false);
// List all the buckets on the server
await ListBuckets.Run(minioClient).ConfigureAwait(false);
// Start listening for bucket notifications
ListenBucketNotifications.Run(minioClient, bucketName, new List<EventType> { EventType.ObjectCreatedAll });
// Start listening for global notifications
ListenNotifications.Run(minioClient, new List<EventType> { EventType.BucketCreatedAll });
// Put an object to the new bucket
await PutObject.Run(minioClient, bucketName, objectName, smallFileName, progress).ConfigureAwait(false);
// Get object metadata
await StatObject.Run(minioClient, bucketName, objectName).ConfigureAwait(false);
// List the objects in the new bucket
await ListObjects.Run(minioClient, bucketName).ConfigureAwait(false);
// Get the file and Download the object as file
await GetObject.Run(minioClient, bucketName, objectName, smallFileName).ConfigureAwait(false);
// Select content from object
await SelectObjectContent.Run(minioClient, bucketName, objectName).ConfigureAwait(false);
// Delete the file and Download partial object as file
await GetPartialObject.Run(minioClient, bucketName, objectName, smallFileName).ConfigureAwait(false);
// Server side copyObject
await CopyObject.Run(minioClient, bucketName, objectName, destBucketName, objectName).ConfigureAwait(false);
// Server side copyObject with metadata replacement
await CopyObjectMetadata.Run(minioClient, bucketName, objectName, destBucketName, objectName)
.ConfigureAwait(false);
// Upload a File with PutObject
await FPutObject.Run(minioClient, bucketName, objectName, smallFileName).ConfigureAwait(false);
// Delete the file and Download the object as file
await FGetObject.Run(minioClient, bucketName, objectName, smallFileName).ConfigureAwait(false);
// Automatic Multipart Upload with object more than 5Mb
await PutObject.Run(minioClient, bucketName, objectName, bigFileName, progress).ConfigureAwait(false);
// Specify SSE-C encryption options
using var aesEncryption = Aes.Create();
aesEncryption.KeySize = 256;
aesEncryption.GenerateKey();
var ssec = new SSEC(aesEncryption.Key);
// Specify SSE-C source side encryption for Copy operations
var sseCpy = new SSECopy(aesEncryption.Key);
// Uncomment to specify SSE-S3 encryption option
var sses3 = new SSES3();
// Uncomment to specify SSE-KMS encryption option
var sseKms = new SSEKMS("kms-key",
new Dictionary<string, string>(StringComparer.Ordinal) { { "kms-context", "somevalue" } });
// Upload encrypted object
var putFileName1 = CreateFile(1 * UNIT_MB);
await PutObject.Run(minioClient, bucketName, objectName, putFileName1, progress, ssec).ConfigureAwait(false);
// Copy SSE-C encrypted object to unencrypted object
await CopyObject.Run(minioClient, bucketName, objectName, destBucketName, objectName, sseCpy, ssec)
.ConfigureAwait(false);
// Download SSE-C encrypted object
await FGetObject.Run(minioClient, destBucketName, objectName, bigFileName, ssec).ConfigureAwait(false);
// List the incomplete uploads
await ListIncompleteUploads.Run(minioClient, bucketName).ConfigureAwait(false);
// Remove all the incomplete uploads
await RemoveIncompleteUpload.Run(minioClient, bucketName, objectName).ConfigureAwait(false);
// Set a policy for given bucket
await SetBucketPolicy.Run(minioClient, bucketName).ConfigureAwait(false);
// Get the policy for given bucket
await GetBucketPolicy.Run(minioClient, bucketName).ConfigureAwait(false);
// Set bucket notifications
await SetBucketNotification.Run(minioClient, bucketName).ConfigureAwait(false);
// Get bucket notifications
await GetBucketNotification.Run(minioClient, bucketName).ConfigureAwait(false);
// Remove all bucket notifications
await RemoveAllBucketNotifications.Run(minioClient, bucketName).ConfigureAwait(false);
// Object Lock Configuration operations
lockBucketName = GetRandomName();
await MakeBucketWithLock.Run(minioClient, lockBucketName).ConfigureAwait(false);
var configuration = new ObjectLockConfiguration(ObjectRetentionMode.GOVERNANCE, 35);
await SetObjectLockConfiguration.Run(minioClient, lockBucketName, configuration).ConfigureAwait(false);
await GetObjectLockConfiguration.Run(minioClient, lockBucketName).ConfigureAwait(false);
await RemoveObjectLockConfiguration.Run(minioClient, lockBucketName).ConfigureAwait(false);
await RemoveBucket.Run(minioClient, lockBucketName).ConfigureAwait(false);
// Bucket Replication operations
var replicationRuleID = "myreplicationID-3333";
await SetBucketReplication.Run(minioClient, bucketName, destBucketName, replicationRuleID)
.ConfigureAwait(false);
await GetBucketReplication.Run(minioClient, bucketName, replicationRuleID).ConfigureAwait(false);
// TODO: we can verify that the replication happens by checking
// the content in the destination matches the source content.
// We also cannot remove the replication config immediately
// after running GetBucketReplication command, as
// replicating the source in the destination takes some time.
await RemoveBucketReplication.Run(minioClient, bucketName).ConfigureAwait(false);
// Get the presigned url for a GET object request
await PresignedGetObject.Run(minioClient, bucketName, objectName).ConfigureAwait(false);
// Get the presigned POST policy curl url
await PresignedPostPolicy.Run(minioClient, bucketName, objectName).ConfigureAwait(false);
// Get the presigned url for a PUT object request
await PresignedPutObject.Run(minioClient, bucketName, objectName).ConfigureAwait(false);
// Delete the list of objects
await RemoveObjects.Run(minioClient, bucketName, objectsList).ConfigureAwait(false);
// Delete the object
await RemoveObject.Run(minioClient, bucketName, objectName).ConfigureAwait(false);
// Delete the object
await RemoveObject.Run(minioClient, destBucketName, objectName).ConfigureAwait(false);
// Retry on failure
await RetryPolicyObject.Run(minioClient, destBucketName, objectName).ConfigureAwait(false);
// Tracing request with custom logger
await CustomRequestLogger.Run(minioClient).ConfigureAwait(false);
// Remove the buckets
await RemoveBucket.Run(minioClient, bucketName).ConfigureAwait(false);
await RemoveBucket.Run(minioClient, destBucketName).ConfigureAwait(false);
// Remove the binary files created for test
File.Delete(smallFileName);
File.Delete(bigFileName);
if (OperatingSystem.IsWindows()) _ = Console.ReadLine();
}
}