Class: Refile::Azure

Inherits:
Object
  • Object
show all
Extended by:
BackendMacros
Defined in:
lib/refile/azure.rb,
lib/refile/azure/version.rb

Overview

A refile backend which stores files in Microsoft Azure

Examples:

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"

Constant Summary collapse

VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_account_name:, storage_access_key:, container:, max_size: nil, hasher: Refile::RandomHasher.new, **azure_options) ⇒ Azure

Sets up an Azure backend

Parameters:

  • container (String)

    The name of the container where files will be stored

  • prefix (String)

    A prefix to add to all files.

  • max_size (Integer, nil) (defaults to: nil)

    The maximum size of an uploaded file

  • hasher (#hash) (defaults to: Refile::RandomHasher.new)

    A hasher which is used to generate ids from files

  • azure_options (Hash)

    Additional options to initialize Azure Client with

See Also:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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_access_key: storage_access_key,
      storage_blob_host: "https://#{}.blob.core.windows.net"
    )
  )
  @storage_account_name = 
  @container = container
  @blobs = @azure.blobs
  @hasher = hasher
  @max_size = max_size
end

Instance Attribute Details

#containerObject (readonly)

Returns the value of attribute container.



32
33
34
# File 'lib/refile/azure.rb', line 32

def container
  @container
end

#max_sizeObject (readonly)

Returns the value of attribute max_size.



32
33
34
# File 'lib/refile/azure.rb', line 32

def max_size
  @max_size
end

#storage_account_nameObject (readonly)

Returns the value of attribute storage_account_name.



32
33
34
# File 'lib/refile/azure.rb', line 32

def 
  @storage_account_name
end

Instance Method Details

#clear!(confirm = nil) ⇒ void

This method returns an undefined value.

Remove all files in this backend. You must confirm the deletion by passing the symbol ‘:confirm` as an argument to this method.

Examples:

backend.clear!(:confirm)

Parameters:

  • confirm (:confirm) (defaults to: nil)

    Pass the symbol ‘:confirm` to confirm deletion.

Raises:

  • (Refile::Confirm)

    Unless the ‘:confirm` symbol has been passed.



156
157
158
159
160
161
162
# 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

#deletevoid

This method returns an undefined value.

Delete a file from this backend

Parameters:

  • id (String)

    The id of the file



97
98
99
100
101
102
# File 'lib/refile/azure.rb', line 97

verify_id def delete(id)
  @blobs.delete_blob(@container, id)
rescue ::Azure::Core::Http::HTTPError => exc
  raise exc unless exc.status_code == 404
  nil
end

#exists?Boolean

Return whether the file with the given id exists in this backend.

Parameters:

  • id (String)

    The id of the file

Returns:

  • (Boolean)


140
141
142
143
144
145
146
# File 'lib/refile/azure.rb', line 140

verify_id 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

#getRefile::File

Get a file from this backend.

Note that this method will always return a File object, even if a file with the given id does not exist in this backend. Use FileSystem#exists? to check if the file actually exists.

Parameters:

  • id (String)

    The id of the file

Returns:

  • (Refile::File)

    The retrieved file



89
90
91
# File 'lib/refile/azure.rb', line 89

verify_id def get(id)
  Refile::File.new(self, id)
end

#openIO

Return an IO object for the uploaded file which can be used to read its content.

Parameters:

  • id (String)

    The id of the file

Returns:

  • (IO)

    An IO object containing the file contents



109
110
111
# File 'lib/refile/azure.rb', line 109

verify_id def open(id)
  StringIO.new(read(id))
end

#readString

Return the entire contents of the uploaded file as a String.

Parameters:

  • id (String)

    The id of the file

Returns:

  • (String)

    The file’s contents



117
118
119
120
121
122
123
# File 'lib/refile/azure.rb', line 117

verify_id 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

#sizeInteger

Return the size in bytes of the uploaded file.

Parameters:

  • id (String)

    The id of the file

Returns:

  • (Integer)

    The file’s size



129
130
131
132
133
134
# File 'lib/refile/azure.rb', line 129

verify_id 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

#uploadRefile::File

Upload a file into this backend

Parameters:

  • uploadable (IO)

    An uploadable IO-like object.

Returns:

  • (Refile::File)

    The uploaded file



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/refile/azure.rb', line 62

verify_uploadable def upload(uploadable)
  id = @hasher.hash(uploadable)

  if uploadable.is_a?(Refile::File) and uploadable.backend.is_a?(Azure) and uploadable.backend. == 
    @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