/
Program.cs
138 lines (112 loc) · 5.52 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
using System;
using Amazon.S3;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3.Model;
using Amazon.S3.Util;
namespace demo
{
class Program
{
private const string accessKey = "E2R228B3YF1TF6IYBZJ1";
private const string secretKey = "clsTQb6EtomIA6kZH54Xv0mrOnRjLyRCCNTTqiWr"; // do not store secret key hardcoded in your production source code!
private const string endpointURL = "http://172.16.70.31:7480";
static void Main(string[] args)
{
// Task.Run(MainAsync).GetAwaiter().GetResult();
Task.Run(DeleteBucketAsync).GetAwaiter().GetResult();
}
private static async Task MainAsync()
{
var config = new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.USEast1, // MUST set this before setting ServiceURL and it should match the `MINIO_REGION` environment variable.
ServiceURL = endpointURL, // replace http://localhost:9000 with URL of your MinIO server
ForcePathStyle = true // MUST be true to work correctly with MinIO server
};
var amazonS3Client = new AmazonS3Client(accessKey, secretKey, config);
// uncomment the following line if you like to troubleshoot communication with S3 storage and implement private void OnAmazonS3Exception(object sender, Amazon.Runtime.ExceptionEventArgs e)
// amazonS3Client.ExceptionEvent += OnAmazonS3Exception;
var listBucketResponse = await amazonS3Client.ListBucketsAsync();
foreach (var bucket in listBucketResponse.Buckets)
{
Console.Out.WriteLine("bucket '" + bucket.BucketName + "' created at " + bucket.CreationDate);
}
if (listBucketResponse.Buckets.Count > 0)
{
var bucketName = listBucketResponse.Buckets[0].BucketName;
var listObjectsResponse = await amazonS3Client.ListObjectsAsync(bucketName);
foreach (var obj in listObjectsResponse.S3Objects)
{
Console.Out.WriteLine("key = '" + obj.Key + "' | size = " + obj.Size + " | tags = '" + obj.ETag + "' | modified = " + obj.LastModified);
}
}
if (listBucketResponse.Buckets.Count > 1)
{
var bucketName = listBucketResponse.Buckets[1].BucketName;
var listObjectsResponse = await amazonS3Client.ListObjectsAsync(bucketName);
foreach (var obj in listObjectsResponse.S3Objects)
{
Console.Out.WriteLine("key = '" + obj.Key + "' | size = " + obj.Size + " | tags = '" + obj.ETag + "' | modified = " + obj.LastModified);
}
}
}
private const string bucketName = "new-bucket-82b006cc";
private static async Task CreateBucketAsync()
{
var config = new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.USEast1, // MUST set this before setting ServiceURL and it should match the `MINIO_REGION` environment variable.
ServiceURL = endpointURL, // replace http://localhost:9000 with URL of your MinIO server
ForcePathStyle = true // MUST be true to work correctly with MinIO server
};
var amazonS3Client = new AmazonS3Client(accessKey, secretKey, config);
try
{
var putBucketRequest = new PutBucketRequest
{
BucketName = bucketName,
UseClientRegion = true
};
PutBucketResponse putBucketResponse = await amazonS3Client.PutBucketAsync(putBucketRequest);
Console.Out.WriteLine("Status code = '" + putBucketResponse.HttpStatusCode);
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
private static async Task DeleteBucketAsync()
{
var config = new AmazonS3Config
{
RegionEndpoint = RegionEndpoint.USEast1, // MUST set this before setting ServiceURL and it should match the `MINIO_REGION` environment variable.
ServiceURL = endpointURL, // replace http://localhost:9000 with URL of your MinIO server
ForcePathStyle = true // MUST be true to work correctly with MinIO server
};
var amazonS3Client = new AmazonS3Client(accessKey, secretKey, config);
try
{
var deleteBucketRequest = new DeleteBucketRequest
{
BucketName = bucketName,
UseClientRegion = true
};
DeleteBucketResponse deleteBucketResponse = await amazonS3Client.DeleteBucketAsync(bucketName);
Console.Out.WriteLine("Status code = '" + deleteBucketResponse.HttpStatusCode);
}
catch (AmazonS3Exception e)
{
Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
}
}
}
}