This post will show you how to perform common scenarios using the Azure Blob service. The samples are written using the Ruby API. The scenarios covered include uploading, listing, downloading, and deleting blobs.
You can download final ruby script from here.
This post assumes you have already created a storage account and a container.
- Type "gem install azure" in the command window to install the gem and dependencies.
- Import installed package in to the script file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "azure" |
- Setup global parameters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Azure.config.storage_account_name = "account" | |
Azure.config.storage_access_key = "xxxxxxxxxxxxxxx+xxxxxxxxxxxxxxxx/xxxx+xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==" | |
CONTAINER = "container" | |
AZURE_BLOB_SERVICE = Azure::BlobService.new |
- Uploading a Blob into a Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def upload(input_path, blob_name) | |
content = File.open(input_path, "rb") { |file| file.read } | |
blob = AZURE_BLOB_SERVICE.create_block_blob(CONTAINER, | |
blob_name, content) | |
puts blob.name + " uploaded succesfully.." | |
end |
- List the Blobs in a Container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def list() | |
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 | |
end |
- Download Blobs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def download(blob_name, output_file) | |
blob, content = AZURE_BLOB_SERVICE.get_blob(CONTAINER,blob_name) | |
File.open(output_file,"wb") {|f| f.write(content)} | |
puts blob_name + " downloaded succesfully.." | |
end |
- Delete a Blob
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def delete(blob_name) | |
AZURE_BLOB_SERVICE.delete_blob(CONTAINER, blob_name) | |
puts blob_name + " deleted succesfully.." | |
end |
No comments:
Post a Comment