Skip to content

Commit f48c7fb

Browse files
author
Vitaliy Basanets
committed
added createcontainer method
1 parent f29fc92 commit f48c7fb

5 files changed

Lines changed: 78 additions & 11 deletions

File tree

ManagedCode.Storage.Aws/AWSStorage.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,14 @@ private async Task UploadStreamInternalAsync(string blobName, Stream dataStream,
316316
}
317317

318318
#endregion
319+
320+
#region CreateContainer
321+
322+
public async Task CreateContainerAsync()
323+
{
324+
await _s3Client.EnsureBucketExistsAsync(_bucket);
325+
}
326+
327+
#endregion
328+
319329
}

ManagedCode.Storage.Azure/AzureStorage.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,24 @@ namespace ManagedCode.Storage.Azure;
1515

1616
public class AzureStorage : IAzureStorage
1717
{
18-
private readonly BlobContainerClient _blobContainerClient;
18+
private BlobContainerClient _blobContainerClient;
19+
private readonly AzureStorageOptions _options;
1920

2021
public AzureStorage(AzureStorageOptions options)
2122
{
23+
_options = options;
2224
_blobContainerClient = new BlobContainerClient(
2325
options.ConnectionString,
2426
options.Container,
2527
options.OriginalOptions
2628
);
2729

28-
if (options.ShouldCreateIfNotExists)
30+
_blobContainerClient.SetAccessPolicy(_options.PublicAccessType);
31+
32+
if (_options.ShouldCreateIfNotExists)
2933
{
3034
_blobContainerClient.CreateIfNotExists(PublicAccessType.BlobContainer);
3135
}
32-
33-
_blobContainerClient.SetAccessPolicy(options.PublicAccessType);
3436
}
3537

3638
public void Dispose()
@@ -246,4 +248,19 @@ public async Task<string> UploadAsync(Stream dataStream, CancellationToken cance
246248
}
247249

248250
#endregion
251+
252+
#region CreateContainer
253+
254+
public async Task CreateContainerAsync()
255+
{
256+
await _blobContainerClient.SetAccessPolicyAsync(_options.PublicAccessType);
257+
258+
if (_options.ShouldCreateIfNotExists)
259+
{
260+
await _blobContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer);
261+
}
262+
}
263+
264+
#endregion
265+
249266
}

ManagedCode.Storage.Core/IStorage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ public interface IStorage : IDisposable
3737
Task<bool> ExistsAsync(BlobMetadata blobMetadata, CancellationToken cancellationToken = default);
3838
IAsyncEnumerable<bool> ExistsAsync(IEnumerable<string> blobNames, CancellationToken cancellationToken = default);
3939
IAsyncEnumerable<bool> ExistsAsync(IEnumerable<BlobMetadata> blobs, CancellationToken cancellationToken = default);
40+
41+
Task CreateContainerAsync();
4042
}

ManagedCode.Storage.FileSystem/FileSystemStorage.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,19 @@ public async Task<string> UploadAsync(Stream dataStream, CancellationToken cance
256256
}
257257

258258
#endregion
259+
260+
#region CreateContainer
261+
262+
public async Task CreateContainerAsync()
263+
{
264+
await Task.Yield();
265+
266+
if (!Directory.Exists(_path))
267+
{
268+
Directory.CreateDirectory(_path);
269+
}
270+
}
271+
272+
#endregion
273+
259274
}

ManagedCode.Storage.Gcp/GCPStorage.cs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,32 @@ public class GCPStorage : IGCPStorage
1818
{
1919
private readonly string _bucket;
2020
private readonly StorageClient _storageClient;
21+
private readonly GCPStorageOptions _gcpStorageOptions;
2122

2223
public GCPStorage(GCPStorageOptions gcpStorageOptions)
2324
{
24-
_bucket = gcpStorageOptions.BucketOptions.Bucket;
25+
_gcpStorageOptions = gcpStorageOptions;
2526

26-
if (gcpStorageOptions.StorageClientBuilder != null)
27+
_bucket = _gcpStorageOptions.BucketOptions.Bucket;
28+
29+
if (_gcpStorageOptions.StorageClientBuilder != null)
2730
{
28-
_storageClient = gcpStorageOptions.StorageClientBuilder.Build();
31+
_storageClient = _gcpStorageOptions.StorageClientBuilder.Build();
2932
}
30-
else if (gcpStorageOptions.GoogleCredential != null)
33+
else if (_gcpStorageOptions.GoogleCredential != null)
3134
{
3235
_storageClient = StorageClient.Create(gcpStorageOptions.GoogleCredential);
3336
}
3437

3538
try
3639
{
37-
if (gcpStorageOptions.OriginalOptions != null)
40+
if (_gcpStorageOptions.OriginalOptions != null)
3841
{
39-
_storageClient.CreateBucket(gcpStorageOptions.BucketOptions.ProjectId, _bucket, gcpStorageOptions.OriginalOptions);
42+
_storageClient.CreateBucket(_gcpStorageOptions.BucketOptions.ProjectId, _bucket, _gcpStorageOptions.OriginalOptions);
4043
}
4144
else
4245
{
43-
_storageClient.CreateBucket(gcpStorageOptions.BucketOptions.ProjectId, _bucket);
46+
_storageClient.CreateBucket(_gcpStorageOptions.BucketOptions.ProjectId, _bucket);
4447
}
4548
}
4649
catch
@@ -263,4 +266,24 @@ private async Task UploadStreamInternalAsync(string blobName, Stream dataStream,
263266
}
264267

265268
#endregion
269+
270+
#region CreateContainer
271+
public async Task CreateContainerAsync()
272+
{
273+
try
274+
{
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+
}
283+
}
284+
catch
285+
{
286+
}
287+
}
288+
#endregion
266289
}

0 commit comments

Comments
 (0)