|
|
|
@ -1,3 +1,148 @@ |
|
|
|
// See https://aka.ms/new-console-template for more information |
|
|
|
|
|
|
|
Console.WriteLine("Hello, World!"); |
|
|
|
using System.Security.AccessControl; |
|
|
|
|
|
|
|
using Minio; |
|
|
|
|
|
|
|
using Minio.DataModel; |
|
|
|
|
|
|
|
using Minio.Exceptions; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace n4_minio_demo |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
class Program |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
static void Main(string[] args) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var endpoint = "storage.nube4.cloud"; |
|
|
|
|
|
|
|
var endpointPort = 9443; |
|
|
|
|
|
|
|
var accessKey = "Wudugi31qqyHTCYPp7ek"; |
|
|
|
|
|
|
|
var secretKey = "UoujxNjCkd1XEqpf1RsP4oxWgFox9IV9zBmOPv38"; |
|
|
|
|
|
|
|
var secure = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the client with access credentials... |
|
|
|
|
|
|
|
MinioClient minio = new MinioClient() |
|
|
|
|
|
|
|
.WithEndpoint(endpoint, endpointPort) |
|
|
|
|
|
|
|
.WithCredentials(accessKey, secretKey) |
|
|
|
|
|
|
|
.WithSSL(secure) |
|
|
|
|
|
|
|
.Build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get the bucket list... |
|
|
|
|
|
|
|
Program.GetBucketList(minio).Wait(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Upload file using a preffix to set a tag vaue... |
|
|
|
|
|
|
|
Program.UploadFile(minio).Wait(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Search for a file uploaded using a preffix... |
|
|
|
|
|
|
|
Program.ListFile(minio); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Download file... |
|
|
|
|
|
|
|
Program.DownloadFile(minio).Wait(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async static Task GetBucketList(MinioClient minio) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Console.WriteLine("Listing buckets..."); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Create an async tsk for listing buckets... |
|
|
|
|
|
|
|
var getListBucketsTask = await minio.ListBucketsAsync().ConfigureAwait(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Iterate over the list of buckets... |
|
|
|
|
|
|
|
foreach (var bucket in getListBucketsTask.Buckets) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Console.WriteLine(bucket.Name + " " + bucket.CreationDateDateTime); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async static Task UploadFile(MinioClient minio) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Console.WriteLine("Upload file..."); |
|
|
|
|
|
|
|
var fileId = "a44d2f66-5c18-11ee-8c99-0242ac120002"; |
|
|
|
|
|
|
|
var bucketName = "test"; |
|
|
|
|
|
|
|
var objectName = fileId + "/axeso-site.pdf"; |
|
|
|
|
|
|
|
var filePath = "/Users/mabcastillo/Downloads/axeso-site.pdf"; |
|
|
|
|
|
|
|
var contentType = "application/pdf"; |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Upload a file to bucket... |
|
|
|
|
|
|
|
var putObjectArgs = new PutObjectArgs() |
|
|
|
|
|
|
|
.WithBucket(bucketName) |
|
|
|
|
|
|
|
.WithObject(objectName) |
|
|
|
|
|
|
|
.WithFileName(filePath) |
|
|
|
|
|
|
|
.WithContentType(contentType); |
|
|
|
|
|
|
|
await minio.PutObjectAsync(putObjectArgs).ConfigureAwait(false); |
|
|
|
|
|
|
|
Console.WriteLine("Successfully uploaded " + objectName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (MinioException e) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Console.WriteLine("File Upload Error: {0}", e.Message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Item GetObjectByPrefix(MinioClient minio, string bucketName, string prefix) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// Some value initialization... |
|
|
|
|
|
|
|
Item selectedItem = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var listArgs = new ListObjectsArgs() |
|
|
|
|
|
|
|
.WithBucket(bucketName) |
|
|
|
|
|
|
|
.WithPrefix(prefix) |
|
|
|
|
|
|
|
.WithRecursive(true); |
|
|
|
|
|
|
|
var observable = minio.ListObjectsAsync(listArgs); |
|
|
|
|
|
|
|
var subscription = observable.Subscribe( |
|
|
|
|
|
|
|
item => selectedItem = item, |
|
|
|
|
|
|
|
ex => Console.WriteLine("OnError: {0}", ex.Message), |
|
|
|
|
|
|
|
() => Console.WriteLine("End listing objects")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(true) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (selectedItem != null) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return selectedItem; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
System.Threading.Thread.Sleep(200); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception e) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Console.WriteLine("GetObjectByPrefix error {0}", e.Message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void ListFile(MinioClient minio) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Console.WriteLine("List objects..."); |
|
|
|
|
|
|
|
var fileId = "a44d2f66-5c18-11ee-8c99-0242ac120002"; |
|
|
|
|
|
|
|
var bucketName = "test"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Item selectedItem = GetObjectByPrefix(minio, bucketName, fileId); |
|
|
|
|
|
|
|
Console.WriteLine("Item found {0} {1}", selectedItem.Key, selectedItem.LastModifiedDateTime); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async static Task DownloadFile(MinioClient minio) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
var bucketName = "test"; |
|
|
|
|
|
|
|
var objectName = "a44d2f66-5c18-11ee-8c99-0242ac120002/axeso-site.pdf"; |
|
|
|
|
|
|
|
var fileName = "/Users/mabcastillo/Downloads/axeso-site-download.pdf"; |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Console.WriteLine("Download file..."); |
|
|
|
|
|
|
|
var args = new GetObjectArgs() |
|
|
|
|
|
|
|
.WithBucket(bucketName) |
|
|
|
|
|
|
|
.WithObject(objectName) |
|
|
|
|
|
|
|
.WithFile(fileName); |
|
|
|
|
|
|
|
var stat = await minio.GetObjectAsync(args).ConfigureAwait(false); |
|
|
|
|
|
|
|
Console.WriteLine("Downloaded the file {0} in bucket {1}", fileName, bucketName); |
|
|
|
|
|
|
|
Console.WriteLine("Stat details of object {0} in bucket {1} {2}", objectName, bucketName, stat); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception e) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
Console.WriteLine("Download File Error {0}", e.Message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|