Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure Blog Storage: Support for nested containers to upload files #391

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading