Microsoft Azure – Storage – Part 5 – Delete blobs with a .NET application

In the prior article of the Microsoft Azure storage series we have downloaded a file (blob) from the container. In this part we will add a method to delete a blob in the Azure storage container.

Delete a blob  in the storage container

Add the DeleteBlob method to the Azure_Helper

  • Open the Azure_Helper.cs file in the Azure_Helper project
  • Add the following method to the class
/// <summary>
/// Deletes a blob in the Azure storage
/// </summary>
/// <param name="sContainerPath"></param>
/// <param name="sBlobName"></param>
public void DeleteBlob(string sContainerPath, string sBlobName) {

    // Create the blob client.
    CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

    // Retrieve reference to a blob named "myblob.txt".
    CloudBlockBlob blockBlob = container.GetBlockBlobReference(sBlobName);

    // Delete the blob.
    blockBlob.Delete();
}

Call the Azure_Helper from the Console Application

  • Open the Program.cs file in the Azure_ConsoleApplication
  • Add the following to the end of the Main method
// Delete the blob from the Azure storage
azureHelper.DeleteBlob(sContainerPath, sBlobName);

When you are testing the application the first time you can place a break point to the line that calls the DeleteBlob method and use the Azure manager  at https://manage.windowsazure.com  to follow the creation and deletion of the blob in the Azure storage container.

Microsoft Azure – Storage – Part 3 – List the contents of the container with a .NET application

In the second part of the Azure Storage series we have developed a .Net application that can create a Microsoft Azure storage  container and upload files into it. Today we will extend the functionality of our application to list the files in the container.

List the files in the container

First let’s create a class that represents the Azure Blob. This way we can isolate our console application from the Azure Storage system, so in the future it can work with any cloud storage systems.

Add a file to the Azure_Helper project to store enumerations

  • Right click the Azure_Helper project and select Add -> Class
  • Name the class Enums
  • Add an enumeration outside of the class definition. The file should look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace nsAzure_Helper {

    public class Enums {
    }

    public enum ItemType {
        undefined
        , BlobDirectory
        , BlockBlob
        , PageBlob

    }
}

Add a class to the Azure_Helper to represent a file or directory in the cloud

  • Right click the Azure_Helper project and select Add -> Class
  • Name the class CloudItem
  • To make the class public enter the word public in front of the word class
public class CloudItem
  • Add public properties to the class. The class definition should look like this:
public class CloudItem {

    public long Length { get; set; }
    public Uri Uri { get; set; }
    public ItemType Itemtype { get; set; }

}
  • Open the Azure_Helper.cs file in the Azure_Helper project
  • Add a new method to list the content of the container
/// <summary>
/// Returns the list of cloud items in the container
/// </summary>
/// <param name="sContainerPath"></param>
/// <returns></returns>
public List<CloudItem> GetContainerItems(string sContainerPath) {

    List<CloudItem> itemList = null;

    // Create the blob client. 
    CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

    // Retrieve reference to a previously created container.
    CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

    if (null == container) {
        // No container found
        return itemList;
    }

    // Create an instance of the item list
    itemList = new List<CloudItem>();

    // Loop over items within the container and output the length and URI.
    foreach (IListBlobItem item in container.ListBlobs(null, false)) {

        // Create a new instance of the Cloud Item
        CloudItem cloudItem = new CloudItem();

        if (item.GetType() == typeof(CloudBlockBlob)) {
            // This is a Block Blob

            CloudBlockBlob blob = (CloudBlockBlob)item;

            cloudItem.Itemtype = ItemType.BlockBlob;
            cloudItem.Length = blob.Properties.Length;
            cloudItem.Uri = blob.Uri;

        } else if (item.GetType() == typeof(CloudPageBlob)) {
            // This is a Page Blob

            CloudPageBlob pageBlob = (CloudPageBlob)item;

            cloudItem.Itemtype = ItemType.PageBlob;
            cloudItem.Length = pageBlob.Properties.Length;
            cloudItem.Uri = pageBlob.Uri;

        } else if (item.GetType() == typeof(CloudBlobDirectory)) {
            // This is a Directory

            CloudBlobDirectory directory = (CloudBlobDirectory)item;

            cloudItem.Itemtype = ItemType.BlobDirectory;
            cloudItem.Uri = directory.Uri;

        }

        // Add the item to the list
        itemList.Add(cloudItem);

    }

    return itemList;

}

The method above returns a list of Cloud Items.

Let’s call the method from our Console Application

  • Open the Program.cs file in the Azure_ConsoleApplication project
  • Add the following code to the existing lines
// Get the list of items in the Container
List<CloudItem> itemList = azureHelper.GetContainerItems(sContainerPath);
if (null != itemList){

    foreach (CloudItem item in itemList){

        if (ItemType.BlockBlob == item.Itemtype) {

            Console.WriteLine("Block blob of length {0}: {1}", item.Length, item.Uri);

        } else if (ItemType.PageBlob == item.Itemtype) {

            Console.WriteLine("Page blob of length {0}: {1}", item.Length, item.Uri);

        } else if (ItemType.BlobDirectory == item.Itemtype) {

            Console.WriteLine("Directory: {0}", item.Uri);
        }

    }
}

Console.WriteLine("Press a key to exit");
Console.Read();

The Program.cs file should look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using nsAzure_Helper;

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

			// Create an instance of the Azure Helper
			Azure_Helper azureHelper = new Azure_Helper();

			string sContainerPath = "test1";
			bool bMakeContainerPublic = true;
			string sBlobName = "blob1";
			string sSourceFilePath =  @"D:\MediaFiles\Audio\Music.wav";

			// Create a public container
			azureHelper.CreateContainer(sContainerPath, bMakeContainerPublic);

			// Upload an audio file
			azureHelper.UploadFile(sContainerPath, sBlobName, sSourceFilePath);

			// Get the list of items in the Container
			List itemList = azureHelper.GetContainerItems(sContainerPath);
			if (null != itemList){

				foreach (CloudItem item in itemList){

					if (ItemType.BlockBlob == item.Itemtype) {

						Console.WriteLine("Block blob of length {0}: {1}", item.Length, item.Uri);

					} else if (ItemType.PageBlob == item.Itemtype) {

						Console.WriteLine("Page blob of length {0}: {1}", item.Length, item.Uri);

					} else if (ItemType.BlobDirectory == item.Itemtype) {

						Console.WriteLine("Directory: {0}", item.Uri);
					}

				}
			}

			Console.WriteLine("Press a key to exit");
			Console.Read();

		}

	}
}

In the next article we will learn how to download files from the Microsoft Azure storage.

Microsoft Azure – Storage – Part 2 – Upload files with a .NET application


In the first part of the Azure Storage series we have created a Microsoft Azure storage in the “cloud”. Now we will create a C# application to upload, list and download files.

Create a Blank  Solution

  • In the File menu select New -> Project

visual studio blank solution

Add a new Console Application project to the solution

  • In the Solution Explorer right click the solution and select Add -> New Project
  • Name the project Azure_ConsoleApplication

visual studio console application

Add  a Class Library project to the solution

  • In the Solution Explorer right click the solution and select Add -> New Project
  • Name the project Azure_Helper

visual studio class library

Set the namespace of the helper project to nsAzure_Helper

When you create a new project Microsoft Visual Studio sets the default namespace to the same as name of the project. In .NET you cannot create a class with the same name as the name space of its project. For clarity I usually name the main class of the project after the name of the project, so I have to change the default namespace of the project.

  • Right click the Azure_Helper project and select Properties
  • On the left side select the Application tab
  • Set the Default namespace to nsAzure_Helper
  • Press CTRL-S on the keyboard to save the project file
    azure set the middletier namespace

Add a new class to the Azure_Helper class library

  • In the Solution Explorer delete automatically created the Class1.cs file
  • Right click the Azure_Helper project and select Add -> Class
  • Name the new class Azure_Helper and click the Add button
    azure middletier add new class

Add an Application Configuration File to the Console Application to store the Azure connection string

  • In the Solution Explorer right click the console application and select Add -> New Item

visual studio app config

  • When the new App.config file opens add lines to it, so it looks like this:
<configuration>
  <appSettings>
      <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=[AccountName];AccountKey=[AccountKey]" />
  </appSettings>
</configuration>

Where
[AccountName] is the Storage Account Name
[AccountKey] is the Primary key of the storage account

Add the Azure framework to the class library

  • Right click the class library and select Manage NuGet Packages…
  • In the upper right corner enter WindowsAzure.Storage
  • Select the Windows Azure Storage package and click Install

azure nuget windows azure storage

  • In the pop-up window click I Accept to download the rest of the necessary libraries.

azure nuget windows azure storage dependencies

Connect to the Azure storage

  • Open the Azure_Helper class in the Azure_Helper project
  • Add using statements to the Azure_Helper class to reference the Azure SDK


using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

Add a reference to the System.Configuration assembly to access the App.config file

  • In the Solution Explorer right click the References folder of the Azure_Helper project and select Add Reference
  • Select Assemblies, Framework, System.Configuration and click the OK button
    visual studio add reference system configuration
  • Add a using statement to the Azure_Helper class to access the app.config file

using System.Configuration;

  • Add an instance variable to the Azure_Helper class to get the connection string from the config file


CloudStorageAccount _storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

Your Azure_Helper class should look like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;

using System.Configuration;

namespace nsAzure_Helper
{
    public class Azure_Helper
    {
        // Retrieve storage account from connection string.
        CloudStorageAccount _storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);

    }
}

Create a storage container

Add a new method to the Azure_Helper class

  • Add a method to your Azure_Helper class to create a container in the Azure Storage account
public void CreateContainer(string sContainerPath) {

	// Create the blob client.
	CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

	// Retrieve a reference to a container. 
	CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

	// Create the container if it doesn't already exist.
	container.CreateIfNotExists();

}

Call the CreateContainer method

Add a reference to the middle tier

  • In the Solution Explorer right click the References folder of the console application project and select Add Reference
  • On the left side of the pop-up window click Solution, Projects, and select to the middle tier project
    azure add reference to middletier

Open the Program.cs file of the console application

  • Add a using statement to the Program.cs class
using nsAzure_Helper;
  • Instantiate the Azure Halper, call the CreateContainer method. Your Program.cs class should look like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using nsAzure_Helper;

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

            // Create an instance of the midle tier
            Azure_Helper azureHelper = new Azure_Helper();

            string sContainerPath = "test1";
            string sBlobName = "blob1";

            // Create a container
            azureHelper.CreateContainer(sContainerPath);

        }
    }
}

Test the application

When you run the application the “test1” container should be created. To verify the result open the Windows Azure management console at https://manage.windowsazure.com/#Workspaces/All/dashboard

  • On the left side select All Items
  • Click the storage account
    azure select storage account
  • Click the Containers link
    azure select containers
  • Click the name of the container to view the files
    azure container list
  • We just created the container, so it is empty
    azure empty container

 

 

You can also use any Azure management tools. The first part of the Azure Storage series contains a link to a list of Azure management tools.

 

Add an option to make the container public

When you create the container only the account owner can access the files in the container. We will add a boolean argument to the CreateContainer method, so the caller of the method can make the container public. Anyone on the Internet can see the public files, but you need the access key to modify and delete them.

  • Change the signature of the CreateContainer method in the Azure_Helper class to
    public void CreateContainer(string sContainerPath, bool bMakeItPublic) {
  • Add the following lines to the end of the method before the closing curly bracket
    // Make the container public if requested
    if (bMakeItPublic) {
    
    	container.SetPermissions( new BlobContainerPermissions { PublicAccess =	BlobContainerPublicAccessType.Blob});
    
    }

The CreateContainer method should look like this

public void CreateContainer(string sContainerPath, bool bMakeItPublic) {

	// Create the blob client.
	CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

	// Retrieve a reference to a container. 
	CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

	// Create the container if it doesn't already exist.
	container.CreateIfNotExists();

	// Make the container public if requested
	if (bMakeItPublic) {

		container.SetPermissions( new BlobContainerPermissions { PublicAccess =	BlobContainerPublicAccessType.Blob});

	}

}

Change the call to the method in the Program.cs class of the Azure_ConsoleApplication project

  • Open the Program.cs file in the Azure_ConsoleApplication project
  • Add true as  the second attribute to the call to the CreateContainer method to create a public container
// Create a public container
azureHelper.CreateContainer(sContainerPath, true);

  Upload a file to the container

The files in the container are called blobs (binary large objects), because those can be any type of files: documents, pictures, videos, music. There are two methods to store these blobs: as blocks or pages. in this example we will store our files a block blobs.

 

Create a new method in the Azure_Helper class:

public void UploadFile(string sContainerPath, string sBlobName, string sSourceFilePath) {

	// Create the blob client.
	CloudBlobClient blobClient = _storageAccount.CreateCloudBlobClient();

	// Retrieve reference to a previously created container.
	CloudBlobContainer container = blobClient.GetContainerReference(sContainerPath);

	// Retrieve reference to a blob named "myblob".
	CloudBlockBlob blockBlob = container.GetBlockBlobReference(sBlobName);

	// Create or overwrite the "myblob" blob with contents from a local file.
	using (var fileStream = System.IO.File.OpenRead(sSourceFilePath)) {
		blockBlob.UploadFromStream(fileStream);
	}

}

  Call the UploadFile method from the console application

  • Open the Program.cs file in the console application and modify the Main method to look like this
    static void Main(string[] args) {
    
        // Create an instance of the Azure Helper
        Azure_Helper azureHelper = new Azure_Helper();
    
        string sContainerPath = "test1";
        bool bMakeContainerPublic = true;
        string sBlobName = "blob1";
        string sSourceFilePath =  @"D:\MediaFiles\Audio\Music.wav";
    
        // Create a public container
        azureHelper.CreateContainer(sContainerPath, bMakeContainerPublic);
    
        // Upload an audio file
        azureHelper.UploadFile(sContainerPath, sBlobName, sSourceFilePath);
    
    }

 Test the application

If you run the application now the file should be uploaded to the Azure storage.

  • Refresh the Windows Azure management page to see the blob in the container
    azure container list of blobs

In the next article we will extend our application to list the contents of the Container.

Microsoft Azure – Storage – Part 1 – Getting started

Sign up for Microsoft Azure

To create a Microsoft Azure storage account first create a Microsoft Azure account. If you are an MSDN subscriber you may open an account without using a credit card and limit your usage to the MSDN provided $150 free monthly allowance.

Once you created your Microsoft Azure account open the Azure Management Console at

https://manage.windowsazure.com/#Workspaces/All/dashboard

Create a storage account

  • On the left side of the page select Storage
  • Click Create a storage account
  • Enter the first part of the URL for your storage
  • Select the region where you want to create the storage
  • If you want to save money for your test environment uncheck Enable Geo-Replication
  • Click the Create storage account at the bottom

Get the Storage Access Keys

To access your storage area you need to use the 512 bit storage access keys. Azure already generated them:

  • In the Azure Management Portal select All Items on the top of the left side
  • Select the storage account you want to administer
  • At the bottom of the page click Manage Access Keys
    azure storage access keys
  • Copy the primary and secondary key from the pop-up window

 

 

 Get a Cloud Storage Management tool

Microsoft did not develop a management tool for its cloud storage solution, so you need to shop around an find a tool you trust. The MSDN web site contains an outdated list of Windows Azure Storage Explorers at

http://blogs.msdn.com/b/windowsazurestorage/archive/2010/04/17/windows-azure-storage-explorers.aspx

One new tool is Cloud Combine. It has a 30 day free trial period and a $9.99 price with unlimited lifetime usage and updates. You can get it at http://www.gapotchenko.com/cloudcombin This tool can also manage your Amazon S3 storage account.

 

In the next article we will create a .Net application to access the Azure Storage.

 

 

 

Display deleted items in the Microsoft Visual Studio Source Control Explorer

When you delete a file or folder in TFS by default it is no longer displayed in the Source Control Explorer. To be able to “undelete” a file or folder enable the visibility of deleted items in Microsoft Visual Studio.

To see deleted items in the Source Control Explorer

  • Start Visual Studio
  • In the Tools menu select Options
  • On the left side expand Source Control
  • On the left side select Visual Studio Team Foundation Server
  • On the right side check Show deleted items in the Source Control Explorer
  • Click OK to save your changes

visual studio tfs show deleted items in source control explorer

To “undelete” a file or folder

  • Start Microsoft Visual Studio
  • Open the Source Control Explorer window
    • In the View menu select Other Windows
    • In the list select Source Control Explorer

In the Source Control Explorer window navigate to the location of the deleted item. Deleted files and folders are displayed in gray with a special icons.

visual studio tfs deleted folders in source control explorer

  •  Right click the item and select Undelete

 

Add if conditional statement to the Visual Studio pre-build and post-build events

There are times when you want to execute statements in the pre-build and post-build events of a Microsoft Visual Studio project when certain conditions met. In the next example we will copy the environment specific config file to the web application folder if the configuration is set to “Local”.

To edit the pre-buld and post-buld events

  • In the Visual Studio Solution Explorer right click the startup project
  • Select Properties
  • On the left side select the Build Events tab
  • To edit the script in a larger, resizable  window click the Edit Pre-Build or Edit Post-Build buttons

 

visual studio edit pre-build event

 

  • Enter the following lines into the event edit region:


if $(ConfigurationName) ==  Local (
attrib "$(ProjectDir)Web.config" -r
copy "$(ProjectDir)Config_2012\$(ConfigurationName).xml" "$(ProjectDir)Web.config"
)

The commands will be interpreted line-by-line the same way as a DOS batch file, so it is important to place the opening parenthesis “(” in the same line as the if statement.

How to add the Config folder to a Microsoft Visual Studio project even if the folder is not in the project directory

Corporations maintain multiple server environments for development, testing and production purposes.
All of these environments require specific settings to connect to the databases and other resources. When the build master builds the application for the specific environment the build process has to select the appropriate configuration and build it into the installer file for deployment.
It is a good practice to store these environment specific config files in a folder that also designates the Visual Studio version.

Console applications and Windows services

For console applications and Windows services place this folder into the “Source” folder.

visual studio add folder to project folder tree

 

In Microsoft Visual Studio you can add a folder to a project, but it will be created in the directory where the project file is located. To add a folder to a project when the folder is not in the project directory we have to manually edit the project file. In this example we will add a folder to our main project that contains the environment specific config files. The “Config_2012” folder is located in the “Source” folder outside of the “Project_2012” project directory.

The following steps will add the “Config_2012” folder to the project

  • Create the “Config_2012” folder in Windows Explorer
  • Add the environment specific config files to it
  • Add the folder to source control
  • Open the solution in Visual Studio
  • Right click the main project and select Unload Project
  • Right click the main project again and select Edit…
  • Add the following to the project file

<ItemGroup>
<None Include="..\Source\Config_2012\Dev.config">
<Link>Config_2012\Dev.config</Link>
</None>
<None Include="..\Source\Config_2012\Local.config">
<Link>Config_2012\Local.config</Link>
</None>
<None Include="..\Source\Config_2012\Prod.config">
<Link>Config_2012\Prod.config</Link>
</None>
<None Include="..\Source\Config_2012\QA.config">
<Link>Config_2012\QA.config</Link>
</None>
<None Include="..\Source\Config_2012\Staging.config">
<Link>Config_2012\Staging.config</Link>
</None>
<None Include="..\Source\Config_2012\UAT.config">
<Link>Config_2012\UAT.config</Link>
</None>
</ItemGroup>

 

  • Save the project file
  • Right click the main project file and select Reload Project

The “Config_2012” folder will appear in the Solution Explorer and the config files will be added as linked files.

visual studio add folder to project

Web applications and web services

For web applications and web services place the Config folder into the web application folder

web application folder structure config_2012 folder highlighted

To add the config folder and files to the web application

  • Open the web solution in Visual Studio
  • Select the web project
  • Click the Show All Files button on the Solution Explorer toolbar

visual studio show all files button

 

  • The Solution Explorer will display all files in the web application folder

web application solution explorer show all files config_2012 folder and files

 

  • Right click the Config_2012 folder  and select Include In Project

web application solution explorer show all files config_2012 folder include in project

 

  • Select all the config files in the Config_2012 folder
  • Right click the selection and select Properties

web application solution explorer show all files config_2012 folder files properties

 

  • Set the Build Action to None

web application file properties build action none

 

  • Click the Show All Files button on the Solution Explorer toolbar again to hide the excluded files

visual studio show all files button

 

Microsoft Visual Studio editor settings

There are some settings that can make your work easier in Visual Studio.

Start Microsoft Visual Studio, and in the “Tools” menu select “Options”

– To insert new tabs to the right of existing tabs and keep the already opened tabs in place and
– Show pinned tabs in a separate row

  • On the left side select “Environment”, “Tabs and Windows”
  • Check “Insert new tabs to the right of existing tabs” and “Show pinned tabs in a separate row”

visual studio options tabs

 

– To use tabs instead of spaces in edited files

  • On the left side select “Text Editor”, “All Languages”, “Tabs”
  • Select “Smart” and “Keep tabs”

visual studio options tab settings

 

How to set up Internet Explorer to debug with Microsoft Visual Studio

When you debug your Visual Studio web application you want to start and stop Internet Explorer automatically when you start and stop the debugging session. With the default settings Internet Explorer starts automatically when you start debugging, but it does not close when you stop the execution.

To enable the automatic stop, enable debugging in Internet Explorer:

  • Start Internet Explorer,
  • On the upper right corner click the gear icon to open the Tools menu,
  • Select “Internet options”,

internet explorer internet options select

 

  • On the “Advanced” tab of the Internet options window uncheck “Disable script debugging (Internet Explorer)”
  • Click “OK”.

internet explorer debug enabled

 

Next time when you debug in Microsoft Visual Studio Internet Explorer will start and stop with the debugging session. When you close Internet Explorer it will stop the debugging session in Visual Studio.