forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes OrchardCMS#12639 added BlobFileStoreFactory
This allows to use registered clients from Azure.Configuration.Extensions (and DefaultAzureCredential )or specify a connectionstring
- Loading branch information
Showing
8 changed files
with
116 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...ore/OrchardCore.FileStorage.AzureBlob/AzureBlobFileStorageOrchardCoreBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace OrchardCore.FileStorage.AzureBlob; | ||
public static class AzureBlobFileStorageOrchardCoreBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// This registers the AzureBlobFileStorage components. | ||
/// Note: this method is safe to call more then once. | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <returns></returns> | ||
public static OrchardCoreBuilder AddAzureBlobFileStorage(this OrchardCoreBuilder builder) | ||
{ | ||
builder.ConfigureServices(services => services.AddAzureBlobFileStorage()); | ||
|
||
return builder; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...Core/OrchardCore.FileStorage.AzureBlob/AzureBlobFileStorageServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
|
||
namespace OrchardCore.FileStorage.AzureBlob; | ||
|
||
public static class AzureBlobFileStorageServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Registers the BlobFileStorage services in the ServiceCollection. | ||
/// Note: this method can be called multiple times | ||
/// </summary> | ||
/// <param name="services"></param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddAzureBlobFileStorage(this IServiceCollection services) | ||
{ | ||
// 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/OrchardCore/OrchardCore.FileStorage.AzureBlob/BlobFileStoreFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System; | ||
using Azure.Storage.Blobs; | ||
using Microsoft.AspNetCore.StaticFiles; | ||
using Microsoft.Extensions.Azure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OrchardCore.Modules; | ||
|
||
namespace OrchardCore.FileStorage.AzureBlob; | ||
public class BlobFileStoreFactory | ||
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
private readonly IAzureClientFactory<BlobServiceClient> _clientFactory; | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public BlobFileStoreFactory(IAzureClientFactory<BlobServiceClient> clientFactory, IServiceProvider serviceProvider) | ||
{ | ||
_clientFactory = clientFactory; | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a <see cref="BlobFileStore"/> from <see cref="BlobStorageOptions"/> | ||
/// If a connectionstring is specified, it will use the connectionstring | ||
/// otherwise it will try to get the BlobServiceClient configured with the specified name in the BlobServiceName property | ||
/// </summary> | ||
/// <param name="options"></param> | ||
/// <returns></returns> | ||
public virtual BlobFileStore Create(BlobStorageOptions options) | ||
{ | ||
var containerClient = CreateContainerClientFromOptions(options); | ||
|
||
return new BlobFileStore( | ||
containerClient, | ||
options.BasePath, | ||
_serviceProvider.GetRequiredService<IClock>(), | ||
_serviceProvider.GetRequiredService<IContentTypeProvider>()); | ||
} | ||
|
||
|
||
private BlobContainerClient CreateContainerClientFromOptions(BlobStorageOptions options) | ||
{ | ||
if (!String.IsNullOrEmpty(options.ConnectionString)) | ||
{ | ||
return new BlobContainerClient(options.ConnectionString, options.ContainerName); | ||
} | ||
|
||
var serviceClient = _clientFactory.CreateClient(options.BlobServiceName); | ||
return serviceClient.GetBlobContainerClient(options.ContainerName); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Would it make sense for this to be an interface so that it can be replaced? I see that the Create method is virtual, but using an interface makes fewer assumptions and is cleaner.
P.S. Good work!