Cosmos.BlobService
9.0.1
dotnet add package Cosmos.BlobService --version 9.0.1
NuGet\Install-Package Cosmos.BlobService -Version 9.0.1
<PackageReference Include="Cosmos.BlobService" Version="9.0.1" />
paket add Cosmos.BlobService --version 9.0.1
#r "nuget: Cosmos.BlobService, 9.0.1"
// Install Cosmos.BlobService as a Cake Addin #addin nuget:?package=Cosmos.BlobService&version=9.0.1 // Install Cosmos.BlobService as a Cake Tool #tool nuget:?package=Cosmos.BlobService&version=9.0.1
Azure Blob Storage Context
This is a an Azure Blob Storage context used by Cosmos CMS and can be used by any .Net Core. project.
It is a simple wrapper around the Azure Blob Storage SDK that provides a more convenient way to interact with the Azure Blob Storage.
Installation
How to install:
From the Nuget Package Manager Console:
Install-Package Cosmos.BlobService
From the .Net CLI:
dotnet add package Cosmos.BlobService
Usage
How to create the context:
In the program secrets, add the connection string to the Azure Blob Storage account like this:
{
"ConnectionStrings": {
"AzureBlobStorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=[YOUR-ACCOUNT-NAME];AccountKey=[YOUR-ACCOUNT-KEY]"
}
}
If you are using a managed identity--and not an account key--define the connection like this:
{
"ConnectionStrings": {
"AzureBlobStorageConnectionString": "DefaultEndpointsProtocol=https;AccountName=[YOUR-ACCOUNT-NAME];AccountKey=AccessToken
}
}
Notice the account key is set to "AccessToken". This is how the Azure Blob Storage context knows to use the managed identity.
Using the context in a .Net Core project
NOTE: For a full example of this package in use, please see the Cosmos CMS project file manager controller.
Meanhile, here is an example of how to use the context in a .Net Core project.
In the Startup.cs or Project.cs file, add this using statement:
using Cosmos.BlobService;
And then add this line to the ConfigureServices method:
// Add the BLOB and File Storage contexts for Cosmos CMS
builder.Services.AddCosmosStorageContext(builder.Configuration);
How to use the context:
In the class where you want to use the Azure Blob Storage context, inject the context like this:
namespace Cosmos.Publisher.Controllers
{
/// <summary>
/// Secure file access controller and proxy.
/// </summary>
[AllowAnonymous]
public class PubController : ControllerBase
{
private readonly StorageContext storageContext;
/// <summary>
/// Initializes a new instance of the <see cref="PubController"/> class.
/// </summary>
/// <param name="options">Cosmos options.</param>
/// <param name="dbContext">Database context.</param>
/// <param name="storageContext">Storage context.</param>
public PubController(StorageContext storageContext)
{
this.storageContext = storageContext;
}
}
}
Here is example code for reading a file from Azure Blob Storage:
/// <summary>
/// Gets a file and validates user authentication.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public virtual async Task<IActionResult> Index(string path)
{
var client = storageContext.GetAppendBlobClient(path);
var properties = await client.GetPropertiesAsync();
return File(fileStream: await client.OpenReadAsync(), contentType: properties.Value.ContentType, lastModified: properties.Value.LastModified, entityTag: new EntityTagHeaderValue(properties.Value.ETag.ToString()));
}
Then you can use the context to upload an image file Azure Blob Storage like this:
/// <summary>
/// Simple image upload.
/// </summary>
public async Task<IActionResult> SimpleUpload(string directory, string entityType = "articles", string editorType = "ckeditor")
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// Gets the file being uploaded.
var file = Request.Form.Files.FirstOrDefault();
if (file.Length > (1048576 * 25))
{
return Json(ReturnSimpleErrorMessage("The image upload failed because the image was too big (max 25MB)."));
}
var extension = Path.GetExtension(file.FileName).ToLower();
var fileName = $"{Guid.NewGuid().ToString().ToLower()}{extension}";
var image = await Image.LoadAsync(file.OpenReadStream());
string relativePath = UrlEncode(directory + fileName);
var contentType = MimeTypeMap.GetMimeType(Path.GetExtension(fileName));
var metaData = new FileUploadMetaData()
{
ChunkIndex = 0,
ContentType = contentType,
FileName = fileName,
RelativePath = relativePath,
TotalChunks = 1,
TotalFileSize = file.Length,
UploadUid = Guid.NewGuid().ToString(),
ImageHeight = image.Height.ToString(),
ImageWidth = image.Width.ToString(),
};
// Read the file into a memory stream.
using var memoryStream = new MemoryStream();
await file.CopyToAsync(memoryStream);
// Append the blob to the storage.
storageContext.AppendBlob(memoryStream, metaData);
return Json(JsonConvert.DeserializeObject<dynamic>("{\"url\": \"" + "/" + relativePath + "\"}"));
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net9.0
- Azure.Identity (>= 1.13.2)
- Azure.Storage.Blobs (>= 12.23.0)
- Azure.Storage.Files.Shares (>= 12.21.0)
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Options (>= 9.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Cosmos.BlobService:
Package | Downloads |
---|---|
Cosmos.Common
This package contains all the common methods and objects used by the Cosmos CMS editor website, and by any website service the role of a publishing website. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
9.0.1 | 94 | 1/20/2025 |
9.0.0.5 | 149 | 1/15/2025 |
8.1.0.1 | 80 | 1/7/2025 |
8.0.11.31 | 259 | 11/21/2024 |
8.0.10.28 | 166 | 11/14/2024 |
8.0.10.26 | 116 | 11/13/2024 |
8.0.10.24 | 124 | 11/7/2024 |
8.0.10.20 | 114 | 11/1/2024 |
8.0.10.18 | 151 | 10/29/2024 |
8.0.10.12 | 143 | 10/25/2024 |
8.0.10.4 | 107 | 10/16/2024 |
8.0.8.1 | 115 | 10/2/2024 |
8.0.7.7 | 112 | 10/1/2024 |
8.0.6.6 | 125 | 9/7/2024 |
8.0.6.2 | 116 | 8/27/2024 |
8.0.4.13 | 149 | 4/18/2024 |
8.0.4.12 | 130 | 3/29/2024 |
8.0.3.1 | 198 | 1/5/2024 |
8.0.0.3 | 154 | 12/18/2023 |
7.4.7.20 | 275 | 3/12/2023 |
7.2.2 | 649 | 11/18/2022 |
7.2.1 | 353 | 11/17/2022 |
7.1.1 | 435 | 11/1/2022 |
7.0.1 | 422 | 10/20/2022 |
6.3.15 | 413 | 10/20/2022 |