spaceduk
spaceduk

Reputation: 115

Azure Storage private container blob to MemoryStream

I'm running around in circles trying to work out the code to download a file from an Azure Storage private container into a MemoryStream.

I have this so far...

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);

It errors on the last line with...

The remote server returned an error: (404) Not Found.

However, it will work without error when the container is not private.

Any help much appreciated, thank you.

Upvotes: 3

Views: 4934

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136346

Please try the following code:

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);

Since the container has Private ACL, the request needs to be authenticated. Using this constructor of CloudBlockBlob takes care of that.

Upvotes: 12

Related Questions