Wrapper for Azure Storage, designed to simplify the file upload process and provide links for downloading them. It also supports file deletion and enumeration.
$ dotnet add package AzureStorageWrapper
AzureStorageWrapper it's a wrapper for Azure Storage blob service, aimed at simplifying the file upload process and obtaining links for downloading them.
📦 View package on NuGet Gallery 📦 View package on nuget.info
To add AzureStorageWrapper to dependencies container, just use the method AddAzureStorageWrapper(...)
public void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddAzureStorageWrapper(configuration =>
{
configuration.ConnectionString = "azure-storage-connection-string"
configuration.MaxSasUriExpiration = 600;
configuration.DefaultSasUriExpiration = 300;
configuration.CreateContainerIfNotExists = true;
});
}
These are the main properties:
ExpiresIn property. By doing so, this value will be automatically set. Expressed in secondstrue or false based on your requirements. Please consider this property if you have automated your infrastructure with any Infrastructure as Code (IaC) mechanism because it affects the state of your infrastructure.Then you can inject IAzureStorageWrapper into your services through constructor:
public class MyService
{
private IAzureStorageWrapper _storageWrapper;
public MyService(IAzureStorageWrapper storageWrapper)
{
_storageWrapper = storageWrapper;
}
}There are 3 options to upload blobs, all the ways follow the same pattern:
The file will be placed in Base64, Bytes or Stream property.
var base64 = "SGVsbG8g8J+Zgg==";
var command = new UploadBase64()
{
Base64 = base64,
Container = "greetings",
Name = "greeting",
Extension = "md",
Metadata = new Dictionary<string, string>() { {"GREETING_PLACE", "Office"} }
};
var response = await _azureStorageWrapper.UploadBlobAsync(command);var bytes = Convert.FromBase64String("SGVsbG8g8J+Zgg==");
var command = new UploadBytes()
{
Bytes = bytes,
Container = "greetings",
Name = "greeting",
Extension = "md",
Metadata = new Dictionary<string, string>() { {"GREETING_PLACE", "Street"} }
};
var response = await _azureStorageWrapper.UploadBlobAsync(command);var stream = new MemoryStream(Convert.FromBase64String("SGVsbG8g8J+Zgg=="));
var command = new UploadStream()
{
Stream = stream,
Container = "greetings",
Name = "greeting",
Extension = "md",
Metadata = new Dictionary<string, string>() { {"GREETING_PLACE", "Park"} }
};
var response = await _azureStorageWrapper.UploadBlobAsync(command);Regardless of the chosen upload mechanism, you will always receive this response after upload a file.
public class BlobReference
{
public string Container { get; set; }
public string Name { get; set; }
public string Extension { get; set; }
public string Uri { get; set; }
public string SasUri { get; set; }
public DateTime SasExpires { get; set; }
public IDictionary<string, string> Metadata { get; set; }
}In example, if you upload the file greeting.md file to container greetings you will receive a response like:
{
"Container": "greetings",
"Name": "greeting",
"Extension": "md",
"Uri": "https://stgazstgwrapper001westeu.blob.core.windows.net/tests/5a19306fc5014a4/greeting.md",
"SasUri": "https://stgazstgwrapper001westeu.blob.core.windows.net/tests/5a19306fc5014a4/greeting.md?sv=2021-10-04\u0026se=2023-09-03T16%3A17%3A02Z\u0026sr=b\u0026sp=r\u0026sig=8hs8AzxABevSTc5y%2BhOWDDN%2FH5qFSpA8Omj4uqoxzms%3D",
"SasExpires": "2023-09-03T16:17:02.8220993Z",
"Metadata": {
"GREETING_PLACE": "Office",
"ASW_FOLDER": "5a19306fc5014a4",
"ASW_TIMESTAMP": "03/09/2023 16:11:02"
}
}It is your responsibility to save the reference (URI property) of the file you have uploaded to Azure Storage somewhere, as you will need it for later downloads.
To download a blob reference, you need specify the Uri.
The Folder it's mandatory.
var command = new DownloadBlobReference()
{
Uri = "https://stgazstgwrapper001westeu.blob.core.windows.net/tests/5a19306fc5014a4/greeting.md"
ExpiresIn = 60,
};
var response = await _azureStorageWrapper.DownloadBlobReferenceAsync(command);The response when downloading file reference resembles the response when uploading files:
{
"Container": "greetings",
"Name": "greeting",
"Extension": "md",
"Uri": "https://stgazstgwrapper001westeu.blob.core.windows.net/tests/5a19306fc5014a4/greeting.md",
"SasUri": "https://stgazstgwrapper001westeu.blob.core.windows.net/tests/5a19306fc5014a4/greeting.md?sv=2021-10-04\u0026se=2023-09-03T16%3A17%3A02Z\u0026sr=b\u0026sp=r\u0026sig=8hs8AzxABevSTc5y%2BhOWDDN%2FH5qFSpA8Omj4uqoxzms%3D",
"SasExpires": "2023-09-03T16:17:02.8220993Z",
"Metadata": {
"GREETING_PLACE": "Office",
"ASW_FOLDER": "5a19306fc5014a4",
"ASW_TIMESTAMP": "03/09/2023 16:11:02"
}
}If you like the project, you can consider making a donation at ko-fi.com
You can contact me via Twitter @sergiobarriel, or if you have an issue, you can open one 🙂