AzureStorageWrapper 2.1.0
See the version list below for details.
dotnet add package AzureStorageWrapper --version 2.1.0
NuGet\Install-Package AzureStorageWrapper -Version 2.1.0
<PackageReference Include="AzureStorageWrapper" Version="2.1.0" />
paket add AzureStorageWrapper --version 2.1.0
#r "nuget: AzureStorageWrapper, 2.1.0"
// Install AzureStorageWrapper as a Cake Addin #addin nuget:?package=AzureStorageWrapper&version=2.1.0 // Install AzureStorageWrapper as a Cake Tool #tool nuget:?package=AzureStorageWrapper&version=2.1.0
AzureStorageWrapper
AzureStorageWrapper it's a wrapper for Azure Storage blob service, aimed at simplifying the file upload process and obtaining links for downloading them.
Usage
Dependency injection
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.DefaultSasUriExpiration = 360;
configuration.MaxSasUriExpiration = 360;
configuration.CreateContainerIfNotExists = true;
});
}
These are the main properties:
- ConnectionString: The connection string of your Azure Storage Account. You can export by following this document
- MaxSasUriExpiration: You can set a maximum duration value for the Shared Access Signature (SAS) of an Azure Storage file to prevent someone from attempting to generate a token with a longer expiration time.
- DefaultSasUriExpiration: You can download a file using AzureStorageWrapper without specifying the
ExpiresIn
property. By doing so, this value will be automatically set. - CreateContainerIfNotExists: When uploading a file to Azure Storage, you need to specify the container, which may not exist and can be created automatically. You can set it to
true
orfalse
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.
Upload blobs
There are 3 options to upload blobs, all the ways follow the same pattern:
- You need to specify the file name and extension.
- You need to specify the container where you want to store the file.
- You can add additional metadata with relevant information.
The file will be placed in Base64, Bytes or Stream property.
Base64
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);
Byte []
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);
Stream
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);
Response after upload blobs
Regardless of the chosen upload mechanism, you will always receive this response after upload a file.
public class BlobReference
{
public string Folder { get;set; }
public string Name { get; set; }
public string Extension { get; set; }
public string FullName { get; set; }
public string Container { 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",
"Folder": "5a19306fc5014a4",
"Name": "greeting",
"Extension": "md",
"FullName": "5a19306fc5014a4/greeting.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 references (folder, file name, and extension) of the file you have uploaded to Azure Storage somewhere, as you will need it for later downloads.
Download blobs
To download a blob reference, you need specify the container, the file name and extension.
The Folder it's mandatory.
var command = new DownloadBlobReference()
{
Container = "greetings",
Folder = "04bc4c89e547478",
Name = "greeting",
Extension = "md",
ExpiresIn = 360,
};
var response = await _azureStorageWrapper.DownloadBlobReferenceAsync(command);
The response when downloading file reference resembles the response when uploading files:
{
"Container": "greetings",
"Folder": "5a19306fc5014a4",
"Name": "greeting",
"Extension": "md",
"FullName": "5a19306fc5014a4/greeting.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"
}
}
With AzureStorageWrapper, you can download files that haven't been uploaded using this tool. You just need to make some adjustments when downloading. Simply modify the Folder parameter and set the file path as it exists in your container.
In example, if you have saved invoices inside a container named invoices and you have virtual folders like 2020/08 you can donwload as:
var command = new DownloadBlobReference()
{
Container = "invoices",
Folder = "2020/08",
Name = "file",
Extension = "pdf",
ExpiresIn = 360,
};
var response = await _azureStorageWrapper.DownloadBlobReferenceAsync(command);
Support
You can contact me via Twitter @sergiobarriel, or if you have an issue, you can open one 🙂
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Azure.Storage.Blobs (>= 12.14.1)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.3.1 | 135 | 7/10/2024 |
2.3.0 | 136 | 6/26/2024 |
2.2.15 | 82 | 6/25/2024 |
2.2.14 | 547 | 2/7/2024 |
2.2.13 | 105 | 2/7/2024 |
2.2.12 | 300 | 12/26/2023 |
2.2.11 | 250 | 12/21/2023 |
2.2.10 | 115 | 12/21/2023 |
2.2.8 | 140 | 12/17/2023 |
2.2.7 | 115 | 12/17/2023 |
2.2.6 | 173 | 11/20/2023 |
2.2.5 | 138 | 11/20/2023 |
2.2.4 | 136 | 10/11/2023 |
2.2.3 | 161 | 10/10/2023 |
2.2.2 | 122 | 10/10/2023 |
2.2.1 | 148 | 9/22/2023 |
2.2.0 | 142 | 9/11/2023 |
2.1.2 | 111 | 9/5/2023 |
2.1.1 | 121 | 9/4/2023 |
2.1.0 | 101 | 9/4/2023 |