Implement azurite
This commit is contained in:
		| @@ -1,4 +1,5 @@ | ||||
| using Azure; | ||||
| using Azure.Storage; | ||||
| using Azure.Storage.Blobs; | ||||
| using Azure.Storage.Blobs.Models; | ||||
| using Azure.Storage.Blobs.Specialized; | ||||
| @@ -6,6 +7,7 @@ using Azure.Storage.Sas; | ||||
| using Core.Blueprint.Storage.Adapters; | ||||
| using Core.Blueprint.Storage.Contracts; | ||||
| using Microsoft.Extensions.Configuration; | ||||
| using System.Threading.Tasks; | ||||
|  | ||||
| namespace Core.Blueprint.Storage.Provider | ||||
| { | ||||
| @@ -15,10 +17,12 @@ namespace Core.Blueprint.Storage.Provider | ||||
|         private readonly BlobContainerClient _blobContainerClient; | ||||
|         private readonly string _containerName; | ||||
|         private readonly Trie _trie = new Trie(); | ||||
|         private readonly IConfiguration _configuration; | ||||
|  | ||||
|         public BlobStorageProvider(BlobServiceClient blobServiceClient, IConfiguration configuration) | ||||
|         { | ||||
|             _blobServiceClient = blobServiceClient; | ||||
|             _configuration = configuration; | ||||
|             _containerName = configuration.GetSection("BlobStorage:ContainerName").Value ?? ""; | ||||
|  | ||||
|             if (string.IsNullOrEmpty(_containerName)) | ||||
| @@ -278,7 +282,8 @@ namespace Core.Blueprint.Storage.Provider | ||||
|         /// </summary> | ||||
|         /// <param name="blobName">The name of the blob for which the download URI is being generated.</param> | ||||
|         /// <returns> | ||||
|         /// An instance of <see cref="BlobDownloadUriAdapter"/> containing the generated URI, blob name, and status. | ||||
|         /// An instance of <see cref="BlobDownloadUriAdapter"/> containing the generated URI, blob name, and status, | ||||
|         /// or <c>null</c> if the blob does not exist. | ||||
|         /// </returns> | ||||
|         /// <remarks> | ||||
|         /// The generated URI includes a Shared Access Signature (SAS) token, which allows secure, time-limited access to the blob. | ||||
| @@ -286,22 +291,36 @@ namespace Core.Blueprint.Storage.Provider | ||||
|         /// </remarks> | ||||
|         /// <exception cref="ArgumentNullException">Thrown if <paramref name="blobName"/> is null or empty.</exception> | ||||
|         /// <exception cref="StorageException">Thrown if there is an issue communicating with the Azure Blob service.</exception> | ||||
|         public BlobDownloadUriAdapter GenerateBlobDownloadUri(string blobName) | ||||
|         public async ValueTask<BlobDownloadUriAdapter?> GenerateBlobDownloadUri(string blobName) | ||||
|         { | ||||
|             var delegationKey = _blobServiceClient.GetUserDelegationKey(DateTimeOffset.UtcNow, | ||||
|                                                                     DateTimeOffset.UtcNow.AddHours(2)); | ||||
|             if (string.IsNullOrWhiteSpace(blobName)) | ||||
|                 throw new ArgumentNullException(nameof(blobName), "Blob name cannot be null or empty."); | ||||
|  | ||||
|             var blob = _blobContainerClient.GetBlobClient(blobName); | ||||
|  | ||||
|             var sasBuilder = new BlobSasBuilder() | ||||
|             if (!await blob.ExistsAsync()) | ||||
|                 return null; | ||||
|  | ||||
|             var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? string.Empty; | ||||
|  | ||||
|             if (environment == "Local") | ||||
|             { | ||||
|                 return GenerateDownloadUri(blob); | ||||
|             } | ||||
|  | ||||
|             var delegationKey = await _blobServiceClient.GetUserDelegationKeyAsync( | ||||
|                 DateTimeOffset.UtcNow, | ||||
|                 DateTimeOffset.UtcNow.AddHours(2)); | ||||
|  | ||||
|             var sasBuilder = new BlobSasBuilder | ||||
|             { | ||||
|                 BlobContainerName = blob.BlobContainerName, | ||||
|                 BlobName = blob.Name, | ||||
|                 Resource = "b", | ||||
|                 StartsOn = DateTimeOffset.UtcNow, | ||||
|                 ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(5), | ||||
|                 ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(5) | ||||
|             }; | ||||
|             sasBuilder.SetPermissions(BlobAccountSasPermissions.Read); | ||||
|             sasBuilder.SetPermissions(BlobSasPermissions.Read); | ||||
|             sasBuilder.Protocol = SasProtocol.Https; | ||||
|  | ||||
|             var blobUriBuilder = new BlobUriBuilder(blob.Uri) | ||||
| @@ -317,6 +336,45 @@ namespace Core.Blueprint.Storage.Provider | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Generates a download URI for a blob using a Shared Access Signature in local (Azurite) environment. | ||||
|         /// </summary> | ||||
|         /// <param name="blob">The blob client for which the URI is being generated.</param> | ||||
|         /// <returns>An instance of <see cref="BlobDownloadUriAdapter"/> containing the SAS URI and metadata.</returns> | ||||
|         private BlobDownloadUriAdapter GenerateDownloadUri(BlobClient blob) | ||||
|         { | ||||
|             var sasBuilder = new BlobSasBuilder | ||||
|             { | ||||
|                 BlobContainerName = blob.BlobContainerName, | ||||
|                 BlobName = blob.Name, | ||||
|                 Resource = "b", | ||||
|                 StartsOn = DateTimeOffset.UtcNow, | ||||
|                 ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(5) | ||||
|             }; | ||||
|             sasBuilder.SetPermissions(BlobSasPermissions.Read); | ||||
|             sasBuilder.Protocol = SasProtocol.HttpsAndHttp; | ||||
|  | ||||
|             var accountName = _configuration["BlobStorage:AccountName"]; | ||||
|             var accountKey = _configuration["BlobStorage:AccountKey"]; | ||||
|  | ||||
|             var storageCredentials = new StorageSharedKeyCredential(accountName, accountKey); | ||||
|             var sasToken = sasBuilder.ToSasQueryParameters(storageCredentials); | ||||
|  | ||||
|             var blobUriBuilder = new BlobUriBuilder(blob.Uri) | ||||
|             { | ||||
|                 Sas = sasToken | ||||
|             }; | ||||
|  | ||||
|             return new BlobDownloadUriAdapter | ||||
|             { | ||||
|                 Uri = blobUriBuilder.ToUri(), | ||||
|                 Name = blob.Name, | ||||
|                 Status = "Available" | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Retrieves the hierarchical folder structure. | ||||
|         /// </summary> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user