| | 1 | | using Azure; |
| | 2 | | using Azure.Storage.Blobs; |
| | 3 | | using Azure.Storage.Blobs.Models; |
| | 4 | | using MRA.Infrastructure.Settings; |
| | 5 | | using SharpCompress.Compressors.Xz; |
| | 6 | | using System.Data.Common; |
| | 7 | | using System.IO; |
| | 8 | |
|
| | 9 | | namespace MRA.Infrastructure.Storage.Connection; |
| | 10 | |
|
| | 11 | | public class AzureStorageConnection : IAzureStorageConnection |
| | 12 | | { |
| | 13 | | private readonly string _connectionString; |
| | 14 | | private BlobServiceClient? _blobServiceClient; |
| | 15 | | private BlobServiceClient BlobServiceClient |
| | 16 | | { |
| | 17 | | get |
| | 18 | | { |
| 0 | 19 | | if (_blobServiceClient is null) |
| 0 | 20 | | _blobServiceClient = new BlobServiceClient(_connectionString); |
| | 21 | |
|
| 0 | 22 | | return _blobServiceClient; |
| | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| 1 | 26 | | public AzureStorageConnection(AppSettings config) |
| | 27 | | { |
| 1 | 28 | | _connectionString = config.AzureStorage.ConnectionString; |
| 1 | 29 | | } |
| | 30 | |
|
| 0 | 31 | | public BlobContainerClient GetContainer(string containerName) => BlobServiceClient.GetBlobContainerClient(containerN |
| | 32 | |
|
| 0 | 33 | | public AsyncPageable<BlobItem> GetListBlobsAsync(string containerName) => GetContainer(containerName).GetBlobsAsync( |
| | 34 | |
|
| 0 | 35 | | public BlobClient GetBlob(string containerName, string blobPath) => GetContainer(containerName).GetBlobClient(blobPa |
| | 36 | |
|
| 0 | 37 | | public async Task<bool> BlobExists(string containerName, string blobPath) => await GetBlob(containerName, blobPath). |
| | 38 | |
|
| | 39 | | public async Task<BlobContentInfo> UploadAsync(string containerName, string blobPath, Stream stream) |
| 0 | 40 | | => await GetBlob(containerName, blobPath).UploadAsync(stream, overwrite: true); |
| | 41 | |
|
| | 42 | | public async Task<BlobContentInfo> UploadImageAsync(string containerName, string blobPath, MemoryStream memoryStream |
| | 43 | | { |
| 0 | 44 | | return await GetBlob(containerName, blobPath).UploadAsync(memoryStream, new BlobUploadOptions() |
| 0 | 45 | | { |
| 0 | 46 | | HttpHeaders = new BlobHttpHeaders() |
| 0 | 47 | | { |
| 0 | 48 | | ContentType = "image/png" |
| 0 | 49 | | } |
| 0 | 50 | | }); |
| 0 | 51 | | } |
| | 52 | | } |