如何使用 .net 中的 SAS 密钥从 Azure Blob 读取数据
Posted
技术标签:
【中文标题】如何使用 .net 中的 SAS 密钥从 Azure Blob 读取数据【英文标题】:How to read data from Azure Blob using SAS key in .net 【发布时间】:2021-11-10 23:34:44 【问题描述】:您好,我正在尝试使用 SAS 令牌访问 Azure Blob 以从 Blob 中获取数据,这是我尝试过的。
我对此很陌生,所以不知道它应该如何工作
这是我迄今为止尝试过的。
您能否建议这是否是从 Blob 获取数据的正确方法以及如何解决我遇到的此错误
错误是
这是目前写的代码
namespace BlobQuickstartV12
class Program
static async Task Main()
const string sasToken = "sasToken";
var blobUri = new System.Uri("https://xx.blob.xx.xx.net/xxxx?sp=ral&st=2021-07-28T22:29:14Z&se=2021-08-26T06:29:14Z&spr=https&sv=2020-08-04&sr=c&sig=ZQ%2Bl5yZ2JpEb7TwnWGBNz9hSjGTZr3ZarSPaQ1ssz48%3D");
var blobUriBuilder = new System.UriBuilder(blobUri)
Query = sasToken
;
var authorizedBlobUri = blobUriBuilder.Uri;
var blobClient = new Azure.Storage.Blobs.BlobClient(authorizedBlobUri);
var getBlobResponse = await blobClient.DownloadContentAsync(); //ERROR HERE
Console.WriteLine("Read blob successfully", getBlobResponse.Value);
错误
Azure.RequestFailedException: 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:cbde76c1-b01e-008e-657c-aa7496000999
Time:2021-09-15T21:55:49.0166554Z
Status: 403 (Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.)
ErrorCode: AuthenticationFailed
Additional Information:
AuthenticationErrorDetail: Signature not valid in the specified time frame: Start [Wed, 28 Jul 2021 22:29:14 GMT] - Expiry [Thu, 26 Aug 2021 06:29:14 GMT] - Current [Wed, 15 Sep 2021 21:55:49 GMT]
Content:
<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:cbde76c1-b01e-008e-657c-aa7496000000
Time:2021-09-15T21:55:49.0166554Z</Message><AuthenticationErrorDetail>Signature not valid in the specified time frame: Start [Wed, 28 Jul 2021 22:29:14 GMT] - Expiry [Thu, 26 Aug 2021 06:29:14 GMT] - Current [Wed, 15 Sep 2021 21:55:49 GMT]</AuthenticationErrorDetail></Error>
Headers:
Server: Microsoft-HTTPAPI/2.0
x-ms-request-id: cbde76c1-b01e-008e-657c-aa7496000000
x-ms-error-code: AuthenticationFailed
Date: Wed, 15 Sep 2021 21:55:48 GMT
Content-Length: 544
Content-Type: application/xml
'
有什么建议
谢谢 回复
【问题讨论】:
【参考方案1】:您应该能够像这样使用“AzureSasCredential”初始化 BlobClient:
var client = new BlobClient(new Uri("https://xx.blob.xx.xx.net/xxxx"), new AzureSasCredential("sp=ral&st=2021-07-28T22:29:14Z&se=2021-08-26T06:29:14Z&spr=https&sv=2020-08-04&sr=c&sig=ZQ%2Bl5yZ2JpEb7TwnWGBNz9hSjGTZr3ZarSPaQ1ssz48%3D"));
编辑:
我刚刚看到错误,您的 SAS 密钥已过期:
签名在指定时间范围内无效:开始 [格林威治标准时间 2021 年 7 月 28 日星期三 22:29:14] - 到期 [格林威治标准时间 2021 年 8 月 26 日星期四 06:29:14]
【讨论】:
谢谢菲尔,当我添加以上行并修改新密钥时,它返回一个新错误:System.ArgumentException: '当资源 URI 还包含共享访问签名时,您不能使用 AzureSasCredential : > 您可以通过创建 BlobUriBuilder、将 BlobUriBuilder.Sas 设置为 null 并调用 BlobUriBuilder.ToUri.'来删除共享访问签名 我已经获得了这个 URI,我无法修改任何内容/从 URI 中删除,如错误所示。 嗨 Auo,请确保您只将域放在 URI 中,而不是查询(问号后面包含 SAS 的所有内容)。因此,从上面的示例中,URI = 'xx.blob.xx.xx.net/xxxx' 和 AzureSasCredential = 'sp=ral&st=2021-07-28T22:29:14Z&se=2021-08-26T06:29:14Z&spr=https&sv=2020-08-04&sr= c&sig=ZQ%2Bl5yZ2JpEb7TwnWGBNz9hSjGTZr3ZarSPaQ1ssz48%3D'。我将编辑我的答案以使其更清楚。以上是关于如何使用 .net 中的 SAS 密钥从 Azure Blob 读取数据的主要内容,如果未能解决你的问题,请参考以下文章