Skip to content

Commit deabf59

Browse files
vitalik23Ruslan DudchenkoVitaliy Basanets
authored
Change upload method (#16)
* changed upload method * added createcontainer test * fixed tests Co-authored-by: Ruslan Dudchenko <rusland@managed-code.com> Co-authored-by: Vitaliy Basanets <vitaliyb@managed-code.com>
1 parent 8942b82 commit deabf59

File tree

6 files changed

+107
-56
lines changed

6 files changed

+107
-56
lines changed

ManagedCode.Storage.Aws/AWSStorage.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,24 +306,30 @@ private async Task UploadStreamInternalAsync(string blobName, Stream dataStream,
306306
BucketName = _bucket,
307307
Key = blobName,
308308
InputStream = dataStream,
309-
AutoCloseStream = true,
309+
AutoCloseStream = false,
310310
ContentType = contentType ?? Constants.ContentType,
311311
ServerSideEncryptionMethod = null
312312
};
313313

314-
await _s3Client.EnsureBucketExistsAsync(_bucket);
315-
await _s3Client.PutObjectAsync(putRequest, cancellationToken);
314+
try
315+
{
316+
await _s3Client.PutObjectAsync(putRequest, cancellationToken);
317+
}
318+
catch (AmazonS3Exception)
319+
{
320+
await CreateContainerAsync();
321+
await _s3Client.PutObjectAsync(putRequest, cancellationToken);
322+
}
316323
}
317324

318325
#endregion
319326

320327
#region CreateContainer
321328

322-
public async Task CreateContainerAsync()
329+
public async Task CreateContainerAsync(CancellationToken cancellationToken = default)
323330
{
324331
await _s3Client.EnsureBucketExistsAsync(_bucket);
325332
}
326333

327334
#endregion
328-
329335
}

ManagedCode.Storage.Azure/AzureStorage.cs

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Azure;
89
using Azure.Storage.Blobs;
910
using Azure.Storage.Blobs.Models;
1011
using ManagedCode.Storage.Azure.Options;
@@ -26,14 +27,6 @@ public AzureStorage(AzureStorageOptions options)
2627
options.Container,
2728
options.OriginalOptions
2829
);
29-
30-
31-
if (_options.ShouldCreateIfNotExists)
32-
{
33-
_blobContainerClient.CreateIfNotExists(PublicAccessType.BlobContainer);
34-
}
35-
36-
_blobContainerClient.SetAccessPolicy(_options.PublicAccessType);
3730
}
3831

3932
public void Dispose()
@@ -188,13 +181,31 @@ public async IAsyncEnumerable<BlobMetadata> GetBlobListAsync(CancellationToken c
188181
public async Task UploadStreamAsync(string blobName, Stream dataStream, CancellationToken cancellationToken = default)
189182
{
190183
var blobClient = _blobContainerClient.GetBlobClient(blobName);
191-
await blobClient.UploadAsync(dataStream, cancellationToken);
184+
185+
try
186+
{
187+
await blobClient.UploadAsync(dataStream, cancellationToken);
188+
}
189+
catch (RequestFailedException)
190+
{
191+
await CreateContainerAsync();
192+
await blobClient.UploadAsync(dataStream, cancellationToken);
193+
}
192194
}
193195

194196
public async Task UploadAsync(string blobName, string content, CancellationToken cancellationToken = default)
195197
{
196198
var blobClient = _blobContainerClient.GetBlobClient(blobName);
197-
await blobClient.UploadAsync(BinaryData.FromString(content), cancellationToken);
199+
200+
try
201+
{
202+
await blobClient.UploadAsync(BinaryData.FromString(content), cancellationToken);
203+
}
204+
catch (RequestFailedException)
205+
{
206+
await CreateContainerAsync();
207+
await blobClient.UploadAsync(BinaryData.FromString(content), cancellationToken);
208+
}
198209
}
199210

200211
public async Task UploadFileAsync(string blobName, string pathToFile, CancellationToken cancellationToken = default)
@@ -203,7 +214,15 @@ public async Task UploadFileAsync(string blobName, string pathToFile, Cancellati
203214

204215
using (var fs = new FileStream(pathToFile, FileMode.Open, FileAccess.Read))
205216
{
206-
await blobClient.UploadAsync(fs, cancellationToken);
217+
try
218+
{
219+
await blobClient.UploadAsync(fs, cancellationToken);
220+
}
221+
catch (RequestFailedException)
222+
{
223+
await CreateContainerAsync();
224+
await blobClient.UploadAsync(fs, cancellationToken);
225+
}
207226
}
208227
}
209228

@@ -225,25 +244,51 @@ public async Task UploadAsync(BlobMetadata blobMetadata, string content, Cancell
225244
public async Task UploadAsync(BlobMetadata blobMetadata, byte[] data, CancellationToken cancellationToken = default)
226245
{
227246
var blobClient = _blobContainerClient.GetBlobClient(blobMetadata.Name);
228-
await blobClient.UploadAsync(BinaryData.FromBytes(data), cancellationToken);
247+
248+
try
249+
{
250+
await blobClient.UploadAsync(BinaryData.FromBytes(data), cancellationToken);
251+
}
252+
catch (RequestFailedException)
253+
{
254+
await CreateContainerAsync();
255+
await blobClient.UploadAsync(BinaryData.FromBytes(data), cancellationToken);
256+
}
229257
}
230258

231259
public async Task<string> UploadAsync(string content, CancellationToken cancellationToken = default)
232260
{
233261
string fileName = $"{Guid.NewGuid().ToString("N").ToLowerInvariant()}.txt";
234262

235263
var blobClient = _blobContainerClient.GetBlobClient(fileName);
236-
await blobClient.UploadAsync(BinaryData.FromString(content), cancellationToken);
237264

265+
try
266+
{
267+
await blobClient.UploadAsync(BinaryData.FromString(content), cancellationToken);
268+
}
269+
catch (RequestFailedException)
270+
{
271+
await CreateContainerAsync();
272+
await blobClient.UploadAsync(BinaryData.FromString(content), cancellationToken);
273+
}
274+
238275
return fileName;
239276
}
240277

241278
public async Task<string> UploadAsync(Stream dataStream, CancellationToken cancellationToken = default)
242279
{
243-
string fileName = Guid.NewGuid().ToString("N").ToLowerInvariant();
244-
280+
var fileName = Guid.NewGuid().ToString("N").ToLowerInvariant();
245281
var blobClient = _blobContainerClient.GetBlobClient(fileName);
246-
await blobClient.UploadAsync(dataStream, cancellationToken);
282+
283+
try
284+
{
285+
await blobClient.UploadAsync(dataStream, cancellationToken);
286+
}
287+
catch (RequestFailedException)
288+
{
289+
await CreateContainerAsync();
290+
await blobClient.UploadAsync(dataStream, cancellationToken);
291+
}
247292

248293
return fileName;
249294
}
@@ -252,17 +297,15 @@ public async Task<string> UploadAsync(Stream dataStream, CancellationToken cance
252297

253298
#region CreateContainer
254299

255-
public async Task CreateContainerAsync()
300+
public async Task CreateContainerAsync(CancellationToken cancellationToken = default)
256301
{
257-
258302
if (_options.ShouldCreateIfNotExists)
259303
{
260-
await _blobContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer);
304+
await _blobContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer, cancellationToken: cancellationToken);
261305
}
262306

263307
await _blobContainerClient.SetAccessPolicyAsync(_options.PublicAccessType);
264308
}
265309

266310
#endregion
267-
268311
}

ManagedCode.Storage.Core/IStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ public interface IStorage : IDisposable
3838
IAsyncEnumerable<bool> ExistsAsync(IEnumerable<string> blobNames, CancellationToken cancellationToken = default);
3939
IAsyncEnumerable<bool> ExistsAsync(IEnumerable<BlobMetadata> blobs, CancellationToken cancellationToken = default);
4040

41-
Task CreateContainerAsync();
41+
Task CreateContainerAsync(CancellationToken cancellationToken = default);
4242
}

ManagedCode.Storage.FileSystem/FileSystemStorage.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public async Task<string> UploadAsync(Stream dataStream, CancellationToken cance
257257

258258
#region CreateContainer
259259

260-
public async Task CreateContainerAsync()
260+
public async Task CreateContainerAsync(CancellationToken cancellationToken = default)
261261
{
262262
await Task.Yield();
263263

@@ -268,5 +268,4 @@ public async Task CreateContainerAsync()
268268
}
269269

270270
#endregion
271-
272271
}

ManagedCode.Storage.Gcp/GCPStorage.cs

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ public GCPStorage(GCPStorageOptions gcpStorageOptions)
3434
{
3535
_storageClient = StorageClient.Create(gcpStorageOptions.GoogleCredential);
3636
}
37-
38-
try
39-
{
40-
if (_gcpStorageOptions.OriginalOptions != null)
41-
{
42-
_storageClient.CreateBucket(_gcpStorageOptions.BucketOptions.ProjectId, _bucket, _gcpStorageOptions.OriginalOptions);
43-
}
44-
else
45-
{
46-
_storageClient.CreateBucket(_gcpStorageOptions.BucketOptions.ProjectId, _bucket);
47-
}
48-
}
49-
catch
50-
{
51-
}
5237
}
5338

5439
public void Dispose()
@@ -262,28 +247,33 @@ private async Task UploadStreamInternalAsync(string blobName, Stream dataStream,
262247
CancellationToken cancellationToken = default)
263248
{
264249
contentType ??= Constants.ContentType;
265-
await _storageClient.UploadObjectAsync(_bucket, blobName, contentType, dataStream, null, cancellationToken);
250+
251+
try
252+
{
253+
await _storageClient.UploadObjectAsync(_bucket, blobName, contentType, dataStream, null, cancellationToken);
254+
}
255+
catch
256+
{
257+
await CreateContainerAsync();
258+
await _storageClient.UploadObjectAsync(_bucket, blobName, contentType, dataStream, null, cancellationToken);
259+
}
266260
}
267261

268262
#endregion
269263

270264
#region CreateContainer
271-
public async Task CreateContainerAsync()
265+
266+
public async Task CreateContainerAsync(CancellationToken cancellationToken = default)
272267
{
273-
try
268+
if (_gcpStorageOptions.OriginalOptions != null)
274269
{
275-
if (_gcpStorageOptions.OriginalOptions != null)
276-
{
277-
await _storageClient.CreateBucketAsync(_gcpStorageOptions.BucketOptions.ProjectId, _bucket, _gcpStorageOptions.OriginalOptions);
278-
}
279-
else
280-
{
281-
await _storageClient.CreateBucketAsync(_gcpStorageOptions.BucketOptions.ProjectId, _bucket);
282-
}
270+
await _storageClient.CreateBucketAsync(_gcpStorageOptions.BucketOptions.ProjectId, _bucket, _gcpStorageOptions.OriginalOptions, cancellationToken);
283271
}
284-
catch
272+
else
285273
{
274+
await _storageClient.CreateBucketAsync(_gcpStorageOptions.BucketOptions.ProjectId, _bucket, cancellationToken: cancellationToken);
286275
}
287276
}
277+
288278
#endregion
289279
}

ManagedCode.Storage.Tests/StorageBaseTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
@@ -602,6 +603,18 @@ public async Task ExistFileByListBlobMetadataAsync()
602603

603604
#endregion
604605

606+
#region CreateContainer
607+
608+
[Fact]
609+
public async Task CreateContainerAsync()
610+
{
611+
await FluentActions.Awaiting(() => Storage.CreateContainerAsync())
612+
.Should().NotThrowAsync<Exception>();
613+
}
614+
615+
#endregion
616+
617+
605618
private async Task PrepareFileToTest(string content, string fileName)
606619
{
607620
if (await Storage.ExistsAsync(fileName))

0 commit comments

Comments
 (0)