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

标签 azure azure-blob-storage

我编写了下面的代码来获取带有缓存过期 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/

相关文章:

azure - 在主二头肌模板中添加 Azure 流量管理器端点作为子资源

azure - 我可以循环 ARM 模板中的属性吗?

sql - Azure SQL 数据迁移助手卡在 “Migrate Data” 上

python - 如何在 python 脚本中创建文件而不将其存储在本地目录中并将其保存在 azure blob 存储上的不同容器中?

azure - 提供对 Azure Blob 容器中文件夹的访问

powershell - 更改工作流的 SerializationMethod

c# - 来自 Azure Blob 下载图像的链接,但我只需要观看(Asp.NET Core)

linq - 如何在Azure表Linq查询中使用包含?

c# - 上传到 Azure Block Blob 时如何选择最佳 block 大小

c# - ADAL - AcquireTokenSilentAsync 失败(Azure Active Directory 身份验证库)