Logs for blob storage and some improvements

This commit is contained in:
Dominic Villemure
2024-07-09 23:45:51 -04:00
parent 349999722b
commit 86c9fb51fa
11 changed files with 133 additions and 69 deletions

View File

@@ -1,22 +1,27 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Azure;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Hutopy.Application.Common.Interfaces;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace Hutopy.Infrastructure.AzureBlob;
public class AzureBlobStorageService : IAzureBlobStorageService
{
private readonly BlobServiceClient _blobServiceClient;
private readonly ILogger<AzureBlobStorageService> _logger;
private readonly long _maxUploadSize = 10 * 1024 * 1024; // 10 MB in bytes
public AzureBlobStorageService(IConfiguration configuration)
public AzureBlobStorageService(IConfiguration configuration, ILogger<AzureBlobStorageService> logger)
{
_logger = logger;
var connectionString = configuration["Azure-Blob-Connection-String"] ?? "";
_blobServiceClient = new BlobServiceClient(connectionString);
}
/// <summary>
@@ -29,29 +34,65 @@ public class AzureBlobStorageService : IAzureBlobStorageService
/// <returns></returns>
public async Task<string> UploadFileAsync(string containerName, string blobName, Stream fileStream, string contentType)
{
// Get a reference to a container
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
// Create the container if it does not exist
await containerClient.CreateIfNotExistsAsync();
// Read the file stream into a memory stream to determine the length
// WATCH FOR MEMORY USAGE USING THE MEMORY STREAM.
var memoryStream = new MemoryStream();
await fileStream.CopyToAsync(memoryStream);
memoryStream.Position = 0;
// Get a reference to a blob
var blobClient = containerClient.GetBlobClient(blobName);
// Define the BlobHttpHeaders to include the content type
var blobHttpHeaders = new BlobHttpHeaders
// Check if the file size exceeds the maximum upload size
if (memoryStream.Length > _maxUploadSize)
{
ContentType = contentType
};
_logger.LogInformation($"Blob storage: File size exceeds the maximum allowed size of {_maxUploadSize} bytes.");
throw new InvalidOperationException($"Blob storage: File size exceeds the maximum allowed size of {_maxUploadSize} bytes.");
}
try
{
// Get a reference to a container
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
// Upload the file
await blobClient.UploadAsync(fileStream, new BlobUploadOptions
// Create the container if it does not exist
await containerClient.CreateIfNotExistsAsync();
// Get a reference to a blob
var blobClient = containerClient.GetBlobClient(blobName);
// Define the BlobHttpHeaders to include the content type
var blobHttpHeaders = new BlobHttpHeaders
{
ContentType = contentType
};
// Upload the file
var response = await blobClient.UploadAsync(memoryStream, new BlobUploadOptions
{
HttpHeaders = blobHttpHeaders
});
var fileUri = blobClient.Uri.ToString();
_logger.LogInformation(
$"Blob storage: Status [ {response.GetRawResponse().Status.ToString()} ] " +
$"Uploaded [ {blobName} ] to the container [ {containerName} ] " +
$"with contentType [ {contentType} ] " +
$"with a length of [ {memoryStream.Length} bytes ]" +
$"with the uri [ {fileUri} ]"
);
// Return the URI of the uploaded blob
return fileUri;
}
catch (RequestFailedException ex)
{
HttpHeaders = blobHttpHeaders
});
// Return the URI of the uploaded blob
return blobClient.Uri.ToString();
_logger.LogError($"Blob storage: Azure Storage request failed: {ex.Message}");
throw new Exception("Error uploading file to Azure Blob Storage", ex);
}
catch (Exception ex)
{
_logger.LogError($"Blob storage: An error occurred: {ex.Message}");
throw;
}
}
/// <summary>
@@ -62,14 +103,14 @@ public class AzureBlobStorageService : IAzureBlobStorageService
/// <returns></returns>
public async Task<MemoryStream> DownloadFileAsync(string containerName, string blobName)
{
// Get a reference to a container
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
// Get a reference to a blob
var blobClient = containerClient.GetBlobClient(blobName);
try
{
// Get a reference to a container
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
// Get a reference to a blob
var blobClient = containerClient.GetBlobClient(blobName);
// Download the blob to a stream
BlobDownloadInfo download = await blobClient.DownloadAsync();
MemoryStream memoryStream = new();
@@ -78,9 +119,14 @@ public class AzureBlobStorageService : IAzureBlobStorageService
return memoryStream;
}
catch (RequestFailedException ex)
{
_logger.LogError($"Azure Storage request failed: {ex.Message}");
throw new Exception("Error downloading file from Azure Blob Storage", ex);
}
catch (Exception ex)
{
// Log and handle the exception as needed
_logger.LogError($"An error occurred: {ex.Message}");
throw new ApplicationException("Error downloading file from Azure Blob Storage.", ex);
}
}