简体   繁体   English

以编程方式从 Azure blob 存储连接字符串中提取属性

[英]Programmatically extract properties from an Azure blob storage connection string

Given a blob storage connection string such as:给定一个 blob 存储连接字符串,例如:

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

Is there a known Microsoft object that this can be converted / deserialized into?是否有已知的 Microsoft 对象可以将其转换/反序列化为? I don't want to actually parse the string, but I need to extract the AccountName and AccountKey from the entire connection string, which I have as a string.我不想实际解析字符串,但我需要从整个连接字符串中提取 AccountName 和 AccountKey,我有一个字符串。

To pre-empt possible "Why do you want to do this?"预先阻止可能的“你为什么要这样做?” questions... I have an existing class that requires the connection string to be injected as a string.问题...我有一个现有的类,需要将连接字符串作为字符串注入。 To avoid breaking changes, I can't alter that.为避免破坏性更改,我无法更改它。 But I do need to add some methods in this class that need the AccountName and AccountKey as individual items.但是我确实需要在这个类中添加一些需要 AccountName 和 AccountKey 作为单独项目的方法。

Thanks!谢谢!

If you install Microsoft.Azure.Storage.Common , you can extract several bits of your connection string programmatically, without parsing the connection string yourself.如果安装Microsoft.Azure.Storage.Common ,则可以以编程方式提取连接字符串的几位,而无需自己解析连接字符串。

For example (with actual info obfuscated):例如(混淆了实际信息):

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());
        }
    }
}

This gives output something like:这给出了类似的输出:

https://youraccount.blob.core.windows.net/
---
Primary = 'https://youraccount.blob.core.windows.net/'; Secondary = 'https://youraccount-secondary.blob.core.windows.net/'
---
youraccount
---
yourkey==

There are no classes that I know of that do this, but it wouldn't be that hard to change it into a dictionary.据我所知,没有任何类可以做到这一点,但将其更改为字典并不难。 Example below.下面举例。

        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]);
        }

Then you could access the values you need using this.然后你可以使用它访问你需要的值。

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

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

CloudStorageAccount sa = CloudStorageAccount.Parse(connString);

The answer of @David Makogon is certainly the most elegant but the package Microsoft.Azure.Storage.Common is deprecated (as stated in comments). @David Makogon 的答案当然是最优雅的,但Microsoft.Azure.Storage.Common包已被弃用(如评论中所述)。 Based on @Patrick Mcvay answer (which is just a bit bugged as there might be '=' in the connection string values), an easy way to parse a connection string would be:基于@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM