Azure存储专用容器blob到MemoryStream

时间:2015-07-22 14:37:42

标签: c# azure model-view-controller azure-storage

我正在试图计算代码,以便将文件从Azure存储专用容器下载到MemoryStream中。

到目前为止,我有这个......

StorageCredentials storageCredentials = new StorageCredentials(*my StorageAccountName*, *my StorageAccountAccessKey*);
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);

Uri blobUri = new Uri(featureFile.URL);
CloudBlockBlob blob = new CloudBlockBlob(blobUri);

MemoryStream mem = new MemoryStream();  
blob.DownloadToStream(mem);

最后一行的错误是......

  

远程服务器返回错误:(404)Not Found。

但是,当容器不是私有容器时,它将正常工作。

非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:11)

请尝试以下代码:

StorageCredentials storageCredentials = new StorageCredentials(*my StorageAccountName*, *my StorageAccountAccessKey*);
CloudStorageAccount storageAccount = new CloudStorageAccount(storageCredentials, true);

Uri blobUri = new Uri(featureFile.URL);
CloudBlockBlob blob = new CloudBlockBlob(blobUri, storageCredentials);//added storageCredentials

MemoryStream mem = new MemoryStream();  
blob.DownloadToStream(mem);

由于容器具有Private ACL,因此需要对请求进行身份验证。使用thisCloudBlockBlob构造函数可以解决这个问题。