azure - Windows Azure 共享访问签名始终给出 : Forbidden 403

标签 azure azure-storage azure-blob-storage

我正在尝试获取单个 blob 的共享访问签名,然后使用 REST api 下载该 blob。但是,我总是收到禁止的 403 错误消息。都在存储模拟器和云上。这是我的代码:

CloudStorageAccount storageAccount = CloudStorageAccount.Parse("myConnectionStringHere...");

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("containerName");
CloudBlob blob = container.GetBlobReference("blobName");

string sasToken = blob.GetSharedAccessSignature(new SharedAccessPolicy()
            {
                Permissions = SharedAccessPermission.Read,
                SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromHours(24)
            }
            );

string completeUri = string.Format(CultureInfo.InvariantCulture, "{0}{1}", blob.Uri, sasToken);

// now use the uri to make the rest call and download
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(completeUri);
request.Method = "GET";
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
{
    using (Stream s = resp.GetResponseStream())
        {
            using (FileStream fs = new FileStream("test.jpg", FileMode.Create, FileAccess.Write))
                {
                        byte[] buffer = new byte[8 * 1024];
                        int len;
                        while ((len = s.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            fs.Write(buffer, 0, len);
                        }
                    }
                }
            }

调用 GetResponse 时,我不断收到 403 错误。任何帮助表示赞赏!

编辑:忘记提及:我正在使用最新的 azure sdk (2.0)

编辑2:我进行了很多实验,发现了一个名为Azure Management Studio的工具。该工具能够创建 SAS token 。我这样做了,并将其与我的 REST 调用代码一起使用。这工作得很好,所以错误必须在我编写的 token 创建代码中。然而,sas字符串的格式是完全相同的。我不知道还能尝试什么

最佳答案

我注意到的一些事情:

  1. 您提到您正在使用 SDK 2.0,但我认为您没有使用最新的存储客户端库 (2.0.6)。从您的代码来看,您似乎仍在使用旧的存储客户端库(1.8)。如果您引用的是 Microsoft.WindowsAzure.StorageClientMicrosoft.WindowsAzure.Storage,请检查您的代码。如果是前者,那么您使用的是旧库。
  2. 如果您使用的是旧存储客户端库,请注意,对于使用旧存储 REST API 的旧存储客户端库,对于匿名 SAS token (即没有容器访问策略的 token ),您无法指定过期时间时间距当前时间超过 1 小时(当然是 UTC 时间)。如果我尝试使用您的 URL,则会收到以下错误消息(在 AuthenticationErrorDetail 节点下:

Access without signed identifier cannot have time window more than 1 hour

您可以尝试创建一个有效期不到 1 小时的 SAS token 吗?例如

var sasToken = blob.GetSharedAccessSignature(new SharedAccessPolicy
    {
        Permissions = SharedAccessPermission.Read,
        SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(30)
    }
);

如果您想继续使用旧的存储客户端库,您有几种选择:

  1. 创建一个有效期不超过一小时的 SAS token ,如上所述。
  2. 使用容器级访问策略创建 SAS token 。通过容器级访问策略,您将能够定义过期日期超过 1 小时的 SAS token 。欲了解更多信息,请点击这里:http://msdn.microsoft.com/en-us/library/windowsazure/ee393341.aspx

如果您使用新的存储客户端库,您将能够定义更长的持续时间 token ,而无需使用容器访问策略。然而,两个版本的库之间存在很多差异,从旧版本迁移到新版本并非易事。几天前我写了一篇关于将代码从旧版本迁移到新版本的博客文章。您可以在这里阅读:http://gauravmantri.com/tag/storage-client-library/ 。最后,我写了一篇关于 SAS 的博客文章,您可以在这里阅读:http://gauravmantri.com/2013/02/13/revisiting-windows-azure-shared-access-signature/ .

关于azure - Windows Azure 共享访问签名始终给出 : Forbidden 403,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17558365/

相关文章:

azure - 使用 Add-AzureVhd 时出现错误 400

c# - 在 Azure 中存储媒体、视频和图像的推荐方法是什么

通过 GitHub Actions (.yml) 部署的 Azure 静态 Web 应用程序在私有(private) npm 包上出现 404 错误

azure - 如何将社交身份提供商按钮添加到 Azure B2C 中的 selfAsserted.html 页面

azure - 每次写入错误时,我是否可以触发 LogAnalytics 发送包含错误本身的电子邮件?

ruby - 在 Ruby 中生成 Azure 存储 SAS 签名

Azure静态网站: The account being accessed does not support http

azure - 将 Azure 静态网站配置为不需要尾部斜杠

Azure 存储帐户在 Data Lake Gen2 验证中停留在 0%

azure - 限制对存储帐户的访问时,无法在资源实例中选择 Azure 函数应用