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

Added BlobFileStoreFactory to allow DefaultAzureCredential or Connectionstring authentication for Azure Blob storage #12874

Conversation

rikbosch
Copy link

@rikbosch rikbosch commented Nov 23, 2022

Fixes #12639

This PR introduces a BlobFileStoreFactory which is used by the OrchardCore.Shells.Azure and OrchardCore.Media.Azure module.

It allows to specify a reference to a configured BlobServiceClient through the Microsoft.Extensions.Azure nuget package.

see https://learn.microsoft.com/en-us/dotnet/azure/sdk/dependency-injection#configure-multiple-service-clients-with-different-names about configuring multiple blob clients and also: https://devblogs.microsoft.com/azure-sdk/best-practices-for-using-azure-sdk-with-asp-net-core/ for best-practices

for example:

program.cs

builder.Services.AddAzureClients(clientBuilder =>
{
    clientBuilder.AddBlobServiceClient(builder.Configuration.GetSection("PublicStorage"));
    clientBuilder.AddBlobServiceClient(builder.Configuration.GetSection("PrivateStorage"))
        .WithName("PrivateStorage");
});

appsettings.json

{
  "OrchardCore": {
    "OrchardCore_Shells_Azure": {
      "BlobServiceName": "PrivateStorage", //<<< ! this is where the magic happens
      "ContainerName": "a-container-name", 
      "BasePath": "shells"
    }
  }
}

This allows to use registered clients from Azure.Configuration.Extensions  (and DefaultAzureCredential )or
specify a connectionstring
@rikbosch rikbosch changed the title fixes #12639 added BlobFileStoreFactory to allow DefaultAzureCredential or Connectionstring authentication for Azure Blob storage Fixes #12639: Added BlobFileStoreFactory to allow DefaultAzureCredential or Connectionstring authentication for Azure Blob storage Nov 23, 2022
@agriffard agriffard changed the title Fixes #12639: Added BlobFileStoreFactory to allow DefaultAzureCredential or Connectionstring authentication for Azure Blob storage Added BlobFileStoreFactory to allow DefaultAzureCredential or Connectionstring authentication for Azure Blob storage Dec 9, 2022
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.Modules;

namespace OrchardCore.FileStorage.AzureBlob;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add new line after the namespace.

private readonly IAzureClientFactory<BlobServiceClient> _clientFactory;
private readonly IServiceProvider _serviceProvider;

private const string DefaultStorageName = "Default";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename the variable to _defaultStorageName


private BlobContainerClient CreateContainerClientFromOptions(BlobStorageOptions options)
{
// return a container client from connectionstring, if specified
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// return a container client from connectionstring, if specified
// Create a container using the specified connection string.

return new BlobContainerClient(options.ConnectionString, options.ContainerName);
}

// else use the clientFactory to lookup a BlobService with the specified name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// else use the clientFactory to lookup a BlobService with the specified name
// Create a container using the specified Blob service.

.GetBlobContainerClient(options.ContainerName);
}

// fall-back to the default registered blob storage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// fall-back to the default registered blob storage
// Create a container using the Default client.

@@ -24,6 +24,9 @@ public static OrchardCoreBuilder AddAzureShellsConfiguration(this OrchardCoreBui

services.TryAddSingleton<IContentTypeProvider, FileExtensionContentTypeProvider>();

// register the azure blob file storage services
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just remove this comment.

// always use TryXXX methods because this method can be called multiple times
// by different modules (currently: Azure Media and Azure Shells module)
services.TryAddSingleton<BlobFileStoreFactory>();
return services;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New line before return

Then either remove the comment or change the comment block to something like this

// TryAddSingleton is used to prevent registering the service again.

{
/// <summary>
/// Registers the BlobFileStorage services in the ServiceCollection.
/// Note: this method can be called multiple times
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove the comments here.

/// <returns></returns>
public static OrchardCoreBuilder AddAzureBlobFileStorage(this OrchardCoreBuilder builder)
{
builder.ApplicationServices.AddAzureBlobFileStorage();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line before return.

@@ -95,22 +95,22 @@ public override void ConfigureServices(IServiceCollection services)
services.AddSingleton<IMediaFileStoreCache>(serviceProvider =>
serviceProvider.GetRequiredService<IMediaFileStoreCacheFileProvider>());

// Register the BlobFileStorageFactory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment isn't needed here

@MikeAlhayek
Copy link
Member

Additionally, please

  1. Fix conflict.
  2. Update the appsettings.json file to add BlobServiceName in there.
  3. Update the docs file with the update setting variable.

@rjpowers10
Copy link
Contributor

It looks like the trail has gone cold on this. Just commenting to say that I'm looking for a way to use Azure Managed Identity for OrchardCore.Shells.Azure and OrchardCore.Media.Azure and this PR seems to be exactly what I'm looking for.

Copy link

This pull request has merge conflicts. Please resolve those before requesting a review.

@Piedone
Copy link
Member

Piedone commented Mar 14, 2024

Is this something you'd like to revisit any time soon @rikbosch or should we close?

@Piedone
Copy link
Member

Piedone commented Apr 8, 2024

Would you like to perhaps take over this, @mariojsnunes?

@mariojsnunes
Copy link
Contributor

Yes, I'll look into it towards the end of the week

@Piedone
Copy link
Member

Piedone commented Apr 15, 2024

Superseded by #15753.

@Piedone Piedone closed this Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to connect to Azure Blob storage with AAD authentication/managed identity
6 participants