Created
November 13, 2012 01:50
-
-
Save nickharris/4063407 to your computer and use it in GitHub Desktop.
Upload image direct to blob storage using SAS and the Storage Client library for Windows CTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
using Microsoft.WindowsAzure.Storage.Auth; | |
using Microsoft.WindowsAzure.Storage.Blob; | |
... | |
private async void OnTakePhotoClick(object sender, RoutedEventArgs e) | |
{ | |
//Take photo or video | |
CameraCaptureUI cameraCapture = new CameraCaptureUI(); | |
StorageFile media = await cameraCapture.CaptureFileAsync(CameraCaptureUIMode.PhotoOrVideo); | |
if (media != null) | |
{ | |
//add todo item to trigger insert operation which returns item.SAS | |
var todoItem = new TodoItem() | |
{ | |
ContainerName = "mypics", | |
ResourceName = media.Name, | |
Text = "NA", | |
}; | |
await todoTable.InsertAsync(todoItem); | |
items.Add(todoItem); | |
//Upload image direct to blob storage using SAS and the Storage Client library for Windows CTP | |
//Get a stream of the image just taken | |
using (var fileStream = await media.OpenStreamForReadAsync()) | |
{ | |
var sasUri = new Uri(todoItem.SAS); | |
//Our credential for the upload is our SAS token | |
StorageCredentials cred = new StorageCredentials(sasUri.Query.Substring(1)); | |
CloudBlobContainer container = new CloudBlobContainer(new Uri(string.Format("https://{0}/{1}", sasUri.Host, todoItem.ContainerName)), cred); | |
CloudBlockBlob blobFromSASCredential = container.GetBlockBlobReference(todoItem.ResourceName); | |
await blobFromSASCredential.UploadFromStreamAsync(fileStream.AsInputStream()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment