Azure App Service, Azure Blob, Azure Cloud, Cloud Computing

Azure storage data services types and How to store files in Azure Storage Account 2/2: Part 9

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

9. Azure storage data services types and how to store files in azure storage account 2/2

Now we will do the same upload process programmatically in visual studio, we will only focus on the relevant code, and we do not discuss any coding best practices as it is out of scope of this series, it is left on user’s own discretion.

Install WindowsAzure.Storage nugget package

Open your visual studio if you have not opened it already and open the solution with which we are working in this series. Right click on solution | Manage Nugget Packages | click on Browse | search for azure storage. Install latest stable version in our case it is 8.7.0 and accept the license. By the time you read this post, Microsoft may have released new version, so do not worry about it as all the code will still work.

Figure 5 Install Windows AZURE Storage nugget package

Open you Index controller and the code should look like below; all you need to do is modify the highlighted with your credentials to make it work.

Figure 6 Change settings for storage account container

Now to get the Blob service endpoint Go to Storage Account | Blob Service | under Primary blob service endpoint you will have the URL, copy it.

Figure 7 Blob service endpoint

For storage account and keys Go to your storage account | Access keys, although storage account can be retrieved from main overview window, but this view will give you both account name and keys in one location. Please remember that whoever have these keys, will be able to manipulate almost anything in the storage account level so you do not want to give these keys to everyone. Azure provides us SAS (shared access service) which is robust and should be used to limit access in production environments. You also have option to regenerate keys, which will invalidate the previous keys and will provide you new keys.

Figure 8 Blob service Storage account and keys

Coding in Visual Studio

In first Index method, all we are doing is listing the blob files in the container images and making a dictionary, which have image name as key and image URL as value, and we are passing it to view using ViewBag.files.

Figure 9 Get all files in container

In post Index method, we are checking for file first, if exists then we are creating reference to the blob container with file name, but you could also use GUID for your blob name. Note that if a blob/file exists in the container with particular name and you use the same name to upload the file again then existing file will be overwritten. We are also using file stream to mitigate the need to save the file on disk first, you can of course implement your logic accordingly.

Figure 10 Insert Files in container

Full code for home controller is as below:

Now let us move to the view part, it is very simple, we just added the code for form, which will post to index method of home controller and is multipart that means will contain file as post response. We create an input with type file and a submit button. Make sure that your input file name is the same as you have mentioned the parameter to post Index file, otherwise you will not get the file in response.

Figure 11 File Upload code in MVC

We also included the code to display file name and picture along with it.

Figure 12 View all images code

Full code for Index View as below:

Now run the application locally and upload a picture, you will see that the filename is encoded in html friendly name as it have spaces in it and images are shown. There are two pictures as we upload first picture right after we created storage account and the other one is uploaded programmatically.

Figure 13 Complete view of file upload

You can also verify the image upload by going to images container in storage account.

Figure 14 Image uploaded to images container

This code tutorial is just starting point; you can play with it and use this code to modify according to your needs. I hope this being information for you.

Coming up next How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger

Azure App Service, Azure Cloud, Azure Cosmos DB, Cloud Computing

How to use NoSQL Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications: Part 6

I am putting together a series for people who are excited to bring Azure into their software development life cycle and use Azure cloud’s extensive services to their full potential.

In this series, I will cover as below:

  1. Getting Started with Azure Development, Create App Service Plan and Publish MVC project using Visual Studio
  2. Deployment Slots and Slot Swap on Azure App Service using Visual Studio and Azure SDK
  3. Remote debugging App Service using visual Studio, monitoring and configuring alerts
  4. Diagnostic logs, live stream, process explorer and KUDU
  5. How to use Azure SQL Database in Dot.net Applications
  6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications
  7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery
  8. Azure storage data services types and how to store files in azure storage account 1/2
  9. Azure storage data services types and how to store files in azure storage account 2/2
  10. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 1/2
  11. How to use Azure Functions and trigger on new image/blob creation in Azure Storage using BlobTrigger 2/2

If you do not know where to start, please check my blog post, which covers detail about getting subscription and setting up. In this series, we assume that you already have active Azure subscription and Visual Studio 2013 or later installed on your system.

6. How to use Azure DocumentDB or Azure Cosmos DB in our Dot.net Applications

Azure DocumentDB or Azure Cosmos DB is database for very low latency and immensely saleable applications, which have global audience and it provides native NOSQL support. Azure cosmos DB support storing and retrieving JSON object at scale.

During development you do not need to create a new instance of documentDB, visual studio provide you the emulator to use during development phase. To explore the possibilities, In our case, we will create Azure Cosmos DB / documentDB on azure and use it in our application that we are building in this series.

In Azure first we need to create an account for Azure Cosmos DB and then we can create collections and databases in it. Let us do just that.

Go to Azure Portal and Click on Add and search for documentDB | select Azure Cosmos DB and click on create.

Figure 1 Create Azure Cosmos DB

Now fill the form, select the API, Azure support multiple APIs like SQL API, MongoDB API, Graph API, Table API and Cassandra API, and choose the appropriate API, we are going for SQL API. This will create a new Azure Cosmos DB account for you.

For more info on APIs

Figure 2 Create Azure Cosmos DB

It may take some time to create it. Once created go to the resource | select your documentdb account | Quick start. Right now, we do not have any collection and we do not have any database. Firstly we will create new collection, and then create database in that collection, but we have a short cut to do just that, click on Create Items collection button. It will create the basic collection for you which will cost you around 0.033$ / hour and will give you 400 reads/sec. Read/sec defined as if the file is 1 kb size and 1 kb file read is represented as 1 RU  then 400 request could be read per second 400 RU/S and under 400 RU/S load, our current documentDB will work just fine. If the requests are, more the throttling will happen and latency will increase, in case of extreme throttling request timeout could happen. Writing operation will cost you more than reading, Writing the file of 1 Kb size will cost you 5 RUs as it is more expensive then read. You can check following link for more info

Figure 3 Create Items collection in Azure Cosmos DB

Click on Create “Items” collection” and then click on  download  button to get the code for TODOList sample Project, we will use this project as sample and will modify our existing project to communicate with the Azure Cosmos DB.

Now go to Collections | Browse you will see that it created new collection Items and database in it called ToDoList , all we have to do now is to integrate todo project we have downloaded, to our app that we are building in this series.

Figure 4 Azure Cosmos DB collections and database

Open the downloaded sample source code in the visual studio and run for few times and add some records. This will create some record in the database that we will see later and we will also clear some misconceptions about documentDB that many developers may have.

Figure 5 Run TodoList sample project

Open DocumentDBRepository, this is our main file, which is communicating with Azure Cosmos DB, and notice it is using , endpoint, authKey, database and collection app settings. We will copy the DocumentDBRepository.cs file and include appSettings to our exisitng application.

Figure 6 DocumentDBRepository

Make sure to install Microsoft.Azure.DocumentDB package through nugget.

Figure 7 Install Microsoft.Azure.DocumentDB

Make sure copy all the files highlighted and change _layout.cshtml file to include our Item controller also highlighted.

Figure 8 migrated the code

Also, make sure you copy below to Global.asax.cs | build  | check locally that there is no error while building or running, and publish the changes to App Service, we have already covered how to publish the source code to app service in previous posts.

DocumentDBRepository<todo.Models.Item>.Initialize();

Figure 9 Insert DocumentDBRepository initialize line

Now once your project run successfully, insert records few times by creating some tasks. Keep in mind that we can use To-do Items example to insert any JSON serializable object in the same collection even if the object do not relate with each other, DocumentDB is not limited to structure like table, you can insert any object of any type given that it is serializable to JSON or can be represented in JSON format.

Figure 10 insert few To-do items in documentDb

Sample code is also given below:

We can also verify the data is inserted through Azure Portal, Go to your Azure Cosmos DB | Collections | Document Explorer | click any item | you will see the data in document tab in JSON format.

{


“id”: “d2dc5626-e279-4390-b079-f6ba1a4af982”,


“name”: “test3”,


“description”: “test3”,


“isComplete”: false

}

Check for more information

At this point you are successfully integrated your existing project to use the Azure Cosmos DB.

Coming up next How to use Visual Studio Team Service to do continuous Integration and continuous delivery