1
- using Amazon ;
1
+ using Amazon ;
2
2
using Amazon . Runtime ;
3
3
using Amazon . S3 ;
4
4
using Microsoft . Extensions . Configuration ;
@@ -8,49 +8,40 @@ namespace Storage.Benchmark.Utils;
8
8
9
9
internal static class BenchmarkHelper
10
10
{
11
- public static readonly byte [ ] StreamBuffer = new byte [ 2048 ] ;
12
-
13
- // ReSharper disable once InconsistentNaming
14
- public static AmazonS3Client CreateAWSClient ( S3Settings settings )
11
+ public static readonly byte [ ] StreamBuffer = new byte [ 2048 ] ;
12
+
13
+ // ReSharper disable once InconsistentNaming
14
+ public static AmazonS3Client CreateAWSClient ( S3BucketSettings settings )
15
15
{
16
- var scheme = settings . UseHttps ? Uri . UriSchemeHttps : Uri . UriSchemeHttp ;
17
- var port = settings . Port . HasValue ? $ ":{ settings . Port } " : string . Empty ;
18
-
19
16
return new AmazonS3Client (
20
17
new BasicAWSCredentials ( settings . AccessKey , settings . SecretKey ) ,
21
18
new AmazonS3Config
22
19
{
23
20
RegionEndpoint = RegionEndpoint . USEast1 ,
24
- ServiceURL = $ " { scheme } :// { settings . EndPoint } { port } " ,
25
- ForcePathStyle = true , // MUST be true to work correctly with MinIO server
21
+ ServiceURL = settings . Endpoint ,
22
+ ForcePathStyle = true , // MUST be true to work correctly with MinIO server
26
23
} ) ;
27
24
}
28
25
29
- public static IMinioClient CreateMinioClient ( S3Settings settings )
26
+ public static IMinioClient CreateMinioClient ( S3BucketSettings settings )
30
27
{
31
- var builder = new MinioClient ( ) ;
32
- var port = settings . Port ;
33
- if ( port . HasValue )
34
- {
35
- builder . WithEndpoint ( settings . EndPoint , port . Value ) ;
36
- }
37
- else
38
- {
39
- builder . WithEndpoint ( settings . EndPoint ) ;
40
- }
28
+ Uri endpoint = new ( settings . Endpoint ) ;
41
29
30
+ var builder = new MinioClient ( ) ;
31
+
42
32
return builder
33
+ . WithEndpoint ( endpoint )
43
34
. WithCredentials ( settings . AccessKey , settings . SecretKey )
44
- . WithSSL ( settings . UseHttps )
35
+
45
36
. Build ( ) ;
46
37
}
47
38
48
- public static S3Client CreateStoragesClient ( S3Settings settings )
39
+ public static S3BucketClient CreateStoragesClient ( S3BucketSettings settings )
49
40
{
50
- return new S3Client ( settings ) ;
41
+ return new S3BucketClient ( new HttpClient ( ) , settings ) ;
51
42
}
52
43
53
- public static void EnsureBucketExists ( S3Client client , CancellationToken cancellation )
44
+ public static void EnsureBucketExists ( S3BucketClient client , CancellationToken cancellation )
54
45
{
55
46
if ( client . IsBucketExists ( cancellation ) . GetAwaiter ( ) . GetResult ( ) )
56
47
{
@@ -62,7 +53,7 @@ public static void EnsureBucketExists(S3Client client, CancellationToken cancell
62
53
63
54
public static void EnsureFileExists (
64
55
IConfiguration config ,
65
- S3Client client ,
56
+ S3BucketClient client ,
66
57
string fileName ,
67
58
CancellationToken cancellation )
68
59
{
@@ -76,7 +67,7 @@ public static void EnsureFileExists(
76
67
77
68
if ( ! result )
78
69
{
79
- throw new Exception ( "File isn't uploaded" ) ;
70
+ throw new FileLoadException ( "File isn't uploaded" ) ;
80
71
}
81
72
}
82
73
@@ -86,7 +77,7 @@ public static byte[] ReadBigArray(IConfiguration config)
86
77
87
78
return ! string . IsNullOrEmpty ( filePath ) && File . Exists ( filePath )
88
79
? File . ReadAllBytes ( filePath )
89
- : GetByteArray ( 123 * 1024 * 1024 ) ; // 123 Mb
80
+ : GetByteArray ( 123 * 1024 * 1024 ) ; // 123 Mb
90
81
}
91
82
92
83
public static InputStream ReadBigFile ( IConfiguration config )
@@ -114,28 +105,28 @@ public static async Task<int> ReadStreamMock(Stream input, byte[] buffer, Cancel
114
105
return result ;
115
106
}
116
107
117
- public static S3Settings ReadSettings ( IConfiguration config )
108
+ public static S3BucketSettings ReadSettings ( IConfiguration config )
118
109
{
119
- var settings = config . GetRequiredSection ( "S3Storage" ) . Get < S3Settings > ( ) ;
120
- if ( settings == null || string . IsNullOrEmpty ( settings . EndPoint ) )
110
+ var settings = config . GetRequiredSection ( "S3Storage" ) . Get < S3BucketSettings > ( ) ;
111
+ if ( settings == null || string . IsNullOrEmpty ( settings . Endpoint ) )
121
112
{
122
- throw new Exception ( "S3Storage configuration is not found" ) ;
113
+ throw new UriFormatException ( "S3Storage configuration is not found" ) ;
123
114
}
124
115
125
116
var isContainer = Environment . GetEnvironmentVariable ( "DOTNET_RUNNING_IN_CONTAINER" ) ;
126
117
if ( isContainer != null && bool . TryParse ( isContainer , out var value ) && value )
127
118
{
128
- settings = new S3Settings
119
+ Uri endpointUri = new ( settings . Endpoint ) ;
120
+
121
+ settings = new S3BucketSettings
129
122
{
130
123
AccessKey = settings . AccessKey ,
131
124
Bucket = settings . Bucket ,
132
- EndPoint = "host.docker.internal" ,
133
- Port = settings . Port ,
125
+ Endpoint = $ "{ endpointUri . Scheme } ://host.docker.internal:{ endpointUri . Port } ",
134
126
Region = settings . Region ,
135
127
SecretKey = settings . SecretKey ,
136
128
Service = settings . Service ,
137
129
UseHttp2 = settings . UseHttp2 ,
138
- UseHttps = settings . UseHttps ,
139
130
} ;
140
131
}
141
132
0 commit comments