@@ -50,16 +50,11 @@ public override async Task<IFile> GetFileAsync(string path, CancellationToken ca
50
50
throw new FileNotFoundException ( path , Prefix ) ;
51
51
}
52
52
53
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
54
- {
55
- throw new ConnectionException ( exception ) ;
56
- }
57
-
58
- throw new AdapterRuntimeException ( exception ) ;
53
+ throw Exception ( exception ) ;
59
54
}
60
55
catch ( Exception exception )
61
56
{
62
- throw new AdapterRuntimeException ( exception ) ;
57
+ throw Exception ( exception ) ;
63
58
}
64
59
}
65
60
@@ -100,27 +95,15 @@ public override async Task<IDirectory> GetDirectoryAsync(string path, Cancellati
100
95
101
96
throw new DirectoryNotFoundException ( path , Prefix ) ;
102
97
}
103
- catch ( FileSystemException )
104
- {
105
- throw ;
106
- }
107
- catch ( AmazonS3Exception exception )
108
- {
109
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
110
- {
111
- throw new ConnectionException ( exception ) ;
112
- }
113
-
114
- throw new AdapterRuntimeException ( exception ) ;
115
- }
116
98
catch ( Exception exception )
117
99
{
118
- throw new AdapterRuntimeException ( exception ) ;
100
+ throw Exception ( exception ) ;
119
101
}
120
102
}
121
103
122
104
public override async Task < IEnumerable < IFile > > GetFilesAsync ( string path = "" , CancellationToken cancellationToken = default )
123
105
{
106
+ await GetDirectoryAsync ( path , cancellationToken ) ;
124
107
path = PrependRootPath ( path ) ;
125
108
126
109
try
@@ -142,18 +125,9 @@ public override async Task<IEnumerable<IFile>> GetFilesAsync(string path = "", C
142
125
143
126
return files ;
144
127
}
145
- catch ( AmazonS3Exception exception )
146
- {
147
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
148
- {
149
- throw new ConnectionException ( exception ) ;
150
- }
151
-
152
- throw new AdapterRuntimeException ( exception ) ;
153
- }
154
128
catch ( Exception exception )
155
129
{
156
- throw new AdapterRuntimeException ( exception ) ;
130
+ throw Exception ( exception ) ;
157
131
}
158
132
}
159
133
@@ -162,6 +136,7 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(
162
136
CancellationToken cancellationToken = default
163
137
)
164
138
{
139
+ await GetDirectoryAsync ( path , cancellationToken ) ;
165
140
path = PrependRootPath ( path ) ;
166
141
167
142
try
@@ -183,56 +158,39 @@ public override async Task<IEnumerable<IDirectory>> GetDirectoriesAsync(
183
158
184
159
return directories ;
185
160
}
186
- catch ( AmazonS3Exception exception )
187
- {
188
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
189
- {
190
- throw new ConnectionException ( exception ) ;
191
- }
192
-
193
- throw new AdapterRuntimeException ( exception ) ;
194
- }
195
161
catch ( Exception exception )
196
162
{
197
- throw new AdapterRuntimeException ( exception ) ;
163
+ throw Exception ( exception ) ;
198
164
}
199
165
}
200
166
201
167
public override async Task CreateDirectoryAsync ( string path , CancellationToken cancellationToken = default )
202
168
{
203
- if ( ! path . EndsWith ( '/' ) )
169
+ if ( await DirectoryExistsAsync ( path , cancellationToken ) )
204
170
{
205
- path += "/" ;
171
+ throw new DirectoryExistsException ( PrependRootPath ( path ) , Prefix ) ;
206
172
}
207
173
174
+ path = PrependRootPath ( path ) ;
175
+ path = path . EndsWith ( '/' ) ? path : path + "/" ;
176
+
208
177
try
209
178
{
210
179
var request = new PutObjectRequest { BucketName = bucketName , Key = path , InputStream = new MemoryStream ( ) } ;
211
180
await client . PutObjectAsync ( request , cancellationToken ) ;
212
181
}
213
- catch ( AmazonS3Exception exception )
214
- {
215
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
216
- {
217
- throw new ConnectionException ( exception ) ;
218
- }
219
-
220
- throw new AdapterRuntimeException ( exception ) ;
221
- }
222
182
catch ( Exception exception )
223
183
{
224
- throw new AdapterRuntimeException ( exception ) ;
184
+ throw Exception ( exception ) ;
225
185
}
226
186
}
227
187
228
188
public override async Task DeleteDirectoryAsync ( string path , CancellationToken cancellationToken = default )
229
189
{
230
190
await GetDirectoryAsync ( path , cancellationToken ) ;
231
191
232
- if ( ! path . EndsWith ( '/' ) )
233
- {
234
- path += "/" ;
235
- }
192
+ path = PrependRootPath ( path ) ;
193
+ path = path . EndsWith ( '/' ) ? path : path + "/" ;
236
194
237
195
try
238
196
{
@@ -248,47 +206,31 @@ public override async Task DeleteDirectoryAsync(string path, CancellationToken c
248
206
249
207
await client . DeleteObjectsAsync ( deleteObjectsRequest , cancellationToken ) ;
250
208
}
251
- catch ( AmazonS3Exception exception )
252
- {
253
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
254
- {
255
- throw new ConnectionException ( exception ) ;
256
- }
257
-
258
- throw new AdapterRuntimeException ( exception ) ;
259
- }
260
209
catch ( Exception exception )
261
210
{
262
- throw new AdapterRuntimeException ( exception ) ;
211
+ throw Exception ( exception ) ;
263
212
}
264
213
}
265
214
266
215
public override async Task DeleteFileAsync ( string path , CancellationToken cancellationToken = default )
267
216
{
268
217
await GetFileAsync ( path , cancellationToken ) ;
218
+ path = PrependRootPath ( path ) ;
269
219
270
220
try
271
221
{
272
222
await client . DeleteObjectAsync ( bucketName , path , cancellationToken ) ;
273
223
}
274
- catch ( AmazonS3Exception exception )
275
- {
276
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
277
- {
278
- throw new ConnectionException ( exception ) ;
279
- }
280
-
281
- throw new AdapterRuntimeException ( exception ) ;
282
- }
283
224
catch ( Exception exception )
284
225
{
285
- throw new AdapterRuntimeException ( exception ) ;
226
+ throw Exception ( exception ) ;
286
227
}
287
228
}
288
229
289
230
public override async Task < byte [ ] > ReadFileAsync ( string path , CancellationToken cancellationToken = default )
290
231
{
291
232
await GetFileAsync ( path , cancellationToken ) ;
233
+ path = PrependRootPath ( path ) ;
292
234
293
235
try
294
236
{
@@ -299,24 +241,16 @@ public override async Task<byte[]> ReadFileAsync(string path, CancellationToken
299
241
300
242
return memoryStream . ToArray ( ) ;
301
243
}
302
- catch ( AmazonS3Exception exception )
303
- {
304
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
305
- {
306
- throw new ConnectionException ( exception ) ;
307
- }
308
-
309
- throw new AdapterRuntimeException ( exception ) ;
310
- }
311
244
catch ( Exception exception )
312
245
{
313
- throw new AdapterRuntimeException ( exception ) ;
246
+ throw Exception ( exception ) ;
314
247
}
315
248
}
316
249
317
250
public override async Task < string > ReadTextFileAsync ( string path , CancellationToken cancellationToken = default )
318
251
{
319
252
await GetFileAsync ( path , cancellationToken ) ;
253
+ path = PrependRootPath ( path ) ;
320
254
321
255
try
322
256
{
@@ -330,29 +264,26 @@ public override async Task<string> ReadTextFileAsync(string path, CancellationTo
330
264
331
265
return await streamReader . ReadToEndAsync ( ) ;
332
266
}
333
- catch ( AmazonS3Exception exception )
334
- {
335
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
336
- {
337
- throw new ConnectionException ( exception ) ;
338
- }
339
-
340
- throw new AdapterRuntimeException ( exception ) ;
341
- }
342
267
catch ( Exception exception )
343
268
{
344
- throw new AdapterRuntimeException ( exception ) ;
269
+ throw Exception ( exception ) ;
345
270
}
346
271
}
347
272
348
- public override async Task WriteFileAsync ( string path , byte [ ] contents , bool overwrite = false ,
349
- CancellationToken cancellationToken = default )
273
+ public override async Task WriteFileAsync (
274
+ string path ,
275
+ byte [ ] contents ,
276
+ bool overwrite = false ,
277
+ CancellationToken cancellationToken = default
278
+ )
350
279
{
351
280
if ( ! overwrite && await FileExistsAsync ( path , cancellationToken ) )
352
281
{
353
282
throw new FileExistsException ( PrependRootPath ( path ) , Prefix ) ;
354
283
}
355
284
285
+ path = PrependRootPath ( path ) ;
286
+
356
287
try
357
288
{
358
289
await using var memoryStream = new MemoryStream ( contents ) ;
@@ -365,18 +296,9 @@ public override async Task WriteFileAsync(string path, byte[] contents, bool ove
365
296
366
297
await client . PutObjectAsync ( request , cancellationToken ) ;
367
298
}
368
- catch ( AmazonS3Exception exception )
369
- {
370
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
371
- {
372
- throw new ConnectionException ( exception ) ;
373
- }
374
-
375
- throw new AdapterRuntimeException ( exception ) ;
376
- }
377
299
catch ( Exception exception )
378
300
{
379
- throw new AdapterRuntimeException ( exception ) ;
301
+ throw Exception ( exception ) ;
380
302
}
381
303
}
382
304
@@ -387,6 +309,8 @@ public override async Task AppendFileAsync(string path, byte[] contents, Cancell
387
309
contents = existingContents . Concat ( contents ) . ToArray ( ) ;
388
310
await DeleteFileAsync ( path , cancellationToken ) ;
389
311
312
+ path = PrependRootPath ( path ) ;
313
+
390
314
try
391
315
{
392
316
await using var memoryStream = new MemoryStream ( contents ) ;
@@ -399,19 +323,28 @@ public override async Task AppendFileAsync(string path, byte[] contents, Cancell
399
323
400
324
await client . PutObjectAsync ( request , cancellationToken ) ;
401
325
}
402
- catch ( AmazonS3Exception exception )
326
+ catch ( Exception exception )
403
327
{
404
- if ( exception . ErrorCode == "InvalidAccessKeyId" || exception . ErrorCode == "InvalidSecurity" )
405
- {
406
- throw new ConnectionException ( exception ) ;
407
- }
408
-
409
- throw new AdapterRuntimeException ( exception ) ;
328
+ throw Exception ( exception ) ;
410
329
}
411
- catch ( Exception exception )
330
+ }
331
+
332
+ private static Exception Exception ( Exception exception )
333
+ {
334
+ if ( exception is FileSystemException )
412
335
{
413
- throw new AdapterRuntimeException ( exception ) ;
336
+ return exception ;
414
337
}
338
+
339
+ if ( exception is AmazonS3Exception amazonS3Exception )
340
+ {
341
+ if ( amazonS3Exception . ErrorCode == "InvalidAccessKeyId" || amazonS3Exception . ErrorCode == "InvalidSecurity" )
342
+ {
343
+ return new ConnectionException ( exception ) ;
344
+ }
345
+ }
346
+
347
+ return new AdapterRuntimeException ( exception ) ;
415
348
}
416
349
}
417
350
}
0 commit comments