storage
storage copied to clipboard
FormatException when using Azure Storage with connection string containing "+" character
I'm trying to use Azure Storage with connection string. The FromConnectionString methods throws FormatException if the key includes the "+" character. This is the minimal code to reproduce:
The key was changed before posting this, if course.
// Initialize Storage.NET and Azure Blobs
using Storage.Net;
StorageFactory.Modules.UseAzureBlobStorage();
// This does work
var blobStorage = StorageFactory.Blobs.AzureBlobStorageWithSharedKey("myaccountname", "L5oM6w/wdzNH/aoxhssLqky0wp47N0H8pgTWXbgQQl8B2kiUXbe4HUWj22UQe1VmNZhgIuQfbV9j+AStuW8V6Q==");
// This does not
var blobStorage = StorageFactory.Blobs.FromConnectionString("azure.blob://account=myaccountname;key=L5oM6w/wdzNH/aoxhssLqky0wp47N0H8pgTWXbgQQl8B2kiUXbe4HUWj22UQe1VmNZhgIuQfbV9j+AStuW8V6Q==");
// This does work again when "+" was replaced by "X"
var blobStorage = StorageFactory.Blobs.FromConnectionString("azure.blob://account=myaccountname;key=L5oM6w/wdzNH/aoxhssLqky0wp47N0H8pgTWXbgQQl8B2kiUXbe4HUWj22UQe1VmNZhgIuQfbV9jXAStuW8V6Q==");
The faulting line results in the following exception:
Error: System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
at System.Convert.FromBase64CharPtr(Char* inputPtr, Int32 inputLength)
at System.Convert.FromBase64String(String s)
at Azure.Storage.StorageSharedKeyCredential.SetAccountKey(String accountKey)
at Azure.Storage.StorageSharedKeyCredential..ctor(String accountName, String accountKey)
at Storage.Net.Factory.AzureBlobStorageWithSharedKey(IBlobStorageFactory factory, String accountName, String key, Uri serviceUri)
at Storage.Net.Microsoft.Azure.Storage.Blobs.Module.CreateBlobStorage(StorageConnectionString connectionString)
at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate)
at Storage.Net.ConnectionString.ConnectionStringFactory.Create[TInstance](String connectionString, Func`3 createAction)
at Storage.Net.ConnectionString.ConnectionStringFactory.CreateBlobStorage(String connectionString)
at Storage.Net.Factory.FromConnectionString(IBlobStorageFactory factory, String connectionString)
The key is (obviously) valid Base-64 value. I discovered that when I use key that does not contain the plus character, it works just fine. I suppose there is some bug in the connection string parsing code that treats + as an delimiter or something similar.
The occurence of this bug is random, as the key is generated by Azure and is random, so it may or may not have contain this character.