Various Ways to Upload File(s) in Blob

Rajnilari2015
Posted by in Azure category on for Beginner level | Points: 250 | Views : 2985 red flag

Azure Blob storage is a service for storing large amounts of unstructured data.In this article we will look into various ways(at least 3 ways) of uploading files into Blob.


 Download source code for Various Ways to Upload File(s) in Blob

Introduction

Azure Blob storage is a service for storing large amounts of unstructured data.In this article we will look into various ways(at least 3 ways) of uploading files into Blob.

Create a Blob Container in the Azure Portal

Open the Azure portal(https://portal.azure.com), and then choose Storage Account

As a first step we need to create Storage Account

Refresh to list the Storage account(e.g. testrnablob)

Click on testrnablob and the below opens

Now we need to create a blob container so click on the "+" button

Click Create button.

But at present no blob is added as revealed below

Step 1: Click on the Upload button

We can upload file by clicking on the Upload button

Click that, and the Upload Blob window opens

Click on Select File icon, and choose file from the folder. We can select one file or multiple files at a time.

Once selected, it will appear as

Now, let us click on the Upload button

Final output

Step 2: Upload through C# programatically

Fire up a console application and add the below Nuget packages

Install-Package WindowsAzure.Storage

Install-Package Microsoft.WindowsAzure.ConfigurationManager

Next write the below function

/// <summary>
/// GetBlobContainerReference
/// Gets a reference to the CloudBlobContainer
/// </summary>
/// <param name="connectionString"></param>
/// <param name="containerName"></param>
/// <returns></returns>
private static CloudBlobContainer GetBlobContainerReference(string connectionString, string containerName)
{
    //Get the Microsoft Azure Storage account
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

    //Create the Blob service client.
    CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient();

    //Returns a reference to a Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer object with the specified name.
    CloudBlobContainer blobcontainer = blobclient.GetContainerReference(containerName);  
    
             
    return blobcontainer;
}

The above function will return a reference to the CloudBlobContainer object with the name specified.

Now inside the main function, let us write the below code

static void Main(string[] args)
{
    string folderPath = ConfigurationManager.AppSettings["sourceFolder"]; //file source folder
    string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString"); //blob connection string
    string containerName = ConfigurationManager.AppSettings["containerName"]; //blob container name

    //Gets the file names from the source folder
    var files = Directory.GetFiles(folderPath).ToList(); 

    //Gets a reference to the CloudBlobContainer
    var blobContainer = GetBlobContainerReference(connectionString, containerName); 

    //reads each file and push to the blob container as stream
    foreach (var file in files)
    {
        //gets a reference to the blob files (e.g. 1.txt, 1.jpg etc.)
        var blob = blobContainer.GetBlockBlobReference(Path.GetFileName(file));

        using (var fileStream = File.OpenRead(file))
        {
            //Uploads a stream to a block blob.
            blob.UploadFromStream(fileStream);
        }
    }
}

The program reads each file and uploads those to the blob container one by one as file stream. To get the Blob Connection string, go to Access key section, and get the Connection String

The app.config file looks as under

<appSettings>
    <add key="sourceFolder" value="D:\BlobSource" />    
    <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=test******;AccountKey=fwx****==;EndpointSuffix=core.windows.net" />
    <add key="containerName" value="rna-blob-container" />   
</appSettings>

The complete program is as under

using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Configuration;
using System.IO;
using System.Linq;

namespace UploadFilesToBlob
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderPath = ConfigurationManager.AppSettings["sourceFolder"]; //file source folder
            string connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString"); //blob connection string
            string containerName = ConfigurationManager.AppSettings["containerName"]; //blob container name

            //Gets the file names from the source folder
            var files = Directory.GetFiles(folderPath).ToList(); 

            //Gets a reference to the CloudBlobContainer
            var blobContainer = GetBlobContainerReference(connectionString, containerName); 

            //reads each file and push to the blob container as stream
            foreach (var file in files)
            {
                //gets a reference to the blob files (e.g. 1.txt, 1.jpg etc.)
                var blob = blobContainer.GetBlockBlobReference(Path.GetFileName(file));

                using (var fileStream = File.OpenRead(file))
                {
                    //Uploads a stream to a block blob.
                    blob.UploadFromStream(fileStream);
                }
            }
        }



        /// <summary>
        /// GetBlobContainerReference
        /// Gets a reference to the CloudBlobContainer
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="containerName"></param>
        /// <returns></returns>
        private static CloudBlobContainer GetBlobContainerReference(string connectionString, string containerName)
        {
            //Get the Microsoft Azure Storage account
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

            //Create the Blob service client.
            CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient();

            //Returns a reference to a Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer object with the specified name.
            CloudBlobContainer blobcontainer = blobclient.GetContainerReference(containerName);  
            
                     
            return blobcontainer;
        }       
    }
}

Now let us run the program.

Step 3: Upload through AzCopy Command-Line Utility

AzCopy is a Windows command-line utility designed for copying data to and from Microsoft Azure storage (e.g.Blob, File, and Table) using commands. In an earlier article, we have discussed about how to upload file using AzCopy tool.

Conclusion

In this article we looked into various ways of uploading files into Blob. Hope this will be helpful. Thanks for reading. Zipped file attached.

Page copy protected against web site content infringement by Copyscape

About the Author

Rajnilari2015
Full Name: Niladri Biswas (RNA Team)
Member Level: Platinum
Member Status: Member,Microsoft_MVP,MVP
Member Since: 3/17/2015 2:41:06 AM
Country: India
-- Thanks & Regards, RNA Team


Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)