【问题标题】:Programmatically extract properties from an Azure blob storage connection string以编程方式从 Azure Blob 存储连接字符串中提取属性
【发布时间】:2020-07-14 19:42:54
【问题描述】:

给定一个 blob 存储连接字符串,例如:

DefaultEndpointsProtocol=https;AccountName=foo;AccountKey=bar;EndpointSuffix=core.windows.net

是否有可以将其转换/反序列化为的已知 Microsoft 对象?我不想实际解析字符串,但我需要从整个连接字符串中提取 AccountName 和 AccountKey,我将其作为字符串。

为了抢占可能的“你为什么要这样做?”问题...我有一个现有的类,需要将连接字符串作为字符串注入。为避免破坏性更改,我无法更改它。但我确实需要在这个类中添加一些需要 AccountName 和 AccountKey 作为单独项目的方法。

谢谢!

【问题讨论】:

    标签: c# azure azure-blob-storage


    【解决方案1】:

    @David Makogon 的答案当然是最优雅的,但 Microsoft.Azure.Storage.Common 包已被弃用(如 cmets 中所述)。基于@Patrick Mcvay 的回答(这只是有点错误,因为连接字符串值中可能有'='),解析连接字符串的简单方法是:

    var parsedConnectionString = new Dictionary<string, string>();
    foreach (var item in ConnectionString.Split(';'))
    {
        var idx = item.IndexOf('=');
        parsedConnectionString[item.Substring(0, idx)] =
            item.Substring(idx + 1, item.Length - idx - 1);
    }
    

    【讨论】:

      【解决方案2】:

      为此,我们有来自Microsoft.WindowsAzure.Storage 程序集的CloudStorageAccount 类型。

      CloudStorageAccount sa = CloudStorageAccount.Parse(connString);
      

      【讨论】:

      【解决方案3】:

      如果您安装Microsoft.Azure.Storage.Common,您可以通过编程方式提取连接字符串的几位,而无需自己解析连接字符串。

      例如(实际信息被混淆):

      using System;
      using Microsoft.Azure.Storage;
      
      namespace dotnet_connectionstring
      {
          class Program
          {
              static void Main(string[] args)
              {
                  CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=yourkey==;EndpointSuffix=core.windows.net");
                  Console.WriteLine(storageAccount.BlobEndpoint);
                  Console.WriteLine("---");
                  Console.WriteLine(storageAccount.BlobStorageUri);
                  Console.WriteLine("---");
                  Console.WriteLine(storageAccount.Credentials.AccountName);
                  Console.WriteLine("---");
                  Console.WriteLine(storageAccount.Credentials.ExportBase64EncodedKey());
              }
          }
      }
      

      这给出了类似的输出:

      https://youraccount.blob.core.windows.net/ --- Primary = 'https://youraccount.blob.core.windows.net/';次要 = 'https://youraccount-secondary.blob.core.windows.net/' --- 你的帐户 --- 你的钥匙==

      【讨论】:

      • Microsoft.Azure.Storage.Common 现已弃用。不幸的是,替换包Azure.Storage.Common 似乎没有任何方法可以以相同的方式解析连接字符串。
      • 我需要从连接字符串中提取帐户密钥,因为我需要生成一个 SAS,如果没有需要帐户密钥的 StorageSharedKeyCredential,这将无法发生。耻辱。
      【解决方案4】:

      据我所知,没有任何类可以做到这一点,但将其更改为字典并不难。下面的例子。

              string connString = "DefaultEndpointsProtocol=https;AccountName=foo;AccountKey=bar;EndpointSuffix=core.windows.net";
      
              var connStringArray = connString.Split(';');
      
              var dictionary = new Dictionary<string, string>();
      
              foreach (var item in connStringArray)
              {
                  var itemKeyValue = item.Split('=');
                  dictionary.Add(itemKeyValue[0], itemKeyValue[1]);
              }
      

      然后您可以使用它访问所需的值。

      dictionary["AccountName"]
      dictionary["AccountKey"]
      

      【讨论】:

      • 是的,好的。这是我希望避免的。但也许这是最好的方法。
      • 对不起,我什至没有意识到你这么说。我真的需要开始更好地阅读这些问题。
      • 这就是图书馆正在做的事情——没什么复杂的。
      • 你可以像connString.Split(';').Where(x =&gt; x.Length &gt; 0).Select(x =&gt; x.Split(new[] {'='}, 2)).ToDictionary(x =&gt; x[0], x =&gt; x[1])这样把它变成一个单行字
      • 我已经把它变成了一个简单的实用程序类here(并考虑了@Vince.Bdn的注意事项)
      猜你喜欢
      • 2021-11-06
      • 2021-04-11
      • 2021-11-11
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多