Posts Tagged 'Azure storage blobs'

Windows Azure Training Series: Writing to Blob Storage

My last couple posts covered the basics of Azure storage. See, Windows Azure Training Series:  Understanding Azure Storage and Windows Azure Training Series:  Creating an Azure Storage Account. Now let’s write some code.

Accessing Blob Storage

Blob storage is accessed using the CloudBlobClient class. When creating it, specify the configuration information, as shown in the last post. Notice below, a property encapsulates the creation of this class. All methods that read or write to blob storage will utilize this property.

private static CloudBlobClient cloudBlobClient{ get { CloudStorageAccount cloudStorageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); return cloudStorageAccount.CreateCloudBlobClient(); } } 

Creating a Blob Container

Before we can write a blob, we need to create a container for it. It’s just 2 lines of code as shown below. The “containerName” variable is just a string, which represents the name of the container. A container is like a folder on a hard disk.

CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); container.CreateIfNotExist(); 

Uploading Blobs to Containers

The following function writes a file to a blob container. Files can be uploaded using an html file upload control. Notice, you need to first get a reference to the container, and then use the UploadFromStream() method to write the file.

public static void CreateBlob(string containerName, HttpFileCollection files) { CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); foreach (string name in files) { var file = files[name]; string blobName = System.IO.Path.GetFileName(file.FileName); CloudBlob cloudBlob = container.GetBlobReference(blobName); cloudBlob.Metadata["rightanswer"] = file.FileName; cloudBlob.UploadFromStream(file.InputStream); } } 

Listing the Files in a Blob Container

The following code will list all the files in a container specified.

CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); return container.ListBlobs().ToList(); 

Accessing Blobs

The following code can be used to access a blob. In this case, the first blob in the container specified.

CloudBlob blob; var blobs = cloudBlobClient.GetContainerReference(containerName).ToList(); blob = blobs[0] as CloudBlob; 

Or, we can access a blob by its name as shown below.

CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); CloudBlob cloudBlob = container.GetBlobReference(blobName); 

Deleting Blobs

The following code can be used to delete a blob.

CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName); CloudBlob cloudBlob = container.GetBlobReference(blobName); cloudBlob.Delete(); 

In the next post, we’ll take a look at the code required to write to Windows Azure Table storage.

Doug Rehnstrom

To learn more about Windows Azure, check out Learning Tree’s course, Windows Azure Platform Introduction:  Programming Cloud-Based Applications.

Windows Azure Training Series: Creating an Azure Storage Account

In this blog post, I assume you already have an Azure subscription and a user account set up. If you do not, check out Windows Azure Training Series:  Understanding Subscriptions and Users. You might also like to read Windows Azure Training Series: Understanding Azure Storage for an overview of Azure storage.

Creating a new Azure storage account

To use Azure storage, you need to create a storage account. To do this, first go to the Windows Azure Platform Manage Portal and log in. Then, click on the New Storage Account button at the top left corner of the portal as shown below.

You will be prompted to enter a unique URL (or account name) for this storage account. Azure storage is always accessed using a URL based on this name, plus the type of storage you’re accessing, plus the “core.windows.net” domain. So, using the name shown below, accessing blob storage would be done using the URL, http://myuniqueaccountname.blob.core.windows.net/. To access table storage you would use the URL, http://myuniqueaccountname.table.core.windows.net/.

After entering your URL, choose a region where your storage account will be located and then click create.

Azure Storage account properties

After the account is created you can view its properties in the Properties grid on the right side of the management portal. The most important property is the primary access key. To access it, click on the View button.

The window shown below will open; it allows you to view and copy the access keys to the clipboard. You will need to add the access key to the configuration file of your Azure application.

In Visual Studio, open the ServiceDefinition.csdef file in your Azure service project, and add the configuration setting shown below.

Also, open the ServiceConfiguration.cscfg file, and set the value of that setting as shown below. Of course, you’ll use your own account name and account key.

Finally, you’ll need to add the following code to your WebRole.cs file.

Tune in next time and we’ll take a look at the code required to write to blob storage.

Doug Rehnstrom

To learn more about Windows Azure, check out Learning Tree’s course, Windows Azure Platform Introduction: Programming Cloud-Based Applications.

Windows Azure Training Series: Understanding Azure Storage

Windows Azure Storage Choices

There are a couple common ways of storing data when using Microsoft Windows Azure. One is SQL Azure, which is a cloud-based version of Microsoft SQL Server. The other is Azure storage. SQL Azure will be familiar to those who already understand relational databases. It may also make moving an application to cloud easier, if that application already uses SQL Server.

Azure storage has some advantages as well. First it is inexpensive. Azure storage costs about 15 cents per gigabytes per month, compared to $10 per gigabyte per month for SQL Azure. It can also be very large. Depending on your instance size, it can be up to 2 terabytes. It is also cross-platform, and accessed using standard internet requests.

Types of Windows Azure Storage

There are four types of Azure storage: blob storage, table storage, queue storage and Azure drives.

Blob storage is used to store binary data. This could be pictures, videos, or any other binary data.

Table storage is used to store structured data. It is similar to a database, but not relational. Table storage is a convenient way of saving business entities in an object-oriented program. In many ways it is simpler than relational storage.

Queue storage provides a simple messaging system that allows different Azure roles to communicate. For example, a user may request a report to be run using an application running in a Web role. That request could be sent to an Azure queue. Later a worker role can process the request, and then email the completed report to the user.

Azure drives allow storage to be access using standard NTFS APIs. This could be particularly useful if you have an application that already writes to a hard disk and you want to migrate it to the cloud.

Accessing Azure Storage

Azure storage can be access using a REST-based API via HTTP. This means storage can be used from any application, whether it is written in .NET, Java, Python, JavaScript or something else.

If you’re using .NET though, accessing storage is made easier using the Azure SDK. If you don’t already have it, go to this link, http://www.microsoft.com/windowsazure/windowsazure/, and then click on the “Get tools and SDK” button. You might also like to read this article, Windows Azure Training Series – Setting up a Development Environment for Free.

Once you have the SDK installed, set a reference to Microsoft.WindowsAzure.StorageClient.dll, and you’re ready to go.

In later posts, we’ll take a look at some code to write to Azure storage. In the meantime, you might like to read the prior posts in this series.

Windows Azure Training Series – Understanding Subscriptions and Users

Windows Azure Training Series – Setting up a Development Environment for Free

Windows Azure Training Series – Creating Your First Azure Project

Windows Azure Training Series – Understanding Azure Roles

Windows Azure Training Series – Deploying a Windows Azure Application

You might also like to come to Learning Tree course 2602, Windows Azure Platform Introduction: Programming Cloud-Based Applications.

Doug Rehnstrom

Windows Azure Storage, Part 1

Software applications often need to store data and applications written for Windows Azure are no exception. Windows Azure provides three types of storage: queues, blobs and tables. As we have already seen something about queues in a prior post we now turn our attention to blobs and tables. Part 1 will cover blobs and Part 2 will cover tables.
  
Note: SQL Azure (part of the Azure Platform) provides relational database capability. SQL Azure will be discussed in a later post.
  
Overview
  
Azure storage is based on REST. All storage objects are available through standard HTTP calls to a named URI. In practice, for .NET developers, though, it is probably easiest to just use the StorageClient assembly included as of version 1.1 of the Azure SDK.
  
It is worthwhile mentioning that all objects stored in Windows Azure are fault-tolerant. Behind the scenes everything is replicated three times. If a corruption is detected Azure can transparently fail-over to a replicated copy. In addition Azure makes a backup copy of our data in a separate data center. That way even if there is a catastrophic event which takes out the main data center our data remains intact. Having these automatic backups is an example of how Azure frees the developer from certain administration and operational details.
  
Blobs
  
Blobs are the simplest type of data storage. Essentially a blob is just a sequence of bytes; it can be anything the application needs it to be. It can be a photo of your mother-in-law, a word processing document, a zip archive containing the entire contents of your Megadeth music library … anything.
  
It is sometimes believed that the term BLOB is an acronym for Binary Large Object although some disagree with that. In any case the word originally came from database terminology. An Azure blob is similar to, but slightly different from that usage.
  
For example: 

  • An Azure blob can have metadata associated with it. Metadata is often useful in providing information about the data (such as the name of the person in a photo) and for searching. 
  • Azure blobs must exist in “containers”. Containers can hold one or more blobs but a container is not necessarily a database. It is useful to think about containers and blobs using the familiar (to most!) folder and file metaphor. A container is like a folder; a blob is like a file. Containers can also have associated metadata.

 So, briefly, how might some of this look in code?
  
If we use Visual Studio to create a Cloud Service project, much of the work (setting up the references, etc.) is taken care of for us.
Just make sure we have these using statements in our source file:  

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;

Then we can begin creating containers and blobs in our application.
  
As a very simplified, minimalistic example we could create a container and then create a blob in that container. In this example our blob is nothing more than a hard-coded array of bytes but, as mentioned, it could be any kind of binary data we can access in our code.
  
private void MySimpleBlobMethod()  
{  
    // create a container in the blob storage account  
    var myStorageAccount = CloudStorageAccount.FromConfigurationSetting(“DataConnectionString”);  
    var myBlobStorage =myStorageAccount.CreateCloudBlobClient();  
    var myContainer = myBlobStorage.GetContainerReference(“mycontainer”);  

    myContainer.CreateIfNotExist();  

    // upload an array of bytes into a blob in that container  
    // here the byte array is just a random sequence of bytes  
    // in real-life it would be the binary data you are interested in  
    byte[] myBytes = {255, 1, 0, 234, 6, 12, 49, 52, 97, 53};   
    var myBlob = myContainer.GetBlockBlobReference(“myBlob”);  
    myBlob.UploadByteArray(myBytes);  
}
 (Click here for demo)

In Learning Tree’s Windows Azure Course we will have hands-on exercises where you get to work with blobs in a much more realistic scenario. This simple introduction was only intended to show the bare essentials of working with Azure blobs!  

Kevin


Learning Tree Logo

Cloud Computing Training

Learning Tree offers over 210 IT training and Management courses, including Cloud Computing training.

Enter your e-mail address to follow this blog and receive notifications of new posts by e-mail.

Join 51 other followers

Follow Learning Tree on Twitter

Archives

Do you need a customized Cloud training solution delivered at your facility?

Last year Learning Tree held nearly 2,500 on-site training events worldwide. To find out more about hosting one at your location, click here for a free consultation.
Live, online training
.NET Blog

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: