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, CICD, Cloud Computing, VSTS

How to use Visual Studio Team Services to do continuous Integration and continuous delivery: Part 7

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.

7. How to use Visual Studio Team Service to do continuous Integration and continuous delivery

In this post, we will be using Visual Studio Team Services as our source code management, if you do not know what the VSTS is, then a short description is that it is a free service which enable source code management, automate and simplify your azure deployment. It also support continuous integration and various tools such as Git, package management, TFVC, Reporting and testing. It requires you to have an account for VSTS, if you do not have, you can start free. Free version can be used up to 5 users, you can check your options at following link

Once you have the account setup, go to http://my.visualstudio.com/ and click on Get Started under Visual Studio Team Services. We will use VSTS to make our Build server and to configure continuous integration and continuous delivery, of course there are a lot of features in the VSTS but we will only focus on Git and continuous Integration and continuous delivery.

Figure 1 Get Started with Visual Studio Team Services

Microsoft will take you to landing page, here you can create an account if you have not created already and will list down your accounts, select an account to proceed.

Figure 2 Create Visual Studio Team Services Account

Once you go to the dashboard of the VSTS, click on create new Project, fill the form and click Create. Remember to choose Git as version control. If you are planning to use TFS as version control, it is totally possible and steps to enable continuous integration and delivery are the similar.

Figure 3 Create Project in VSTS

Once the project is created you will be presented with some useful information to get started,but in our case, since we already have a source code so we will use visual studio to upload our source code to this project.

Figure 4 Dashboard after new git project is created

Open up the visual studio and load your project if you have not opened it already. Now Go to Tools | Options | Source Control | and make sure that Git is selected in Current source control plug-in drop down. If you use TFS then make sure you have selected TFS while you created your project in VSTS as source control.

Figure 5 Select Git as plugin

We have opened the project and are ready to check in our source code. Now right click on Solution and click Add to Source control, or at the very bottom right of the visual studio click Add to Source control. You will see two options one is with Visual Studio Team services and other once is to connect with GitHub. Since we are using VSTS, click on connect under Visual Studio Team Services. In this process visual studio will create a master branch on local repository for you and also configure security and folders that needs to be uploaded and will also put rules to ignore folders like bin, debug and obj, plus it will ignore any user specific settings such is publishing profiles etc. Visual makes it easy to work with different source control providers and make it easy to handle security of your source code by excluding some files which are developer specific or machine specific.

Figure 6 Configure Visual Studio Team Services as source control

Now if you are not logged in, go ahead and login, choose the visual studio account | select project and click on connect and then click on clone, as you can see that it will clone the project from local repository to the online repository.

Now re-open the project and right click on solution and choose commit, enter a message and then choose Commit staged. Then click on sync to server option. You can also click on upward arrow button, which is at the very bottom and it will present the same window or you can click on team explorer to get to the same menu. Now click on Publish Git Repo.

Now select your account and then click advanced and select you team project and click Publish repository. Once your source code is published it is time to enable the continuous integration and continuous delivery in VSTS.

Figure 7 Publish code to the git repository

Go back to the dashboard of your project in VSTS, you will see now that it have changed the view and now showing, set up Build button, You can also access this Set up Build, by going to Build and Release tab and then click on setup a new definition.

Figure 8 Continuous Integration set up

Choose Azure Web App template under Featured tab, and click apply.

Figure 9 New Definition Azure Web App

Now choose your subscription and click authorize complete the process, choose app service name, choose hosted agent as hosted VS2017. Click on Get Sources under Process tab.

Figure 10 Configure build Process

You can see that is already selected the Repository and branch, you can change these settings if you want to configure any other source i.e. GitHub. Go through all other tabs just to see what is in there; you do not need to change anything. Keep in mind this will deploy to the main App Service, you can also change the settings to deploy it on any slot you have in the App Service.

Figure 11 Repository configuration

Now click on Triggers tab and check Enable continuous integration, this option will automate our build when you check in the code.

Figure 12 Enable continuous Integration

Save the settings and click on Queue, or click on Save & queue option. We will manually trigger the build process to see what is the process, and will also check in the code in visual studio to aromatically trigger the build which will deploy the source code to our App Service.

Figure 13 Queue the changes to App service.

Now click on builds and select your application, you will see that the Build is successfully deployed.

Figure 14 successfully deployed the build

Click on link under recently completed to see more information. Now it is time to test out continuous integration next.

Figure 15 recently run Build

Go to VS2017 | Views | Home | Index.cshtml and change it like below and right click on project solution and chose commit | choose commit all | sync | in outgoing commit click push

<div
class=”jumbotron”>


<h1>ASP.NET</h1>


<p
class=”lead”>@WebConfigurationManager.AppSettings[“WelcomeMsg”]</p>


<p
class=”lead”>We have implemented continuous integration.</p>


<p><a
href=”https://asp.net&#8221;
class=”btn btn-primary btn-lg”>Learn more &raquo;</a></p>

</div>

Figure 16 Commit Changes

Figure 17 sync the changes

Figure 18 Push changes for continuous Integration to trigger.

Once the push is clicked, you will see that a new build task is added in queue and is in progress.

Figure 19 New build job is in progress

Once the build is successful, refresh you site and you will find that new changes have been applied.

Figure 20 build successful

If you want to know what exactly was deployed you can actually see the files and can even download them as well, click on any of the build in above screen and the click on artifacts tabs and then explore the drop folder. In this folder you will find the zip file, which contains the files that were deployed to the App Service, you also have parameters and commands, it could be useful if in some case you need to replicate the things on local system, you just need to download the artifacts and configure them locally. Now let us see the result on actual site after our recent continuous integration deployment.

Figure 21 Artifacts Explorer

Before changes

Figure 22 before changes applied

After changes applied through continuous integration.

Figure 23 after changes applied through continuous integration

Remember that this was a build definition that we created there is another robust definition called release definition which have many scenarios that may suit your needs.

Figure 24 Release Definitions

Congratulations! You have successfully completed the full life cycle of Azure development, I hope it will help you in setting up development environment and carry on the Azure development. Please go through site you may find other series that may interest you. Good Luck!

coming up next Azure storage data services types and how to store files in azure storage account 1/2