Introduction to Azure Storage Service
Azure Storage is Microsoft's modern storage solution. If you compare the previous storage techniques with Azure Storage Services, you will find a lot of differences in security, availability, and accessibility.
Here, we will discuss all these Azure Storage benefits and will work on Blob Storage service.
- Durable and Highly Available
Your data in Azure service is distributed over geographical data center and will be available to the users in case of any system, software or hardware Failure. This redundancy ensures that the data is safe and can be accessible even in case of disaster.
- Secure
All the data written to the Azure Storage is encrypted by services.
- Highly Scalable
You can scale up horizontally or vertically as per your requirement.
- Accessible
Data in Azure storage can be accessible from everywhere in the world using HTTP or HTTPS protocol. Microsoft provides SDKs for Azure Storage in various languages.
There are mainly 4 types of standard storage services in Azure,
- BLOB
- Table
- Queues
- File storage
Here in this session, we will work with blob storage.
- Blob storage is mainly for storing a massive amount of unstructured data such as text or binary data.
- Blob storage is ideally for Images, audio, video, etc.
- Its also used for stream Audio and Video.
- You can also store backup files.
- Objects in the blob can be accessed from anywhere in the world using HTTP or HTTPS protocol.
- Blob data is stored in a container, so we can use a large number of containers for storing a large number of the blob data. A container provides the security from the outer world to protect our data.
Now, let's start working with a Azure Blob storage. To start working with Blob, we need to create a storage account like this,
Access Tier
- Hot: Hot storage has higher storage costs than cool storage, but the lowest access costs.
- Cool:Cool storage tier has lower storage costs and higher access costs compared to hot storage.
Once you create an account ,then you need to create a container. Here is how you can create a container.
Once the container is ready, you can upload your files as blob.
Now let's create an MVC application and save image files to Blob.
Here is my MVC view,
- @{
- ViewBag.Title = "Home Page";
- }
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
-
- <script type="text/javascript">
- function show(input) {
- if (input.files && input.files[0]) {
- var filerdr = new FileReader();
- filerdr.onload = function (e) {
- $('#user_img').attr('src', e.target.result);
- }
- filerdr.readAsDataURL(input.files[0]);
- }
- }
- </script>
-
-
- <div class="jumbotron">
-
- @using (Html.BeginForm("Index", "Home", FormMethod.Post, new
- {
- enctype = "multipart/form-data"
- }))
- {
- <div class="form-group">
- <div class="col-md-10">
- <label>Upload Your Image</label>
- </div>
- <br />
- <img id = "user_img" height = "300" width = "500" style = "border:solid" / >
- <br />
- <div class="col-md-10">
- <input type="file" name="image" accept="image/*" class="form-control fileupload" onchange="show(this)" />
- </div>
- <div class="col-md-10">
- <input type="submit" title="save" />
- </div>
-
-
- </div>
-
- <div>
- </div>
-
- }
-
- </div>
-
-
Now go to the portal container and copy the connection string and paste in web.config as follow.
Now copy the connection string and paste in the Web.config as follow.
- <connectionStrings>
- <add name="StorageConnectionString" connectionString="DefaultEndpointsProtocol=https;AccountName=myazurecontainer;AccountKey=cc1tX3yX9KrlyflrsZDzd4OGSrELk7D+aJP6ntaJokUPD6v1xAIw==;EndpointSuffix=core.windows.net" />
- </connectionStrings>
Now download a storage client to work on Azure storege service from Nuget Package.
Now here is the code in controller to save the file in Blob.The below code will read the connection string from config file.
- CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ToString());
Here is the code to save the file in Blob.
- [HttpPost]
- public ActionResult Index(HttpPostedFileBase image)
- {
-
- if (image.ContentLength>0)
- {
-
- CloudBlobClient client = storageAccount.CreateCloudBlobClient();
-
- CloudBlobContainer container = client.GetContainerReference("myazurecontainer");
-
- container.SetPermissions(new BlobContainerPermissions
- {
- PublicAccess = BlobContainerPublicAccessType.Blob
-
- });
-
- CloudBlockBlob blob = container.GetBlockBlobReference(image.FileName);
- blob.UploadFromStream(image.InputStream);
- }
- return View();
- }
Now click submit to save the file in Azure Blob.You can find the file in blob as shown below.
So in this way we can upload files in Azure Blob storage.