Ruby
Azure
blob

Set a filename alias in Azure Blob Service

This article is a translation from https://qiita.com/kawasaki/items/918462f7d98fe61e3e61 .


What I want to do is ...

to set a filename alias for a file stored in Azure Blob Service so that I can download the blob by an arbitrary name.

Situation

  • The target file is already stored in Azure Blob Service with the name "file1234"
  • The file should be downloaded as "manual.pdf"
  • You cannot realize it through the Azure GUI interface as of May, 2018

Code

#!/usr/bin/env ruby

require "azure"  

Azure.config.storage_account_name = "myaccountname"
Azure.config.storage_access_key = "Rfof7hZYw7oj.......uKbI1nPGoUkc0yFvA=="

Azure::Blob::BlobService.new.set_blob_properties(
  "mycontainer", 
  "file1234",
  { blob_content_disposition: "attachment; filename=manual.pdf" }
)

Explanation

  • require "azure"
    Require Azure library provided by Microsoft. It's not included in the ruby standard library. So, you should install it before you call it by doing "sudo gem install azure" or writing a Gemfile and "bundle install"
  • Two lines of Azure.config are the necessary information to use Azure. "myaccountname" in the first line is the name you specified when you made the Blob service resource and the second line is your access key which you should keep secret and you can get it from the "Access keys" menu in your storage account.
  • Azure::Blob::BlobService.new.set_blob_properties
    Set the property of the blob "file1234" in the target container "mycontainer". The information you specify here will be used in the Content-Disposition header in the HTTP response.

Test environment (May 8, 2018)

System or library Version
Ruby 2.3.1
azure (ruby gem) 0.7.10