gpt4 book ai didi

azure - 如何找到共享访问 token 已过期的 bloburl?

转载 作者:行者123 更新时间:2023-12-03 03:15:01 24 4
gpt4 key购买 nike

我编写了下面的代码来获取带有缓存过期 token 的 blob url,实际上设置了 2 小时来使 blob url 过期,

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlockBlob blockBlob = container.GetBlockBlobReference("blobname");
//Create an ad-hoc Shared Access Policy with read permissions which will expire in 2 hours
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(2),
};

SharedAccessBlobHeaders headers = new SharedAccessBlobHeaders()
{
ContentDisposition = string.Format("attachment;filename=\"{0}\"", "blobname"),
};
var sasToken = blockBlob.GetSharedAccessSignature(policy, headers);
blobUrl = blockBlob.Uri.AbsoluteUri + sasToken;

使用上面的代码我得到带有有效到期 token 的 blob url,现在我想检查 blob url 在一个客户端应用程序中是否有效。我通过传递 URL 并获取响应状态代码尝试了 Web 请求和 http 客户端方法。如果响应代码是 404,那么我假设 URL 已过期,如果不是,则 URL 仍然有效,但这种方法需要更多时间。

请建议我任何其他方式。

最佳答案

我尝试运行与您的代码非常相似的代码,但收到 403 错误,这实际上是本例中所期望的。根据您的问题,我不确定 403 是否比 404 对您更有帮助。以下是在返回 403 的控制台应用程序中运行的代码:

class Program
{
static void Main(string[] args)
{
string blobUrl = CreateSAS();
CheckSAS(blobUrl);

Console.ReadLine();
}

//This method returns a reference to the blob with the SAS, and attempts to read it.
static void CheckSAS(string blobUrl)
{
CloudBlockBlob blob = new CloudBlockBlob(new Uri(blobUrl));

//If the DownloadText() method is run within the two minute period that the SAS is valid, it succeeds.
//If it is run after the SAS has expired, it returns a 403 error.
//Sleep for 3 minutes to trigger the error.
System.Threading.Thread.Sleep(180000);
Console.WriteLine(blob.DownloadText());
}

//This method creates the SAS on the blob.
static string CreateSAS()
{
string containerName = "forum-test";
string blobName = "blobname";
string blobUrl = "";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
container.CreateIfNotExists();

CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName + DateTime.Now.Ticks);
blockBlob.UploadText("Blob for forum test");

//Create an ad-hoc Shared Access Policy with read permissions which will expire in 2 hours
SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
{
Permissions = SharedAccessBlobPermissions.Read,
SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(2),
};

SharedAccessBlobHeaders headers = new SharedAccessBlobHeaders()
{
ContentDisposition = string.Format("attachment;filename=\"{0}\"", blobName),
};
var sasToken = blockBlob.GetSharedAccessSignature(policy, headers);
blobUrl = blockBlob.Uri.AbsoluteUri + sasToken;

return blobUrl;
}
}

在某些情况下,SAS 故障确实会返回 404,这可能会给使用 SAS 进行故障排除操作带来问题。 Azure 存储团队已意识到此问题,在未来版本中,SAS 故障可能会返回 403。如需帮助排除 404 错误,请参阅 http://azure.microsoft.com/en-us/documentation/articles/storage-monitoring-diagnosing-troubleshooting/#SAS-authorization-issue

关于azure - 如何找到共享访问 token 已过期的 bloburl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495038/

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