locked
Uploading mp3 files from local machine to blob storage in azure account RRS feed

  • Question

  • Hi,

    I am a beginner in azure development. I want to be able to upload mp3 files from my local machine to my windows azure account.

    I have tried the following code.

     public class WebRole : RoleEntryPoint
        {
            public override bool OnStart()
            {
                // For information on handling configuration changes
                // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
                
    
                // 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.CreateIfNotExists();
    
                // Retrieve reference to a blob named "myblob".
                CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
    
                // Create or overwrite the "myblob" blob with contents from a local file.
                using (var fileStream = System.IO.File.OpenRead(@"C:\Users\xxxx\Music\yyyy\zzzz.mp3"))
                {
    
                return base.OnStart();
            }
        }

    I configured the connection string with my account name and access key.

    But the blob is not getting created in my account and upon running this code, 127.0.0.1:81 link is opening in my browser.

    Please help me with this. It would be great if anyone can provide some pointers for resources that would help with the azure development.

    Thanks in advance.


    • Edited by rfvg192 Thursday, August 15, 2013 7:51 AM
    Thursday, August 15, 2013 7:50 AM

Answers

All replies

  • I don't see the code to upload file into blob storage. All I see above is that you're reading a file from your local computer. Other than that the code looks fine to me (though I would not attempt to upload a file on role's startup but do through a web page but that's another story).

    Thursday, August 15, 2013 12:10 PM
  • I got your code and test it. Firstly, I configured connection string with my account name. And then I add the upload method in your code (“blockBlob.UploadFromStream (fileStream)”).The code like this:

    // Retrieve storage account from connection string.
    
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConection"));
    
    
                // 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.CreateIfNotExists();
    
    
                // Retrieve reference to a blob named "myblob".
    
                CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
    
    
                // Create or overwrite the "myblob" blob with contents from a local file.
    
               
    
                // using (var fileStream = System.IO.File.OpenRead(@"C:\Users\xxxx\Music\yyyy\zzzz.mp3"))
    
              
    
    
                using (var fileStream = System.IO.File.OpenRead(@"C:\Users\***\***\***.mp3"))
    
                {
    
                    blockBlob.UploadFromStream(fileStream);
    
                    
    
                }
    

    And it works fine.

    I think firstly you need make sure the connection string is right, and secondly you can try add upload method (“blockBlob.UploadFromStream (fileStream)”) in you code .

     Please refer to it:http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage/

    Thanks.  

    Friday, August 16, 2013 7:35 AM
  • Hi,

      >>  I want to be able to upload mp3 files from my local machine to my windows azure account.

    Looks like your code is incomplete. May I know what you are doing after you open the file? The provided code is not uploading the file to blob. I would like to suggest you to use the UploadFile method (http://msdn.microsoft.com/en-us/library/windowsazure/microsoft.windowsazure.storageclient.cloudblob.uploadfile.aspx). This method takes the file path as a parameter.

    Best Regards,

    Ming Xu


    Ming Xu
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, August 19, 2013 1:44 AM