locked
Implement file upload using blob storage RRS feed

  • Question

  • Hi All,

    one of the functionality of my app is uploading files.

    To make it work on azure, i have to use blob(block) storage. I have created POC by referring link. But can i do something like creating own directory structure(\uploadedfiles\users\profilephoto\) on blob storage as we are doing on file server ?

    can anyone has done this before ? can anyone provide me examples/links ?

    BR,

    Jonyy

    Monday, December 3, 2012 1:14 PM

Answers

  • Hi,

    The directry structure is simulated in blob storage, so you will see the full blob names, kust as you see the full file path when using System.IO:File objects. You will have to split the string to get the "file name" from the blob name.

    Regards,

    Alan


    Free e-book: Windows Azure Service Bus Developer Guide.

    Tuesday, December 4, 2012 10:28 AM

All replies

  • Hi,

    You can simulate a hierarchical structure in a Blob container by giving the blob a name that includs the paths:

    files\photos\me.jpg

    You can also query the blob hirarchy by listing blobs starting with the appropreate path.

    Regards,

    Alan


    Free e-book: Windows Azure Service Bus Developer Guide.

    Monday, December 3, 2012 1:24 PM
  • i tried this example.

    but when i saw output, its not showing directory structure. it is showing as blob name. please have a look at below snap:

    wwhat is the problem ?

    Tuesday, December 4, 2012 7:19 AM
  • Hi, You will have to use the storage API to list the blobs hirarchicaly, there is a code example here: http://msdn.microsoft.com/en-us/library/windowsazure/hh674669.aspx Regards, Alan

    Free e-book: Windows Azure Service Bus Developer Guide.

    Tuesday, December 4, 2012 8:56 AM
  • Hi Alan,

    thanks for quick response. i have tried that too. following is my code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.WindowsAzure;
    using Microsoft.WindowsAzure.StorageClient;
    using System.Configuration;
    using Microsoft.WindowsAzure.ServiceRuntime;

    namespace BlobStorage
    {
        public partial class _Default : System.Web.UI.Page
        {
            CloudStorageAccount objStorage;
            CloudBlobClient objClient;
            CloudBlobContainer objContainer;
            CloudBlob obj;
            BlobStream blobstream;

            protected void Page_Load(object sender, EventArgs e)
            {
                //if (!IsPostBack)
                //{
                    try
                    {

                        objStorage = CloudStorageAccount.Parse("key");

                       

                        objClient = new CloudBlobClient(objStorage.BlobEndpoint, objStorage.Credentials);

                        // Get Container reference
                        objContainer = objClient.GetContainerReference("myfilescontainer");

                        // Create the container if it does not exist
                        objContainer.CreateIfNotExist();

                        objContainer.SetPermissions(new BlobContainerPermissions()
                        {
                            PublicAccess = BlobContainerPublicAccessType.Container
                        }); 

                        Label1.Text = "account created";
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = ex.Message;
                    }
                //}
            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                  
                    obj = objClient.GetBlobReference(string.Format("myfilescontainer/{0}/{1}", "upload",FileUpload1.FileName.ToString()));
                    blobstream = obj.OpenWrite();
                    blobstream.Write(FileUpload1.FileBytes, 0, FileUpload1.FileBytes.Count());
                    blobstream.Close();

                    Label1.Text = "file uploaded successfully.";
                }
                catch (Exception)
                {
                    Label1.Text = "error.";
                }


            }

            
        }
    }

    when i saw an output in Azure Web Storage Explorer(http://storageexplorer.cloudapp.net/login.aspx), it is giving me blob name like

    "http://storacc.blob.core.windows.net/myfilescontainer/upload/abc.png"

    it is not directory structure, right ? it is a blob name. can you please look at the code and let me know where i made mistake ?

    Tuesday, December 4, 2012 10:02 AM
  • Hi,

    The directry structure is simulated in blob storage, so you will see the full blob names, kust as you see the full file path when using System.IO:File objects. You will have to split the string to get the "file name" from the blob name.

    Regards,

    Alan


    Free e-book: Windows Azure Service Bus Developer Guide.

    Tuesday, December 4, 2012 10:28 AM