我想获取单个blob的共享访问签名,然后使用REST API下载blob。但是,我总是得到一个禁止的403错误消息。无论是在存储模拟器还是云端。这里是我的代码:Windows Azure共享访问签名总是给出:Forbidden 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错误。任何帮助感谢!
编辑:忘了提:我使用的是最新的蔚蓝SDK(2.0)
编辑2:我尝试了很多,发现了一个名为Azure的管理Studio工具。该工具能够创建SAS令牌。我做到了,并将其与我的REST调用代码一起使用。这工作得很好,所以错误必须在我写的令牌创建代码中。但是,sas字符串的格式完全相同。我不知道还有什么尝试
你能提供一个你会使用的blob和容器名称的例子吗? –
容器名称:“cname” blob名称:“bname” – Christian
查看我的一些代码,我使用:blob.Uri.AbsoluteUri + sasToken作为url。 –