【问题标题】:Access blob by URI using Storage Connection String in C# SDK使用 C# SDK 中的存储连接字符串通过 URI 访问 blob
【发布时间】:2021-03-12 04:49:20
【问题描述】:

我在 C# 中使用带有 .net 核心的 Azure.Storage.Blobs SDK 版本 12.7,我想使用 Blob 存储连接字符串通过其 URI 直接访问 Blob。

我该怎么做? 我假设我可以使用 BlobClient 类,该构造函数采用 URI,但是我没有被授权。构造函数也需要一个StorageSharedKeyCredential,但是如何从连接字符串创建它?

【问题讨论】:

    标签: c# .net-core azure-storage azure-blob-storage


    【解决方案1】:

    您可以通过这种方式创建StorageSharedKeyCredential。注意它使用the key instead of the connection string:

    StorageSharedKeyCredential storageSharedKeyCredential = new StorageSharedKeyCredential(<your-account-name>,<your-account-key>)
    

    你可以参考这个official document

    顺便说一句,如果你想使用连接字符串,你可以这样使用:

    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
    
    BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(<you-container-name>)
    
    BlobClient blobClient = containerClient.GetBlobClient(fileName);
    

    请参考这个official document

    【讨论】:

    • 谢谢!有没有办法从连接字符串中获取帐户名和帐户密钥?我不想使用 BlobContainerClient,因为我需要通过它的 URI 直接访问 Blob ;-)。在我的情况下,我没有容器名称,只有 Blob URI。
    • 可以从连接字符串中获取key和value,解析连接字符串即可。连接字符串的格式如下:DefaultEndpointsProtocol=https;AccountName=&lt;your-account-key&gt;;AccountKey=&lt;your-account-key&gt;;EndpointSuffix=core.windows.net
    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 2022-01-13
    • 2013-02-24
    • 1970-01-01
    • 2012-10-21
    • 2022-09-28
    • 2020-08-25
    • 2016-02-18
    相关资源
    最近更新 更多