Viacheslav
Viacheslav

Reputation: 151

Download image from azure blob storage in winRt

I now about method DownloadToStreamAsync in azure-sdk-winRT, but i can't implement this. How can i download image from blob storage, and save in to a local folder?

Upvotes: 0

Views: 814

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136196

You can try something like below (very bare bone implementation with no error checking):

        CloudStorageAccount account = new CloudStorageAccount(new StorageCredentials("accountname", "accountkey"), true);
        var client = account.CreateCloudBlobClient();
        var container = client.GetContainerReference("containername");
        var blob = container.GetBlockBlobReference("imagename.ext");//e.g myimage.png
        var file = await KnownFolders.PicturesLibrary.CreateFileAsync("imagename.ext", CreationCollisionOption.ReplaceExisting);
        var stream = await file.OpenAsync(FileAccessMode.ReadWrite);
        await blob.DownloadToStreamAsync(stream);
        await stream.FlushAsync();
        stream.Dispose();

A few comments though:

  • The example above writes to the "Pictures" library. You would need to ensure that your Windows 8 App has the capability defined for it.
  • Try looking at shared access signature functionality instead of going the account credentials route especially if you're allowing users of your app to download images from your storage account.

Upvotes: 2

Related Questions