【问题标题】:Download Azure Blob using SAS key in .net在 .net 中使用 SAS 密钥下载 Azure Blob
【发布时间】:2021-11-13 06:30:11
【问题描述】:

我正在尝试使用 SAS 令牌访问 Azure Blob 以从 Blob 下载数据,这是我尝试过的。

我对此很陌生,所以不知道它应该如何工作

这是我迄今为止尝试过的。

            const string sasToken = "SAS TOKEN";
  
            var blobUri = new System.Uri("URI");

            var blobUriBuilder = new System.UriBuilder(blobUri)
            {
                Query = sasToken
            };

            var authorizedBlobUri = blobUriBuilder.Uri;
            var blobClient = new BlobClient(authorizedBlobUri);

            Console.WriteLine("blob read successfully: {0} ", blobClient.BlobContainerName);

            // Create a local file in the ./data/ directory for uploading and downloading
            string localPath = "./data/";
            string fileName = "quickstart" + Guid.NewGuid().ToString() + ".txt";
            string localFilePath = Path.Combine(localPath, fileName);

            string downloadFilePath = localFilePath.Replace(".txt", "DOWNLOADED.txt");
           
            var response = await blobClient.DownloadToAsync(downloadFilePath);
            Console.WriteLine("blob read successfully: {0} {1}", response.Content, response.Status);

错误是

您能否建议这是否是从 Blob 获取数据的正确方法以及如何解决我遇到的此错误

【问题讨论】:

  • 这很可能是 SAS 令牌的问题。你能分享一下 SAS 令牌吗?
  • 你好@Auo,如果我的回答对你有帮助,你可以接受它作为答案(点击答案旁边的复选标记,将它从灰色切换为填充。)。这对其他社区成员可能是有益的。谢谢

标签: c# azure-blob-storage


【解决方案1】:

建议您在 GITHUB 中使用其社区成员之一的以下代码共享。

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

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

namespace AzureBlobStorage
{
    internal static class Program
    {
        private static void Main()
        {
            const string accountName = "account-name";
            const string blobContainerName = "blob-container-name";
            const string downloadLocation = @"C:\data";
            const string sasToken =
                @"<SAS token (just the token, not the URI)>";
            
            // Create a credential object from the SAS token then use this and the account name to create a cloud storage connection
            var accountSAS = new StorageCredentials(sasToken);
            var storageAccount = new CloudStorageAccount(accountSAS, accountName, null, true);

            // Get the Blob storage container
            var blobClient = storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(blobContainerName);

            // Fail if the container does not exist
            if (!container.Exists())
            {
                Console.Error.WriteLine($"Can't find container '{blobContainerName}'");
                return;
            }

            // Get the list of blobs in the container, just for demo purposes this is ordered by blob name and taking only
            // the first 10 items
            var blobs = container
                .ListBlobs(blobListingDetails: BlobListingDetails.Metadata)
                .OfType<CloudBlob>()
                .OrderBy(blobItem => blobItem.Name)
                .Take(10)
                .ToList();

            // Set the request options to ensure we're performing MD5 validation
            var downloadOptions = new BlobRequestOptions
            {
                DisableContentMD5Validation = false,
                UseTransactionalMD5 = true
            };

            // Download the files
            // The parameters are named for reference only and can safely be removed
            Task.WaitAll(
                blobs.Select(
                    blobItem => blobItem.DownloadToFileAsync(
                        path: Path.Combine(downloadLocation, blobItem.Name),
                        mode: FileMode.Create, 
                        accessCondition: null, 
                        options: downloadOptions, 
                        operationContext: null)).ToArray());
        }
    }
}

参考:https://gist.github.com/dazfuller/9b5ea145525879fb5114bd78b53e9ee5#file-azureblobdownload-cs

【讨论】:

  • 这不适用于我在针对存储帐户进行测试时。要点没有提供他们正在使用的包的特定版本。例如,今天的最新版本没有 ListBlobs 和 container.Exists() 无效。
猜你喜欢
  • 2021-11-10
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 1970-01-01
  • 2019-09-11
  • 2020-04-28
  • 2021-11-30
  • 2021-08-15
相关资源
最近更新 更多