Implement azurite
This commit is contained in:
parent
eda79010ce
commit
626105cf0c
@ -22,6 +22,12 @@ namespace Core.Blueprint.Storage.Configuration
|
|||||||
{
|
{
|
||||||
if (environment == "Local")
|
if (environment == "Local")
|
||||||
{
|
{
|
||||||
|
var accountKey = configuration.GetSection("BlobStorage:AccountKey").Value;
|
||||||
|
var accountName = configuration.GetSection("BlobStorage:AccountName").Value;
|
||||||
|
|
||||||
|
if(string.IsNullOrEmpty(accountKey) && string.IsNullOrEmpty(accountName))
|
||||||
|
throw new ArgumentException("The BlobStorage configuration section is missing or empty.");
|
||||||
|
|
||||||
cfg.AddBlobServiceClient(configuration.GetConnectionString("BlobStorage"));
|
cfg.AddBlobServiceClient(configuration.GetConnectionString("BlobStorage"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|||||||
@ -162,7 +162,7 @@ namespace Core.Blueprint.Storage.Contracts
|
|||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="blobName"/> is null or empty.</exception>
|
/// <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>
|
/// <exception cref="StorageException">Thrown if there is an issue communicating with the Azure Blob service.</exception>
|
||||||
BlobDownloadUriAdapter GenerateBlobDownloadUri(string blobName);
|
ValueTask<BlobDownloadUriAdapter?> GenerateBlobDownloadUri(string blobName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Retrieves the hierarchical folder structure.
|
/// Retrieves the hierarchical folder structure.
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using Azure;
|
using Azure;
|
||||||
|
using Azure.Storage;
|
||||||
using Azure.Storage.Blobs;
|
using Azure.Storage.Blobs;
|
||||||
using Azure.Storage.Blobs.Models;
|
using Azure.Storage.Blobs.Models;
|
||||||
using Azure.Storage.Blobs.Specialized;
|
using Azure.Storage.Blobs.Specialized;
|
||||||
@ -6,6 +7,7 @@ using Azure.Storage.Sas;
|
|||||||
using Core.Blueprint.Storage.Adapters;
|
using Core.Blueprint.Storage.Adapters;
|
||||||
using Core.Blueprint.Storage.Contracts;
|
using Core.Blueprint.Storage.Contracts;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Core.Blueprint.Storage.Provider
|
namespace Core.Blueprint.Storage.Provider
|
||||||
{
|
{
|
||||||
@ -15,10 +17,12 @@ namespace Core.Blueprint.Storage.Provider
|
|||||||
private readonly BlobContainerClient _blobContainerClient;
|
private readonly BlobContainerClient _blobContainerClient;
|
||||||
private readonly string _containerName;
|
private readonly string _containerName;
|
||||||
private readonly Trie _trie = new Trie();
|
private readonly Trie _trie = new Trie();
|
||||||
|
private readonly IConfiguration _configuration;
|
||||||
|
|
||||||
public BlobStorageProvider(BlobServiceClient blobServiceClient, IConfiguration configuration)
|
public BlobStorageProvider(BlobServiceClient blobServiceClient, IConfiguration configuration)
|
||||||
{
|
{
|
||||||
_blobServiceClient = blobServiceClient;
|
_blobServiceClient = blobServiceClient;
|
||||||
|
_configuration = configuration;
|
||||||
_containerName = configuration.GetSection("BlobStorage:ContainerName").Value ?? "";
|
_containerName = configuration.GetSection("BlobStorage:ContainerName").Value ?? "";
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(_containerName))
|
if (string.IsNullOrEmpty(_containerName))
|
||||||
@ -278,7 +282,8 @@ namespace Core.Blueprint.Storage.Provider
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="blobName">The name of the blob for which the download URI is being generated.</param>
|
/// <param name="blobName">The name of the blob for which the download URI is being generated.</param>
|
||||||
/// <returns>
|
/// <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>
|
/// </returns>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// The generated URI includes a Shared Access Signature (SAS) token, which allows secure, time-limited access to the blob.
|
/// 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>
|
/// </remarks>
|
||||||
/// <exception cref="ArgumentNullException">Thrown if <paramref name="blobName"/> is null or empty.</exception>
|
/// <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>
|
/// <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,
|
if (string.IsNullOrWhiteSpace(blobName))
|
||||||
DateTimeOffset.UtcNow.AddHours(2));
|
throw new ArgumentNullException(nameof(blobName), "Blob name cannot be null or empty.");
|
||||||
|
|
||||||
var blob = _blobContainerClient.GetBlobClient(blobName);
|
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,
|
BlobContainerName = blob.BlobContainerName,
|
||||||
BlobName = blob.Name,
|
BlobName = blob.Name,
|
||||||
Resource = "b",
|
Resource = "b",
|
||||||
StartsOn = DateTimeOffset.UtcNow,
|
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;
|
sasBuilder.Protocol = SasProtocol.Https;
|
||||||
|
|
||||||
var blobUriBuilder = new BlobUriBuilder(blob.Uri)
|
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>
|
/// <summary>
|
||||||
/// Retrieves the hierarchical folder structure.
|
/// Retrieves the hierarchical folder structure.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user