【发布时间】:2013-07-09 21:15:19
【问题描述】:
我正在尝试获取单个 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 令牌。我这样做了,并将它与我的 REST 调用代码一起使用。这工作得很好,所以错误必须在我编写的令牌创建代码中。但是,sas 字符串的格式是完全一样的。我不知道还有什么可以尝试的
【问题讨论】:
-
您能否提供一个您将使用的 blob 和容器名称的示例?
-
容器名称:“cname” blob 名称:“bname”
-
查看我的一些代码,我使用:blob.Uri.AbsoluteUri + sasToken 作为 url。
-
在实际使用 sas 令牌之前,是否可能需要将令牌创建提交到 blob 存储?
-
我不这么认为,它只是你拥有的代码 (blob.Uri) 将真的是 blob.Uri.ToString() 可能与 blob.Uri.AbsoluteUri 不同
标签: azure azure-storage azure-blob-storage