Class: Refile::Azure
- Inherits:
-
Object
- Object
- Refile::Azure
- Extended by:
- BackendMacros
- Defined in:
- lib/refile/azure.rb,
lib/refile/azure/version.rb
Overview
A refile backend which stores files in Microsoft Azure
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#storage_account_name ⇒ Object
readonly
Returns the value of attribute storage_account_name.
Instance Method Summary collapse
-
#clear!(confirm = nil) ⇒ void
Remove all files in this backend.
-
#delete ⇒ void
Delete a file from this backend.
-
#exists? ⇒ Boolean
Return whether the file with the given id exists in this backend.
-
#get ⇒ Refile::File
Get a file from this backend.
-
#initialize(storage_account_name:, storage_access_key:, container:, max_size: nil, hasher: Refile::RandomHasher.new, **azure_options) ⇒ Azure
constructor
Sets up an Azure backend.
-
#open ⇒ IO
Return an IO object for the uploaded file which can be used to read its content.
-
#read ⇒ String
Return the entire contents of the uploaded file as a String.
-
#size ⇒ Integer
Return the size in bytes of the uploaded file.
-
#upload ⇒ Refile::File
Upload a file into this backend.
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
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 = ::Azure.client( .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 |
Instance Attribute Details
#container ⇒ Object (readonly)
Returns the value of attribute container.
32 33 34 |
# File 'lib/refile/azure.rb', line 32 def container @container end |
#max_size ⇒ Object (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_name ⇒ Object (readonly)
Returns the value of attribute storage_account_name.
32 33 34 |
# File 'lib/refile/azure.rb', line 32 def storage_account_name @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.
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 |
#delete ⇒ void
This method returns an undefined value.
Delete a file from this backend
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.
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 |
#get ⇒ Refile::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.
89 90 91 |
# File 'lib/refile/azure.rb', line 89 verify_id def get(id) Refile::File.new(self, id) end |
#open ⇒ IO
Return an IO object for the uploaded file which can be used to read its content.
109 110 111 |
# File 'lib/refile/azure.rb', line 109 verify_id def open(id) StringIO.new(read(id)) end |
#read ⇒ String
Return the entire contents of the uploaded file as a String.
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 |
#size ⇒ Integer
Return the size in bytes of the uploaded file.
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 |
#upload ⇒ Refile::File
Upload a file into this backend
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.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 |