Create Blob


The architecture of Azure Blob Storage reflects key principles of cloud-native design. The service is built to handle massive amounts of data, partitioning it across multiple servers to ensure efficient storage and retrieval. The seamless integration of Azure Blob Storage with other Azure services, such as Azure Functions and Azure Logic Apps, further enhances its capabilities.

Register the service:

builder.Services.AddSingleton<IBlobStorageService, BlobStorageService>();
builder.Services.AddSingleton(b => new 
BlobServiceClient(builder.Configuration["AzureBlobStorageConnectionString"
]));

The connection string (this should be from Key Vault, but for your testing...):

"AzureBlobStorageConnectionString": 
"DefaultEndpointsProtocol=https;AccountName=XYZ;AccountKey=
XYZ;EndpointSuffix=core.windows.net",

The Service and the implementation:

using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using GeneralAPI.Utilities;
namespace GeneralAPI.Services.Storages;
public interface IBlobStorageService {
  Task < BlobDownloadInfo > GetAsync(string blobName, string containerName);
  Task < IEnumerable < string >> GetListAsync(string containerName);
  Task UploadFileAsync(string path, string blobName, string containerName);
  Task UploadContentAsync(MemoryStream memoryStream, string blobName,
    string containerName);
  Task DeleteAsync(string blobName, string containerName);
}
public class BlobStorageService: IBlobStorageService {
  private readonly BlobServiceClient _blobServiceClient;
  public BlobStorageService(BlobServiceClient blobServiceClient) {
    _blobServiceClient = blobServiceClient;
  } 

    /// <summary>
  ///  Download blob using DownloadAsync. A RequestFailedException will be thrown if a failure occurs.
  /// </summary>
  /// <param name="blobName">The blob name</param>
  /// <param name="containerName">The container name</param>
  /// <returns>BlobDownloadInfo</returns>
  public async Task < BlobDownloadInfo > GetAsync(string blobName, string containerName) {
    var containerClient =
      _blobServiceClient.GetBlobContainerClient(containerName);
    var blobClient = containerClient.GetBlobClient(blobName);
    return await blobClient.DownloadAsync();
  } 

    /// <summary>
  /// Get a list with all blobs in the container. A RequestFailedException will be thrown if a failure occurs.
  /// </summary>
  /// <param name="containerName">Container name</param>
  /// <returns>IEnumerable of strings</returns>
  public async Task < IEnumerable < string >> GetListAsync(string containerName) {
    var containerClient =
      _blobServiceClient.GetBlobContainerClient(containerName);
    var result = new List < string > ();
    await foreach(var blob in containerClient.GetBlobsAsync()) {
      result.Add(blob.Name);
    }
    return result;
  } 

    /// <summary>
  ///  Content type is extracted from the path. A RequestFailedException will be thrown if a failure occurs.
  /// </summary>
  /// <param name="path">Path to file including extension</param>
  /// <param name="blobName">Name to be saved in blob storage</param>
  /// <param name="containerName">Container name, does not set a 
  default < /param>
  public async Task UploadFileAsync(string path, string blobName, string containerName) {
    var containerClient =
      _blobServiceClient.GetBlobContainerClient(containerName);
    var blobClient = containerClient.GetBlobClient(blobName);
    await blobClient.UploadAsync(path, new BlobHttpHeaders {
      ContentType = path.GetContentType()
    });
  } 

   /// <summary>
  ///  Content type from blob name. A RequestFailedException will be thrown if a failure occurs.
  /// </summary>
  /// <param name="memoryStream">The memory stream to upload</param>
  /// <param name="blobName">Name to be save as, should have the 
  extension < /param>
  /// <param name="containerName">Container name, does not set a 
  default < /param>
  public async Task UploadContentAsync(MemoryStream memoryStream, string blobName, string containerName) {
    var containerClient =
      _blobServiceClient.GetBlobContainerClient(containerName);
    var blobClient = containerClient.GetBlobClient(blobName);
    await blobClient.UploadAsync(memoryStream, new BlobHttpHeaders {
      ContentType = blobName.GetContentType()
    });
  } 

    /// <summary>
  ///  A RequestFailedException will be thrown if a failure occurs.
  /// </summary>
  /// <param name="blobName">Blob name as it is on storage</param>
  /// <param name="containerName">Container name</param>
  public async Task DeleteAsync(string blobName, string containerName) {
    var containerClient =
      _blobServiceClient.GetBlobContainerClient(containerName);
    var blobClient = containerClient.GetBlobClient(blobName);
    await blobClient.DeleteIfExistsAsync();
  }
}

Usage:

[HttpGet("{blobName}, {containerName}")]
public async Task < IActionResult > GetSingleBlob(string blobName, string containerName = "general") {
  try {
    var blobDownload = await _blobStorageService.GetAsync(blobName,
      containerName);
    _telemetryClient.TrackTrace("Log something here"); // pretend you didn't see it!
    return File(blobDownload.Content, blobDownload.ContentType);
  } catch (RequestFailedException rfe) {
    _telemetryClient.TrackException(rfe);
    return Problem(statusCode: rfe.Status, detail: rfe.Message,
      instance: "BlobStorageApi.GetSingleBlob");
  } catch (Exception e) {
    _telemetryClient.TrackException(e);
    return Problem(statusCode:
      StatusCodes.Status500InternalServerError, detail: e.Message, instance:
      "BlobStorageApi.GetSingleBlob");
  }
}

 


No files yet, migration hasn't completed yet!