如何使用 C# 连接到 Blob 存储容器

How to connect to blob storage container using c#

我需要连接到 blob 存储容器并检索容器内的数据。我没有需要通过访问令牌连接的连接字符串。我有使用连接字符串与 blob 通信的代码。谁能根据与访问密钥的通信修改代码并从容器中检索这些数据。

string storageConnectionString = "";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("ContainerName");
CloudBlockBlob blob = container.GetBlockBlobReference("FileName");
string xmlFile = blob.DownloadTextAsync().Result;
Console.WriteLine(xmlFile);

这是对我有用的代码

using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            string storageAccountName = "<YOUR STORAGE ACCOUNT>";
            string containerName = "<YOUR CONTAINER NAME>";
            string sasToken = "<YOUR SAS TOKEN>";
            StorageCredentials creds;
            CloudBlobContainer cloudBlobContainer;
            creds = new StorageCredentials(sasToken);

            cloudBlobContainer = new CloudBlobContainer(new Uri("https://" + storageAccountName + ".blob.core.windows.net/" + containerName), creds);
            BlobContinuationToken blobContinuationToken = null;
            var blobs = cloudBlobContainer.ListBlobsSegmentedAsync("", blobContinuationToken);
            var blob = blobs.Result;
            foreach (var i_blob in blob.Results)
            {
                Console.WriteLine(i_blob.Uri);
            }

            Console.ReadKey();
        }
    }
}

结果:

参考资料: Access blob by sas token