title | description | services | documentationcenter | author | manager | editor | ms.assetid | ms.service | ms.workload | ms.tgt_pltfrm | ms.devlang | ms.topic | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to use Blob storage (object storage) from Ruby | Microsoft Docs |
Store unstructured data in the cloud with Azure Blob storage (object storage). |
storage |
ruby |
mmacy |
timlt |
tysonn |
e2fe4c45-27b0-4d15-b3fb-e7eb574db717 |
storage |
storage |
na |
ruby |
article |
12/08/2016 |
marsma |
[!INCLUDE storage-selector-blob-include]
[!INCLUDE storage-try-azure-tools-queues]
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.
[!INCLUDE storage-blob-concepts-include]
[!INCLUDE storage-create-account-include]
Create a Ruby application. For instructions, see Ruby on Rails Web application on an Azure VM
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 a command-line interface such as PowerShell (Windows), Terminal (Mac), or Bash (Unix).
- Type "gem install azure" in the command window to install the gem and dependencies.
Using your favorite text editor, add the following to the top of the Ruby file where you intend to use storage:
require "azure"
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 before using Azure::Blob::BlobService with the following code:
Azure.config.storage_account_name = "<your azure storage account>"
Azure.config.storage_access_key = "<your azure storage access key>"
To obtain these values from a classic or Resource Manager storage account in the Azure portal:
- Log in to the Azure portal.
- Navigate to the storage account you want to use.
- In the Settings blade on the right, click Access Keys.
- In the Access keys blade that appears, you'll see the access key 1 and access key 2. You can use either of these.
- Click the copy icon to copy the key to the clipboard.
To obtain these values from a classic storage account in the classic Azure portal:
- Log in to the classic Azure portal.
- Navigate to the storage account you want to use.
- Click MANAGE ACCESS KEYS at the bottom of the navigation pane.
- In the pop-up dialog, you'll see the storage account name, primary access key and secondary access key. For access key, you can use either the primary one or the secondary one.
- Click the copy icon to copy the key to the clipboard.
[!INCLUDE storage-container-naming-rules-include]
The Azure::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::Blob::BlobService.new
begin
container = azure_blob_service.create_container("test-container")
rescue
puts $!
end
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 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.
- container: 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.
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")
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 = File.open("test.png", "rb") { |file| file.read }
blob = azure_blob_service.create_block_blob(container.name,
"image-blob", content)
puts blob.name
To list the containers, use list_containers() method. To list the blobs within a container, use list_blobs() method.
This outputs the urls of all the blobs in all the containers for the account.
containers = azure_blob_service.list_containers()
containers.each do |container|
blobs = azure_blob_service.list_blobs(container.name)
blobs.each do |blob|
puts blob.name
end
end
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(container.name,"image-blob")
File.open("download.png","wb") {|f| f.write(content)}
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(container.name, "image-blob")
To learn about more complex storage tasks, follow these links:
- Azure Storage Team Blog
- Azure SDK for Ruby repository on GitHub
- Transfer data with the AzCopy Command-Line Utility