gpt4 book ai didi

c# - 在 C# 中为 block blob 生成共享访问签名

转载 作者:行者123 更新时间:2023-12-03 04:36:41 25 4
gpt4 key购买 nike

我有一些代码应该为我在 azure blob 中的 pdf 上提供共享访问签名。

class Program
{
static void Main(string[] args)
{

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("BlobEndpoint=<url>;AccountKey=<accountKey>");

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

CloudBlobContainer container = blobClient.GetContainerReference("products");

BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;

container.SetPermissions(containerPermissions);

containerPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessPolicy()
{
SharedAccessStartTime = DateTime.UtcNow.AddSeconds(1),
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
Permissions = SharedAccessPermissions.Read | SharedAccessPermissions.Write
});

string sas = container.GetSharedAccessSignature(new SharedAccessPolicy(), "mypolicy");

CloudBlobClient sasBlobClient = new CloudBlobClient(storageAccount.BlobEndpoint, new StorageCredentialsSharedAccessSignature(sas));

CloudBlob blob = sasBlobClient.GetBlobReference("products/virtual/computer/brochure.pdf");

Console.WriteLine(blob.Uri.AbsoluteUri + sas);

}
}

当我运行代码并将 URL 粘贴到浏览器中时,我得到以下内容:

<?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:05c9cfc0-d7fd-4352-9b21-9e273efdc09f Time:2014-02-05T13:36:05.6126781Z</Message>
<AuthenticationErrorDetail>Signature did not match. String to sign used was /portalvhdsv53d88583slg1/products mypolicy</AuthenticationErrorDetail>
</Error>

我想要的只是生成一个 URL,让我可以在一段时间内访问 Blob 存储中某个项目的 pdf。我做错了什么?

最佳答案

根据您的代码,权限策略将在创建后 1 秒后生效。

SharedAccessStartTime = DateTime.UtcNow.AddSeconds(1),
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),

所以创建后至少要等待1秒才能访问。

试试这个插入:

SharedAccessStartTime = DateTime.UtcNow(),
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),

在这种情况下,策略立即生效

关于c# - 在 C# 中为 block blob 生成共享访问签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21579119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com