7 min read

In this article by John Chapman and Aman Dhally, authors of the book, Automating Microsoft Azure with PowerShell, you will see that Microsoft Azure offers a variety of different services to store and retrieve data in the cloud. This includes File and Blob storage. Within Azure, each of these types of data is contained within an Azure storage account. While Azure SQL databases are also storage mechanisms, they are not part of an Azure storage account.

(For more resources related to this topic, see here.)

Azure File storage versus Azure Blob storage

In a Microsoft Azure storage account, both the Azure File storage service and the Azure Blob storage service can be used to store files. Deciding which service to use depends on the purpose of the content and who will use the content. To break down the differences and similarities between these two services, we will cover the features, structure, and common uses for each service.

Azure File storage

Azure File storage provides shared storage using the Server Message Block (SMB) protocol. This allows clients, such as Windows Explorer, to connect and browse the File storage (such as a typical network file share). In a Windows file share, clients can add directory structures and files to the share. Similar to file shares, Azure File storage is typically used within an organization and not with users outside the organization.

Azure File shares can only be mounted in Windows Explorer as a drive within virtual machines running in Azure. They cannot be mounted from computers outside of Azure.

A few common uses of Azure File storage include:

  • Sharing files between on-premise computers and Azure virtual machines
  • Storing application configuration and diagnostic files in shared location
  • Sharing documents and other files with users in the same organization but in different geographical locations

Azure Blob storage

A blob refers to a binary large object, which might not be an actual file. The Azure Blob storage service is used to store large amounts of unstructured data. This data can be accessed via HTTP or HTTPS, making it particularly useful to share large amounts of data publicly. Within an Azure storage account, blobs are stored within containers. Each container can be public or private, but it does not offer any directory structure as the File storage service does.

A few common uses of Azure Blob storage include:

  • Serving images, style sheets (CSS), and static web files for a website, much like a content delivery network
  • Streaming media
  • Backups and disaster recovery
  • Sharing files to external users

Getting the Azure storage account keys

Managing services provided by Microsoft Azure storage accounts require two pieces of information: the storage account name and an access key. While we can obtain this information from the Microsoft Azure web portal, we will do so with PowerShell.

Azure storage accounts have a primary and a secondary access key. If one of the access key is compromised, it can be regenerated without affecting the other.

To obtain the Azure storage account keys, we will use the following steps:

  1. Open Microsoft Azure PowerShell from the Start menu and connect it to an Azure subscription.
  2. Use the Get-AzureStorageKey cmdlet with the name of the storage account to retrieve the storage account key information and assign it to a variable:
    PS C:> $accountKey = Get-AzureStorageKey -
    StorageAccountName psautomation
  3. Use the Format-List cmdlet (PS C:> $accountKey | Format-List –Property Primary,Secondary) to display the Primary and Secondary access key properties. Note that we are using the PowerShell pipeline to use the Format-List cmdlet on the $accountKey variable:
    Automating Microsoft Azure with PowerShell
  4. Assign one of the keys (Primary or Secondary) to a variable for us to use:
    PS C:> $key = $accountKey.Primary

Using Azure File storage

As mentioned in the Azure File storage versus Azure Blob storage section, Azure File services act much like typical network files shares. To demonstrate Azure File services, we will first create a file share. After this, we will create a directory, upload a file, and list the files in a directory.

To complete Azure File storage tasks, we will use the following steps:

  1. In the PowerShell session from the Getting the Azure storage account keys section in which we obtained an access key, use the New-AzureStorageContext cmdlet to connect to the Azure storage account and assign it to a variable. Note that the first parameter is the name of the storage account, whereas the second parameter is the access key:
    PS C:> $context = New-AzureStorageContext psautomation $key
  2. Create a new file share using the New-AzureStorageShare cmdlet and assign it to a variable:
    PS C:> $share = New-AzureStorageShare psautomationshare –Context $context
  3. Create a new directory in the file share using the New-AzureStorageDirectory cmdlet:
    PS C:> New-AzureStorageDirectory –Share $share –Path TextFiles
  4. Before uploading a file to the newly created directory, we need to ensure that we have a file to upload. To create a sample file, we can use the Set-Content cmdlet to create a new text file:
    PS C:> Set-Content C:FilesMyFile.txt –Value "Hello"
  5. Upload a file to the newly created directory using the Set-AzureStorageFileContent cmdlet:
    PS C:> Set-AzureStorageFileContent –Share $share –Source C:
    FilesMyFile.txt –Path TextFiles
  6. Use the Get-AzureStorageFile cmdlet (PS C:> Get-AzureStorageFile –Share $share –Path TextFiles) to list the files in the directory (similar to executing the dir or ls commands), as shown in the following screenshot:
    Automating Microsoft Azure with PowerShell

Using Azure Blog storage

As mentioned in the Azure File storage versus Azure Blob storage section, Azure Blob storage can be used to store any unstructured data, including file content. Blobs are stored within containers, whereas permissions are set at the container level. The permission levels that can be assigned to a container are shown in the following table:

Permission level

Access provided

Container

This provides anonymous read access to the container and all blobs in the container. In addition, it allows anonymous users to list the blobs in the container.

Blob

This provides anonymous read access to blobs within the container. Anonymous users cannot list all of the blobs in the container.

Off

This does not provide anonymous access. It is only accessible with the Azure storage account keys.

To illustrate Azure Blob storage, we will use the following steps to create a public container, upload a file, and access the file from a web browser:

  1. In the PowerShell session from the Getting Azure storage account keys section in which we obtained an access key, use the New-AzureStorageContext cmdlet to connect to the Azure storage account and assign it to a variable. Note that the first parameter is the name of the storage account, whereas the second parameter is the access key:
    PS C:> $context = New-AzureStorageContext psautomation $key
  2. Use the New-AzureStorageContainer cmdlet to create a new public container. Note that the name must contain only numbers and lowercase letters. No special characters, spaces, or uppercase letters are permitted:
    PS C:> New-AzureStorageContainer –Name textfiles –Context $context –Permission Container
  3. Before uploading a file to the newly created directory, we need to ensure that we have a file to upload. To create a sample file, we can use the Set-Content cmdlet to create a new text file:
    PS C:> Set-Content C:FilesMyFile.txt –Value "Hello"
  4. Upload a file using the Set-AzureStorageBlobContent cmdlet:
    PS C:> Set-AzureStorageBlobContent –File C:FilesMyFile.txt 
    –Blob "MyFile.txt" –Container textfiles –Context $context
  5. Navigate to the newly uploaded blob in Internet Explorer. The URL for the blob is formatted as https://<StorageAccountName>.blob.core.windows.net/<ContainerName>/<BlobName>. In our example, the URL is https://psautomation.blob.core.windows.net/textfiles/MyFile.txt, as shown in the following screenshot:
     Automating Microsoft Azure with PowerShell

Summary

In this article, you learned about Microsoft Azure storage accounts and how to interact with the storage account services with PowerShell. This included the Azure File storage and Azure Blob storage.

Resources for Article:


Further resources on this subject:


LEAVE A REPLY

Please enter your comment!
Please enter your name here