Chr*_*ian 2 azure azure-storage azure-storage-blobs
我试图获得单个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);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在调用GetResponse时一直收到403错误.任何帮助赞赏!
编辑:忘了提:我使用的是最新的azure sdk(2.0)
编辑2:我做了很多实验,找到了一个名为Azure Management Studio的工具.该工具可以创建SAS令牌.我这样做了,并将它与我的REST调用代码一起使用.这工作得很好,所以错误必须在我编写的令牌创建代码中.但是,sas字符串的格式完全相同.我不知道还有什么可以尝试的
我注意到的一些事情:
Microsoft.WindowsAzure.StorageClient或是,请查看您的代码Microsoft.WindowsAzure.Storage.如果它是前者,那么你正在使用旧图书馆.AuthenticationErrorDetail节点下:没有签名标识符的访问不能超过1小时的时间窗口
您可以尝试创建一个有效期不到1小时的SAS令牌吗?例如
var sasToken = blob.GetSharedAccessSignature(new SharedAccessPolicy
{
Permissions = SharedAccessPermission.Read,
SharedAccessExpiryTime = DateTime.UtcNow + TimeSpan.FromMinutes(30)
}
);
Run Code Online (Sandbox Code Playgroud)
如果您仍然想要使用旧的存储客户端库,那么您有以下几种选择:
如果使用新的存储客户端库,则可以在不使用容器访问策略的情况下定义更长持续时间的令牌.但是,两个版本的库之间存在很多差异,从旧版本迁移到较新版本并非易事.几天前我写了一篇关于将代码从旧版本迁移到更新版本的博客文章.你可以在这里阅读:http://gauravmantri.com/tag/storage-client-library/.最后,我写了一篇关于SAS的博客文章,你可以在这里阅读:http://gauravmantri.com/2013/02/13/revisiting-windows-azure-shared-access-signature/.
| 归档时间: |
|
| 查看次数: |
2738 次 |
| 最近记录: |