none
Upload Image /video file into Windows Azure Blob in Windows 8 Modern UI app

    Question

  • Hi,

    Presently I'm working on an Win8 modern UI app which requires to upload the image file into my own Windows Azure StoreageClient(Blob).

    I searched a lot, but not finding suitable source.

    Can somebody throw light on this?

    Thanks,

    Hema

    Tuesday, September 18, 2012 3:52 AM

Answers

  • Hi HemaRao,

    You can find lots of code snippets and tutorials online for this. Here's one for uploading Blobs:

    https://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/

    How to: Programmatically access blob storage

    Add the following code namespace declarations to the top of any C# file in which you wish to programmatically access Windows Azure Storage:

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;

    You can use the CloudStorageAccount type and CloudConfigurationManager type to retrieve your storage connection string and storage account information from the Windows Azure service configuration:

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

    CloudBlobClient type allows you to retrieve objects that represent containers and blobs stored within the Blob Storage Service. The following code creates a CloudBlobClient object using the storage account object we retrieved above:

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    How to: Create a container

    All storage blobs reside in a container. You can use a CloudBlobClient object to get a reference to the container you want to use. You can create the container if it doesn't exist:

    // Retrieve storage account from connection string
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the blob client 
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
    // Retrieve a reference to a container 
    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    
    // Create the container if it doesn't already exist
    container.CreateIfNotExist();

    By default, the new container is private, so you must specify your storage access key (as you did above) to download blobs from this container. If you want to make the files within the container available to everyone, you can set the container to be public using the following code:

    container.SetPermissions(
        new BlobContainerPermissions { PublicAccess = 
        BlobContainerPublicAccessType.Blob });

    Anyone on the Internet can see blobs in a public container, but you can modify or delete them only if you have the appropriate access key.

    How to: Upload a blob into a container

    To upload a file to a blob, get a container reference and use it to get a blob reference. Once you have a blob reference, you can upload any stream of data to it by calling the UploadFromStream method on the blob reference. This operation will create the blob if it didn't exist, or overwrite it if it did. The below code sample shows this, and assumes that the container was already created.

    // Retrieve storage account from connection string
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the blob client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
    // Retrieve reference to a previously created container
    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    
    // Retrieve reference to a blob named "myblob"
    CloudBlob blob = container.GetBlobReference("myblob");
    
    // Create or overwrite the "myblob" blob with contents from a local file
    using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
    {
        blob.UploadFromStream(fileStream);
    }

    How to: List the blobs in a container

    To list the blobs in a container, first get a container reference. You can then use the container's ListBlobsmethod to retrieve the blobs within it. The following code demonstrates how to retrieve and output the Uriof each blob in a container:

    // Retrieve storage account from connection string
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the blob client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
    // Retrieve reference to a previously created container
    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    
    // Loop over blobs within the container and output the URI to each of them
    foreach (var blobItem in container.ListBlobs())
    {
        Console.WriteLine(blobItem.Uri);
    }

    Hope this helps!


    Best Regards,
    Carlos Sardo

    Tuesday, September 18, 2012 8:50 AM
  • Hi Hema - you're correct - the existing library won't work from Win 8 Modern UI clients.  While we're working on making this available, your best option is to use the REST API, documented here: http://msdn.microsoft.com/en-us/library/windowsazure/dd179355.aspx.


    -Jeff

    Wednesday, September 19, 2012 12:17 AM
    Moderator

All replies

  • Hi HemaRao,

    You can find lots of code snippets and tutorials online for this. Here's one for uploading Blobs:

    https://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/

    How to: Programmatically access blob storage

    Add the following code namespace declarations to the top of any C# file in which you wish to programmatically access Windows Azure Storage:

    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;

    You can use the CloudStorageAccount type and CloudConfigurationManager type to retrieve your storage connection string and storage account information from the Windows Azure service configuration:

    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));

    CloudBlobClient type allows you to retrieve objects that represent containers and blobs stored within the Blob Storage Service. The following code creates a CloudBlobClient object using the storage account object we retrieved above:

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

    How to: Create a container

    All storage blobs reside in a container. You can use a CloudBlobClient object to get a reference to the container you want to use. You can create the container if it doesn't exist:

    // Retrieve storage account from connection string
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the blob client 
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
    // Retrieve a reference to a container 
    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    
    // Create the container if it doesn't already exist
    container.CreateIfNotExist();

    By default, the new container is private, so you must specify your storage access key (as you did above) to download blobs from this container. If you want to make the files within the container available to everyone, you can set the container to be public using the following code:

    container.SetPermissions(
        new BlobContainerPermissions { PublicAccess = 
        BlobContainerPublicAccessType.Blob });

    Anyone on the Internet can see blobs in a public container, but you can modify or delete them only if you have the appropriate access key.

    How to: Upload a blob into a container

    To upload a file to a blob, get a container reference and use it to get a blob reference. Once you have a blob reference, you can upload any stream of data to it by calling the UploadFromStream method on the blob reference. This operation will create the blob if it didn't exist, or overwrite it if it did. The below code sample shows this, and assumes that the container was already created.

    // Retrieve storage account from connection string
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the blob client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
    // Retrieve reference to a previously created container
    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    
    // Retrieve reference to a blob named "myblob"
    CloudBlob blob = container.GetBlobReference("myblob");
    
    // Create or overwrite the "myblob" blob with contents from a local file
    using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
    {
        blob.UploadFromStream(fileStream);
    }

    How to: List the blobs in a container

    To list the blobs in a container, first get a container reference. You can then use the container's ListBlobsmethod to retrieve the blobs within it. The following code demonstrates how to retrieve and output the Uriof each blob in a container:

    // Retrieve storage account from connection string
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
        CloudConfigurationManager.GetSetting("StorageConnectionString"));
    
    // Create the blob client
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
    // Retrieve reference to a previously created container
    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    
    // Loop over blobs within the container and output the URI to each of them
    foreach (var blobItem in container.ListBlobs())
    {
        Console.WriteLine(blobItem.Uri);
    }

    Hope this helps!


    Best Regards,
    Carlos Sardo

    Tuesday, September 18, 2012 8:50 AM
  • Thanks Carlos for the quick reply :)

    Actually I've gone through this code. We went in this flow: Firstly created BlobClient and provided the credential, later we tried to upload the imagefile which has been converted as byte array through method UploadFromByteArray .

    But this does not work in Win 8 Modern UI client app (previously called as metro)...

    Regrds,

    Hema


    • Edited by HemaRao Tuesday, September 18, 2012 10:46 AM
    Tuesday, September 18, 2012 10:45 AM
  • But this does not work in Win 8 Modern UI client app (previously called as metro)...


    What is the exact error/issue that you are having?

    Best Regards,
    Carlos Sardo

    Tuesday, September 18, 2012 10:51 AM
  • Hi Hema - you're correct - the existing library won't work from Win 8 Modern UI clients.  While we're working on making this available, your best option is to use the REST API, documented here: http://msdn.microsoft.com/en-us/library/windowsazure/dd179355.aspx.


    -Jeff

    Wednesday, September 19, 2012 12:17 AM
    Moderator
  • Thank you all :)

    I guess this can be solved by using Azure Mobile service which has been released just 2 days back.

    Thursday, September 27, 2012 6:13 AM
  • Actually, I'm getting a little hung up on this as well. I see ScottGu's blog post but I'm a little lost in the mechanics. So in his post it would seem that you still can't access the ClientStorage APIs on a Win8 app (the way that they are referenced in the answer above), but that you can reference them from your mobile service scripting (Update, Read, Insert, Delete). Does this mean that you need to run an update or read on your table then reroute it to the table storage (or blob storage?) Do you add those props to the table definition in your code then just reroute them every time (so that they can exist in your 'item' variable in your script)?

    Does that mean that I need to create the container and everything else up in the insert function in the script?

    Any help here would be wonderful.

    Thursday, November 1, 2012 5:36 PM
  • I was having the same issues, refer to this post:

    http://social.msdn.microsoft.com/Forums/en-US/azuremobile/thread/830d3242-e45f-4e67-b81f-02c7aa106990

    The REST API is the way to go.

    Friday, November 2, 2012 4:20 PM
  • Please find more information about the CTP release of our Windows Runtime library at  http://blogs.msdn.com/b/windowsazurestorage/archive/2012/11/05/windows-azure-storage-client-library-for-windows-runtime.aspx
    Monday, November 12, 2012 9:00 PM