Wednesday, July 6, 2016

Document Upload in Azure Cloud with ASP.Net MVC

  • Configure Azure storage connection string

To configure your connection string, open the web.config or app.config file from Solution Explorer in Visual Studio.Add below line in <appSettings> tag of your config file. Replace "your account name" with the name of your storage account, and "your account key" with your account access key:
<add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<your account name>;AccountKey=<your account key>" />
  • Parse the connection string from Config file
The Microsoft Azure Configuration Manager Library for .NET provides a class for parsing a connection string from a configuration file.
below is example that shows to retrieve a connection string from a configuration file:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
Above line will parse CloudStorageAccount from your connection string.
  • Create the Blob service client
The CloudBlobClient class enables you to retrieve containers and blobs stored in your storage. Here's one way to create the service client:
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

  • Create Containers for  Blob service Client
Below example shows how to create a container for blob service client, you can create different containers for different users by replacing your names with "<container name>" in below code and also shows how to set permissions for that perticular container :
CloudBlobContainer container = blobClient.GetContainerReference(<container name>);
if (container.CreateIfNotExists())
           container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });

For defining container name there are basic rules are there, which are as follows :
  1. Container names must start with a letter or number, and can contain only letters, numbers, and the dash (-) character.
  2. Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names.
  3. All letters in a container name must be lowercase.
  4. Container names must be from 3 through 63 characters long.
  • Create Blob into Containers
To upload a file to a block blob, get a container reference as shown above and use it to get a block blob reference. Once you have a blob reference, you can upload any stream of data to it by calling the UploadFromStream method. This operation will create the blob if it didn't previously exist, or overwrite it if it does exist.
The following example shows how to upload a blob into a container which is already created :
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

Now, you can get that files or data by using blob.Uri.AbsoluteUri, this will give you url for download or display data or files that you uploaded into
blob container. Or you can use below line of code for download stream of data which uploaded uisng  UploadFromStream function :

// Retrieve reference to a blob named .
CloudBlockBlob blockBlob = container.GetBlockBlobReference(<blob name>);
// Save blob contents to a file.
using (var fileStream = System.IO.File.OpenWrite(@"path\myfile"))
In above code, first it will retreive refernce of blob which you want to download, and then it will transfer the blob contents to a stream object and then save that stream into file which given as "@"path\myfile".
Delete blob from Container :

To delete a blob, first get a blob reference and then call the Delete method of that blob as shown below :
// Retrieve reference to a blob
CloudBlockBlob blockBlob = container.GetBlockBlobReference(<blob name>);

// Delete the blob.

No comments:

Post a Comment