How to use Blob storage from Ruby


Manage Azure Blob storage resources with Azure Storage Explorer. Azure Storage Explorer is a free, standalone app from Microsoft that enables you to manage Azure Blob storage resources. Using Azure Storage Explorer, you can visually create, read, update, and delete blob containers and blobs, as well as manage access to your blobs containers and blobs.


Azure Blob storage is a service that stores unstructured data in the cloud as objects/blobs. Blob storage can store any type of text or binary data, such as a document, media file, or application installer. Blob storage is also referred to as object storage.

This guide will show you how to perform common scenarios using Blob storage. The samples are written using the Ruby API. The scenarios covered include uploading, listing, downloading, and deleting blobs.

What is Blob storage?

Azure Blob storage is a service for storing large amounts of unstructured object data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. You can use Blob storage to expose data publicly to the world, or to store application data privately.

Common uses of Blob storage include:

  • Serving images or documents directly to a browser.
  • Storing files for distributed access.
  • Streaming video and audio.
  • Storing data for backup and restore, disaster recovery, and archiving.
  • Storing data for analysis by an on-premises or Azure-hosted service.

Blob service concepts

The Blob service contains the following components:

Diagram of Blob service architecture

  • Storage account: All access to Azure Storage is done through a storage account. This storage account can be a General-purpose storage account or a Blob storage account, which is specialized for storing objects or blobs. See About Azure storage accounts for more information.
  • Container: A container provides a grouping of a set of blobs. All blobs must be in a container. An account can contain an unlimited number of containers. A container can store an unlimited number of blobs. Note that the container name must be lowercase.
  • Blob: A file of any type and size. Azure Storage offers three types of blobs: block blobs, append blobs, and page blobs.

    Block blobs are ideal for storing text or binary files, such as documents and media files. A single block blob can contain up to 50,000 blocks of up to 100 MB each, for a total size of slightly more than 4.75 TB (100 MB X 50,000).

    Append blobs are similar to block blobs in that they are made up of blocks, but they are optimized for append operations, so they are useful for logging scenarios. A single append blob can contain up to 50,000 blocks of up to 4 MB each, for a total size of slightly more than 195 GB (4 MB X 50,000).

    Page blobs can be up to 1 TB in size, and are more efficient for frequent read/write operations. Azure Virtual Machines use page blobs as operating system and data disks.

    For details about naming containers and blobs, see Naming and referencing containers, blobs, and metadata.

Create an Azure storage account

The easiest way to create your first Azure storage account is by using the Azure portal. To learn more, see Create a storage account.

You can also create an Azure storage account by using Azure PowerShell, Azure CLI, or the Storage Resource Provider Client Library for .NET.

If you prefer not to create a storage account at this time, you can also use the Azure storage emulator to run and test your code in a local environment. For more information, see Use the Azure Storage Emulator for Development and Testing.

Create a Ruby application

Create a Ruby application. For instructions, see Ruby on Rails Web application on an Azure VM

Configure your application to access Storage

To use Azure Storage, you need to download and use the Ruby azure package, which includes a set of convenience libraries that communicate with the storage REST services.

Use RubyGems to obtain the package

  1. Use a command-line interface such as PowerShell (Windows), Terminal (Mac), or Bash (Unix).
  2. Type "gem install azure-storage-blob" in the command window to install the gem and dependencies.

Import the package

Using your favorite text editor, add the following to the top of the Ruby file where you intend to use storage:

require "azure/storage/blob"

Set up an Azure Storage Connection

The azure module will read the environment variables AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY for information required to connect to your Azure storage account. If these environment variables are not set, you must specify the account information using Azure::Blob::BlobService::create with the following code:

blob_client = Azure::Storage::Blob::BlobService.create(
    storage_account_name: account_name,
    storage_access_key: account_key

To obtain these values from a classic or Resource Manager storage account in the Azure portal:

  1. Log in to the Azure portal.
  2. Navigate to the storage account you want to use.
  3. In the Settings blade on the right, click Access Keys.
  4. In the Access keys blade that appears, you'll see the access key 1 and access key 2. You can use either of these.
  5. Click the copy icon to copy the key to the clipboard.

Create a container

Every blob in Azure storage must reside in a container. The container forms part of the blob name. For example, mycontainer is the name of the container in these sample blob URIs:

A container name must be a valid DNS name, conforming to the following naming rules:

  1. Container names must start with a letter or number, and can contain only letters, numbers, and the dash (-) character.
  2. Every dash (-) character must be immediately preceded and followed by a letter or number; consecutive dashes are not permitted in container names.
  3. All letters in a container name must be lowercase.
  4. Container names must be from 3 through 63 characters long.


Note that the name of a container must always be lowercase. If you include an upper-case letter in a container name, or otherwise violate the container naming rules, you may receive a 400 error (Bad Request).

The Azure::Storage::Blob::BlobService object lets you work with containers and blobs. To create a container, use the create_container() method.

The following code example creates a container or prints the error if there is any.

azure_blob_service = Azure::Storage::Blob::BlobService.create_from_env
    container = azure_blob_service.create_container("test-container")
    puts $!

If you want to make the files in the container public, you can set the container's permissions.

You can just modify the create_container() call to pass the :public_access_level option:

container = azure_blob_service.create_container("test-container",
    :public_access_level => "<public access level>")

Valid values for the :public_access_level option are:

  • blob: Specifies public read access for blobs. Blob data within this container can be read via anonymous request, but container data is not available. Clients cannot enumerate blobs within the container via anonymous request.
  • container: Specifies full public read access for container and blob data. Clients can enumerate blobs within the container via anonymous request, but cannot enumerate containers within the storage account.

Alternatively, you can modify the public access level of a container by using set_container_acl() method to specify the public access level.

The following code example changes the public access level to container:

azure_blob_service.set_container_acl('test-container', "container")

Upload a blob into a container

To upload content to a blob, use the create_block_blob() method to create the blob, use a file or string as the content of the blob.

The following code uploads the file test.png as a new blob named "image-blob" in the container.

content ="test.png", "rb") { |file| }
blob = azure_blob_service.create_block_blob(,
    "image-blob", content)

List the blobs in a container

To list the containers, use list_containers() method. To list the blobs within a container, use list_blobs() method. In order to list all blobs in a container, you must follow a continuation token returned by service and continue running list_blobs with this token. See the List Blobs REST API for details.

The following code outputs the all the blobs in a container.

nextMarker = nil
loop do
    blobs = azure_blob_service.list_blobs(container_name, { marker: nextMarker })
    blobs.each do |blob|
        puts "\tBlob name #{}"
    nextMarker = blobs.continuation_token
    break unless nextMarker && !nextMarker.empty?

Download blobs

To download blobs, use the get_blob() method to retrieve the contents.

The following code example demonstrates using get_blob() to download the contents of "image-blob" and write it to a local file.

blob, content = azure_blob_service.get_blob(,"image-blob")"download.png","wb") {|f| f.write(content)}

Delete a Blob

Finally, to delete a blob, use the delete_blob() method. The following code example demonstrates how to delete a blob.

azure_blob_service.delete_blob(, "image-blob")

Next steps

To learn about more complex storage tasks, follow these links: