Tags

, , , , , ,

Let me explain how to upload something to Azure cloud blob with a detailed progression report.
First, create a Cloud storage in Azure and name it as somegoodstorageblog1
Now create a Container in that storage called somegoodcontainer

blog2

blog3

blog4

Now open Visual Studio and create a blank console application.

We need to add Azure Storage references for the current project to continue with out main task. So, go through Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution.

There search for Azure Storage and install that package to the project.

blognuget

Now again go to Azure management portal and go to the dashboard of the previously created somegoodstorage storage and get the primary accesskey to access the storage programmatically by

blog31Now come back to Visual Studio and code like the below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
using System.Threading;

namespace Azure_Blob_Update_Console
{
    class Program
    {
        static void Main(string[] args)
        {

            StorageCredentials sc = new StorageCredentials("somegoodstorage",
                "please_paste_the_accesskey_here");

            CloudStorageAccount storageAcc = new CloudStorageAccount(sc, true);

            CloudBlobClient cbc = storageAcc.CreateCloudBlobClient();

            CloudBlobContainer cbcontainer = cbc.GetContainerReference("somegoodcontainer");
            CloudBlockBlob filetoupload= cbcontainer.GetBlockBlobReference("somegoodpicture.jpg");
            filetoupload.UploadFromFile(@"C:\VacancyAd.jpg", System.IO.FileMode.OpenOrCreate);
        }
    }
}

If you see line 6 – 9, we are calling all necessary references to the project from those lines.

After that we create a new StorageCredentails object that refers to the cloud storage. You can see we directly say its name somegoodstorage there and please don’t forget to replace the Accesskey.
Line 28 says the file that we are going to create on the Azure storage. If you miss the extension for the file or the file type, you will still get the file but without any filetypes.
Line 29 called the VacancyAd.jpg file from C:\ and upload it.
Again in Line 29, you may notice something System.IO.FileMode.OpenOrCreate this means, system should open a file if it exists; otherwise, a new file should be created. There are several FileMode enumerations are there, read them from http://msdn.microsoft.com/en-us/library/system.io.filemode%28v=vs.110%29.aspx
Thats all folks, easy nah… šŸ˜‰
Happy coding.

Advertisements