Hey there, fellow Ruby developer! Ready to dive into the world of Azure Blob Storage? You're in for a treat. Azure Blob Storage is a powerhouse for storing massive amounts of unstructured data, and with the azure-storage
gem, we'll be integrating it into our Ruby projects in no time.
Before we jump in, make sure you've got:
azure-storage
gem installed (gem install azure-storage
)Got all that? Great! Let's get our hands dirty.
First things first, we need to authenticate. Grab your storage account connection string from the Azure portal and let's initialize our BlobService
client:
require 'azure/storage/blob' client = Azure::Storage::Blob::BlobService.create( storage_account_name: 'your_account_name', storage_access_key: 'your_access_key' )
Easy peasy, right? Now we're ready to rock and roll.
Let's create a container to store our blobs:
container_name = 'my-awesome-container' client.create_container(container_name)
Time to upload your first blob:
blob_name = 'cool-file.txt' content = 'Hello, Azure!' client.create_block_blob(container_name, blob_name, content)
Retrieving your blob is just as simple:
blob, content = client.get_blob(container_name, blob_name) puts content # Outputs: Hello, Azure!
Want to see what's in your container?
client.list_blobs(container_name).each do |blob| puts blob.name end
Cleaning up is important:
client.delete_blob(container_name, blob_name)
Add some extra info to your blobs:
client.set_blob_metadata( container_name, blob_name, {'category' => 'images', 'type' => 'png'} )
Control who can access your blobs:
client.set_container_acl(container_name, 'container')
Need temporary access? SAS to the rescue:
sas_token = client.generate_shared_access_signature( container_name, blob_name, Azure::Storage::Blob::BlobService::BlobPermissions::READ, expiry: Time.now + 3600 )
Always be prepared for the unexpected:
begin client.create_block_blob(container_name, blob_name, content) rescue Azure::Core::Http::HTTPError => e logger.error "HTTP Error: #{e.message}" retry end
For large files, use block blobs and parallel operations:
client.create_block_blob(container_name, 'large-file.zip', File.open('large-file.zip'), parallel_upload: true)
Always enable encryption at rest and secure transfer:
client = Azure::Storage::Blob::BlobService.create( storage_account_name: 'your_account_name', storage_access_key: 'your_access_key', default_endpoints_protocol: 'https' )
For unit tests, mock the Azure client:
require 'minitest/autorun' require 'mocha/minitest' class BlobStorageTest < Minitest::Test def test_upload_blob mock_client = mock() mock_client.expects(:create_block_blob).once # Your upload method here upload_blob(mock_client, 'container', 'blob', 'content') end end
And there you have it! You're now equipped to integrate Azure Blob Storage into your Ruby projects like a pro. Remember, this is just scratching the surface - there's so much more you can do with Azure Blob Storage.
Keep exploring, keep coding, and most importantly, have fun with it! If you want to dive deeper, check out the Azure Storage Blobs documentation for more advanced features and best practices.
Now go forth and build something awesome! 🚀