Skip to content

Instantly share code, notes, and snippets.

@seankearon
Last active July 13, 2022 14:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seankearon/cae6441bd2789fede6f0ca80a50fd790 to your computer and use it in GitHub Desktop.
Save seankearon/cae6441bd2789fede6f0ca80a50fd790 to your computer and use it in GitHub Desktop.
Simple Azure Storage Connection String Parser

At time of writing (May 2021), the latest Azure.Storage.Blobs package doesn't seem to have a convenience class for parsing Azure Storage Account connection strings.

This Gist contains a simple class to parse values from connection strings which is based on this SO answer from Partick Mcavy.

There is an example of usage that creates a SAS access URL for a blob (my original use case behind this!) which takes inspriation from Deeksha Gupta's answer here.

using System;
using System.Collections.Generic;
/// <summary> Based on Patric Macvey's answer here https://stackoverflow.com/a/60998564/2608 </summary>
public class AzureStorageConnectionStringParser
{
readonly Dictionary<string, string> _values = new Dictionary<string, string>();
readonly string _connectionString;
public AzureStorageConnectionStringParser(string connectionString)
{
_connectionString = connectionString;
foreach (var item in _connectionString.Split(';').Where(x => !string.IsNullOrWhiteSpace(x)))
{
var (key, value) = SplitAtFirstEquals(item);
_values.Add(key, value);
}
}
public string ConnectionString => _connectionString;
public string AccountName => ValueOrDefault("AccountName");
public string AccountKey => ValueOrDefault("AccountKey");
public string BlobEndpoint => ValueOrDefault("BlobEndpoint");
public Uri BlobEndpointUri => new Uri(BlobEndpoint);
private string ValueOrDefault(string key) => _values.TryGetValue(key, out var value) ? value : default;
private (string key, string value) SplitAtFirstEquals(string s)
{
var index = s.IndexOf('=');
var key = s.Substring(0, index);
var value = s.Substring(index + 1, s.Length - index - 1);
return (key, value);
}
}
/// <summary>
/// This shows an example of use of the AzureStorageConnectionStringParser and also how to construct a SAS access URL for a blob.
/// </summary>
public class SomeClass
{
private ConnectionString { get; }
private ContainerName { get; }
/// <summary>
/// Inspriation taken from Deeksha Gupta's answer here: https://github.com/Azure/azure-storage-net/issues/895#issuecomment-843038602
/// </summary>
public string GeneratePreSignedURLAzure(string containerName, string blobName, int timeInMinutes = 15, string storedPolicyName = null)
{
var connectionString = new AzureStorageConnectionStringParser(ConnectionString);
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(connectionString.AccountName, connectionString.AccountKey);
var sasBuilder = new BlobSasBuilder()
{
BlobContainerName = containerName,
BlobName = blobName,
Resource = "b",
};
if (storedPolicyName == null)
{
sasBuilder.StartsOn = DateTimeOffset.UtcNow.AddMinutes(-5); // Allow for clock differences between the client and the server.
sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(timeInMinutes);
sasBuilder.SetPermissions(BlobContainerSasPermissions.Read);
}
else
{
sasBuilder.Identifier = storedPolicyName;
}
var sasToken = sasBuilder.ToSasQueryParameters(sharedKeyCredential);
return new Uri($"{connectionString.BlobEndpointUri}{ContainerName}/{blobName}?{sasToken}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment