Skip to content

Commit

Permalink
[Azure] Support raw connection strings for Azure Blob Storage (loic-s…
Browse files Browse the repository at this point in the history
  • Loading branch information
loic-sharma authored Oct 30, 2019
1 parent ce69248 commit c61fdc1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
9 changes: 7 additions & 2 deletions docs/quickstart/azure.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ Update the `appsettings.json` file:

"Storage": {
"Type": "AzureBlobStorage",

"AccountName": "my-account",
"Container": "my-container",
"AccessKey": "abcd1234"
"AccessKey": "abcd1234",
"Container": "my-container"

// You can also use a connection string:
// "ConnectionString": "AccountName=my-account;AccountKey=abcd1234;...",
// "Container": "my-container"
},

...
Expand Down
58 changes: 52 additions & 6 deletions src/BaGet.Azure/Configuration/BlobStorageOptions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,62 @@
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace BaGet.Azure.Configuration
{
public class BlobStorageOptions
/// <summary>
/// BaGet's configurations to use Azure Blob Storage to store packages.
/// See: https://loic-sharma.github.io/BaGet/quickstart/azure/#azure-blob-storage
/// </summary>
public class BlobStorageOptions : IValidatableObject
{
[Required]
/// <summary>
/// The Azure Blob Storage connection string.
/// If provided, ignores <see cref="AccountName"/> and <see cref="AccessKey"/>.
/// </summary>
public string ConnectionString { get; set; }

/// <summary>
/// The Azure Blob Storage account name. Ignored if <see cref="ConnectionString"/> is provided.
/// </summary>
public string AccountName { get; set; }

[Required]

/// <summary>
/// The Azure Blob Storage access key. Ignored if <see cref="ConnectionString"/> is provided.
/// </summary>
public string AccessKey { get; set; }

[Required]
/// <summary>
/// The Azure Blob Storage container name.
/// </summary>
public string Container { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
const string helpUrl = "https://loic-sharma.github.io/BaGet/quickstart/azure/#azure-blob-storage";

if (string.IsNullOrEmpty(ConnectionString))
{
if (string.IsNullOrEmpty(AccountName))
{
yield return new ValidationResult(
$"The {nameof(AccountName)} configuration is required. See {helpUrl}",
new[] { nameof(AccountName) });
}

if (string.IsNullOrEmpty(AccessKey))
{
yield return new ValidationResult(
$"The {nameof(AccessKey)} configuration is required. See {helpUrl}",
new[] { nameof(AccessKey) });
}
}

if (string.IsNullOrEmpty(Container))
{
yield return new ValidationResult(
$"The {nameof(Container)} configuration is required. See {helpUrl}",
new[] { nameof(Container) });
}
}
}
}
5 changes: 5 additions & 0 deletions src/BaGet.Azure/Extensions/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public static IServiceCollection AddBlobStorageService(this IServiceCollection s
{
var options = provider.GetRequiredService<IOptions<BlobStorageOptions>>().Value;

if (!string.IsNullOrEmpty(options.ConnectionString))
{
return CloudStorageAccount.Parse(options.ConnectionString);
}

return new CloudStorageAccount(
new StorageCredentials(
options.AccountName,
Expand Down

0 comments on commit c61fdc1

Please sign in to comment.