Skip to content

Commit f409cbc

Browse files
Add AzureBlobOptions and manually validate (#1)
1 parent 6e1b812 commit f409cbc

File tree

4 files changed

+83
-32
lines changed

4 files changed

+83
-32
lines changed

Umbraco.Cloud.StorageProviders.AzureBlob.sln

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 16
44
VisualStudioVersion = 16.0.30114.105
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{EEC243CE-EF50-4E06-8B76-ACFD77161D7C}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Umbraco.Cloud.StorageProviders.AzureBlob", "src\Umbraco.Cloud.StorageProviders.AzureBlob\Umbraco.Cloud.StorageProviders.AzureBlob.csproj", "{DD4F5091-4EAB-4863-95EB-6143FD3A241C}"
77
EndProject
8-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Umbraco.Cloud.StorageProviders.AzureBlob", "src\Umbraco.Cloud.StorageProviders.AzureBlob\Umbraco.Cloud.StorageProviders.AzureBlob.csproj", "{DD4F5091-4EAB-4863-95EB-6143FD3A241C}"
8+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{428646C1-853C-4A76-A38A-D3334684B74D}"
9+
ProjectSection(SolutionItems) = preProject
10+
.editorconfig = .editorconfig
11+
.gitattributes = .gitattributes
12+
.gitignore = .gitignore
13+
LICENSE = LICENSE
14+
NuGet.config = NuGet.config
15+
README.md = README.md
16+
EndProjectSection
917
EndProject
1018
Global
1119
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -16,9 +24,6 @@ Global
1624
Release|x64 = Release|x64
1725
Release|x86 = Release|x86
1826
EndGlobalSection
19-
GlobalSection(SolutionProperties) = preSolution
20-
HideSolutionNode = FALSE
21-
EndGlobalSection
2227
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2328
{DD4F5091-4EAB-4863-95EB-6143FD3A241C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2429
{DD4F5091-4EAB-4863-95EB-6143FD3A241C}.Debug|Any CPU.Build.0 = Debug|Any CPU
@@ -33,7 +38,10 @@ Global
3338
{DD4F5091-4EAB-4863-95EB-6143FD3A241C}.Release|x86.ActiveCfg = Release|Any CPU
3439
{DD4F5091-4EAB-4863-95EB-6143FD3A241C}.Release|x86.Build.0 = Release|Any CPU
3540
EndGlobalSection
36-
GlobalSection(NestedProjects) = preSolution
37-
{DD4F5091-4EAB-4863-95EB-6143FD3A241C} = {EEC243CE-EF50-4E06-8B76-ACFD77161D7C}
41+
GlobalSection(SolutionProperties) = preSolution
42+
HideSolutionNode = FALSE
43+
EndGlobalSection
44+
GlobalSection(ExtensibilityGlobals) = postSolution
45+
SolutionGuid = {019DE032-AEE1-4803-BBAD-3595E4347425}
3846
EndGlobalSection
3947
EndGlobal

src/Umbraco.Cloud.StorageProviders.AzureBlob/AzureBlobComposer.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,41 @@
11
using System;
22
using Microsoft.Extensions.Configuration;
33
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Options;
45
using Umbraco.Cms.Core.Composing;
56
using Umbraco.Cms.Core.DependencyInjection;
67
using Umbraco.Cms.Web.Common.ApplicationBuilder;
7-
using Umbraco.StorageProviders.AzureBlob;
88

99
namespace Umbraco.Cloud.StorageProviders.AzureBlob
1010
{
11+
/// <summary>
12+
/// Automatically configures Azure Blob Storage for use on Umbraco Cloud.
13+
/// </summary>
14+
/// <seealso cref="Umbraco.Cms.Core.Composing.IComposer" />
1115
public class AzureBlobComposer : IComposer
1216
{
17+
/// <inheritdoc />
1318
public void Compose(IUmbracoBuilder builder)
1419
{
1520
if (builder == null) throw new ArgumentNullException(nameof(builder));
1621

17-
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("UMBRACO__CLOUD__ENVIRONMENT"))) return;
18-
19-
builder.AddAzureBlobMediaFileSystem();
20-
2122
var configuration = new ConfigurationBuilder()
2223
.AddEnvironmentVariables("Umbraco:Cloud:")
2324
.Build();
2425

25-
builder.Services.Configure<AzureBlobFileSystemOptions>(AzureBlobFileSystemOptions.MediaFileSystemName,
26-
options =>
27-
{
28-
var section = configuration.GetSection("Storage:AzureBlob");
26+
// Get options and manually validate (no need to add them to the service collection)
27+
var azureBlobOptions = configuration.GetSection("Storage:AzureBlob").Get<AzureBlobOptions>();
28+
if (azureBlobOptions == null) return;
2929

30-
options.ConnectionString = $"BlobEndpoint={section["Endpoint"]};SharedAccessSignature={section["SharedAccessSignature"]}";
31-
options.ContainerName = section["ContainerName"];
32-
});
30+
var validateResult = new DataAnnotationValidateOptions<AzureBlobOptions>(null).Validate(null, azureBlobOptions);
31+
if (validateResult.Failed) return;
32+
33+
// Configure Azure Blob Storage
34+
builder.AddAzureBlobMediaFileSystem(options =>
35+
{
36+
options.ConnectionString = azureBlobOptions.ConnectionString;
37+
options.ContainerName = azureBlobOptions.ContainerName;
38+
});
3339

3440
builder.Services.Configure<UmbracoPipelineOptions>(options =>
3541
{
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Umbraco.Cloud.StorageProviders.AzureBlob
4+
{
5+
/// <summary>
6+
/// The Azure Blob options used on Umbraco Cloud.
7+
/// </summary>
8+
internal class AzureBlobOptions
9+
{
10+
/// <summary>
11+
/// Gets or sets the endpoint.
12+
/// </summary>
13+
/// <value>
14+
/// The endpoint.
15+
/// </value>
16+
[Required]
17+
public string Endpoint { get; set; } = null!;
18+
19+
/// <summary>
20+
/// Gets or sets the shared access signature.
21+
/// </summary>
22+
/// <value>
23+
/// The shared access signature.
24+
/// </value>
25+
[Required]
26+
public string SharedAccessSignature { get; set; } = null!;
27+
28+
/// <summary>
29+
/// Gets the connection string.
30+
/// </summary>
31+
/// <value>
32+
/// The connection string.
33+
/// </value>
34+
public string ConnectionString => $"BlobEndpoint={Endpoint};SharedAccessSignature={SharedAccessSignature}";
35+
36+
/// <summary>
37+
/// Gets or sets the name of the container.
38+
/// </summary>
39+
/// <value>
40+
/// The name of the container.
41+
/// </value>
42+
[Required]
43+
public string ContainerName { get; set; } = null!;
44+
}
45+
}
Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<TargetFramework>net5.0</TargetFramework>
54
<Nullable>enable</Nullable>
65
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
6+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77
</PropertyGroup>
8-
98
<ItemGroup>
10-
<PackageReference Include="Nerdbank.GitVersioning" Version="3.4.220">
11-
<PrivateAssets>all</PrivateAssets>
12-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13-
</PackageReference>
14-
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.25.0.33663">
15-
<PrivateAssets>all</PrivateAssets>
16-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
17-
</PackageReference>
18-
<PackageReference Include="Umbraco.StorageProviders.AzureBlob" Version="0.1.0-gace5069105" />
9+
<PackageReference Include="Nerdbank.GitVersioning" Version="3.4.231" PrivateAssets="all" />
10+
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.27.0.35380" PrivateAssets="all" />
11+
<PackageReference Include="Umbraco.StorageProviders.AzureBlob" Version="0.1.0-gace5069105" />
1912
</ItemGroup>
20-
2113
</Project>

0 commit comments

Comments
 (0)