Skip to content

Commit

Permalink
Azure Blog Storage: Support for nested containers to upload files (#391)
Browse files Browse the repository at this point in the history
* Split containerName to get root and sub containers

* Update azure storage docs to point nested containers
  • Loading branch information
sa-es-ir authored Dec 23, 2024
1 parent f128449 commit aefd9d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/Media/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ The following settings in `appsettings.json` control media upload functionality:
| AuthenticationMode | string | Authentication method - either `Default` for Microsoft Entra ID or `ConnectionString` for connection string auth |
| ConnectionString | string | Azure Storage connection string (only used and mandatory when AuthenticationMode is `ConnectionString`) |
| ServiceUrl | string | Azure Blob Storage service URL (only used and mandatory when AuthenticationMode is `Default`) |
| ContainerName | string | Name of the Azure Storage container to store uploaded files. |
| ContainerName | string | Name of the Azure Storage container to store uploaded files. It can be nested containers as well ``path/to/upload`` |
| CdnEndpoint | string | Optional CDN endpoint to use for uploaded images. If set, the blog will return this URL instead of the storage account URL for uploaded assets. |

## Authentication Methods
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Azure.Identity;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Microsoft.Extensions.Options;
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace LinkDotNet.Blog.Web.Features.Services.FileUpload;

Expand All @@ -23,8 +24,10 @@ public async Task<string> UploadFileAsync(string fileName, Stream fileStream, Up

var containerName = azureBlobStorageConfiguration.Value.ContainerName;
var client = CreateClient(azureBlobStorageConfiguration.Value);
var blobContainerClient = client.GetBlobContainerClient(containerName);
var blobClient = blobContainerClient.GetBlobClient(fileName);

var (rootContainer, subContainer) = SplitContainerName(containerName);
var blobContainerClient = client.GetBlobContainerClient(rootContainer);
var blobClient = blobContainerClient.GetBlobClient($"{subContainer}/{fileName}");

var blobOptions = new BlobUploadOptions();
if (options.SetCacheControlHeader)
Expand All @@ -39,6 +42,18 @@ public async Task<string> UploadFileAsync(string fileName, Stream fileStream, Up
return GetAssetUrl(blobClient.Uri.ToString(), azureBlobStorageConfiguration.Value);
}

private static (string rootContainer, string subContainer) SplitContainerName(string containerName)
{
var containerNames = containerName.Split('/', StringSplitOptions.RemoveEmptyEntries);

if (containerNames.Length == 0)
return (string.Empty, string.Empty);

var rootContainer = containerNames[0];
var subContainer = string.Join("/", containerNames.Skip(1));
return (rootContainer, subContainer);
}

private static BlobServiceClient CreateClient(UploadConfiguration configuration)
{
if (configuration.AuthenticationMode == AuthenticationMode.ConnectionString.Key)
Expand Down

0 comments on commit aefd9d4

Please sign in to comment.