class Refile::Azure
A refile backend which stores files in Microsoft Azure
@example
backend = Refile::Backend::Azure.new( storage_account_name: 'mystorage', storage_access_key: 'secret_key', container: "my-container", ) file = backend.upload(StringIO.new("hello")) backend.read(file.id) # => "hello"
Constants
- VERSION
Attributes
Public Class Methods
Sets up an Azure
backend
@param [String] container The name of the container where files will be stored @param [String] prefix A prefix to add to all files. @param [Integer, nil] max_size
The maximum size of an uploaded file @param [#hash] hasher A hasher which is used to generate ids from files @param [Hash] azure_options Additional options to initialize Azure
Client with @see github.com/Azure/azure-sdk-for-ruby
# File lib/refile/azure.rb, line 42 def initialize(storage_account_name:, storage_access_key:, container:, max_size: nil, hasher: Refile::RandomHasher.new, **azure_options) @azure_options = azure_options @azure = ::Azure.client( azure_options.merge( storage_account_name: storage_account_name, storage_access_key: storage_access_key, storage_blob_host: "https://#{storage_account_name}.blob.core.windows.net" ) ) @storage_account_name = storage_account_name @container = container @blobs = @azure.blobs @hasher = hasher @max_size = max_size end
Public Instance Methods
Remove all files in this backend. You must confirm the deletion by passing the symbol `:confirm` as an argument to this method.
@example
backend.clear!(:confirm)
@raise [Refile::Confirm] Unless the `:confirm` symbol has been passed. @param [:confirm] confirm Pass the symbol `:confirm` to confirm deletion. @return [void]
# File lib/refile/azure.rb, line 156 def clear!(confirm = nil) raise Refile::Confirm unless confirm == :confirm @blobs.list_blobs(@container).each do |blob| @blobs.delete_blob(@container, blob.name) end end
# File lib/refile/azure.rb, line 97 def delete(id) @blobs.delete_blob(@container, id) rescue ::Azure::Core::Http::HTTPError => exc raise exc unless exc.status_code == 404 nil end
# File lib/refile/azure.rb, line 140 def exists?(id) @blobs.get_blob_properties(@container, id) true rescue ::Azure::Core::Http::HTTPError => exc raise exc unless exc.status_code == 404 false end
# File lib/refile/azure.rb, line 89 def get(id) Refile::File.new(self, id) end
# File lib/refile/azure.rb, line 109 def open(id) StringIO.new(read(id)) end
# File lib/refile/azure.rb, line 117 def read(id) blob, body = @blobs.get_blob(@container, id) body rescue ::Azure::Core::Http::HTTPError => exc raise exc unless exc.status_code == 404 nil end
# File lib/refile/azure.rb, line 129 def size(id) @blobs.get_blob_properties(@container, id).properties[:content_length] rescue ::Azure::Core::Http::HTTPError => exc raise exc unless exc.status_code == 404 nil end
# File lib/refile/azure.rb, line 62 def upload(uploadable) id = @hasher.hash(uploadable) if uploadable.is_a?(Refile::File) and uploadable.backend.is_a?(Azure) and uploadable.backend.storage_account_name == storage_account_name @blobs.copy_blob(@container, id, uploadable.backend.container, uploadable.id) else body = if IO === uploadable uploadable elsif uploadable.respond_to?(:read) uploadable.read else uploadable end @blobs.create_block_blob(@container, id, body) end Refile::File.new(self, id) end