简体   繁体   中英

How to connect to blob storage container using c#

I need to connect to blob storage container and retrieve the data inside the container. I do not have connection string I need to connect via access token. I have the code communicating with blob using connection string. Could anyone modify the code based on communicating with access key and retrieve those data from the container.

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

Here is the code that worked for me

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

RESULT:在此处输入图像描述

REFERENCES: Access blob by sas token

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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