Hey there, fellow Ruby developer! Ready to dive into the world of Azure Files? You're in for a treat. Azure Files offers cloud-based file shares that are accessible via industry-standard protocols. Today, we're going to harness this power using the azure-storage-file
gem. Let's get cracking!
Before we jump in, make sure you've got:
azure-storage-file
gem installed (gem install azure-storage-file
)Got all that? Great! Let's move on to the fun stuff.
First things first, we need to authenticate. Grab your storage account access key from the Azure portal and let's initialize our client:
require 'azure/storage/file' client = Azure::Storage::File::FileService.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 start by creating a file share:
client.create_share('my-awesome-share')
Now, let's add a directory to our share:
client.create_directory('my-awesome-share', 'my-cool-directory')
Time to upload a file:
content = File.open('local_file.txt', 'rb') { |file| file.read } client.create_file('my-awesome-share', 'my-cool-directory', 'remote_file.txt', content.length) client.put_file_range('my-awesome-share', 'my-cool-directory', 'remote_file.txt', 0, content.length - 1, content)
And now, let's download it:
file, content = client.get_file('my-awesome-share', 'my-cool-directory', 'remote_file.txt') File.open('downloaded_file.txt', 'wb') { |f| f.write(content) }
Want to see what's in your share? No problem:
client.list_directories_and_files('my-awesome-share').each do |item| puts item.name end
You can add custom metadata to your files:
metadata = { 'key1' => 'value1', 'key2' => 'value2' } client.set_file_metadata('my-awesome-share', 'my-cool-directory', 'remote_file.txt', metadata)
Need to update file properties? We've got you covered:
client.set_file_properties('my-awesome-share', 'my-cool-directory', 'remote_file.txt', content_type: 'text/plain')
Copying files is a breeze:
source_uri = 'https://myaccount.file.core.windows.net/myshare/myfile' client.copy_file('my-awesome-share', 'my-cool-directory', 'copied_file.txt', source_uri)
You can set up shared access signatures for more granular control:
start_time = Time.now - 300 expiry_time = start_time + 3600 sas_token = client.generate_share_shared_access_signature( 'my-awesome-share', { permissions: 'r', start: start_time, expiry: expiry_time } )
When working with cloud services, things can sometimes go wrong. Let's add some retry logic:
require 'retries' with_retries(max_tries: 3, base_sleep_seconds: 0.5, max_sleep_seconds: 1.0) do client.create_file('my-awesome-share', 'my-cool-directory', 'remote_file.txt', content.length) end
This will retry the operation up to 3 times with exponential backoff.
And there you have it! You're now equipped to integrate Azure Files into your Ruby applications like a pro. Remember, practice makes perfect, so don't be afraid to experiment and push the boundaries of what you can do with Azure Files.
Happy coding, and may your files always be in the right place at the right time!