Class: Dragonfly::AzureDataStore
- Inherits:
-
Object
- Object
- Dragonfly::AzureDataStore
- Defined in:
- lib/dragonfly/azure_data_store.rb,
lib/dragonfly/azure_data_store/version.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Instance Attribute Summary collapse
-
#access_key ⇒ Object
Returns the value of attribute access_key.
-
#account_name ⇒ Object
Returns the value of attribute account_name.
-
#container_name ⇒ Object
Returns the value of attribute container_name.
-
#root_path ⇒ Object
Returns the value of attribute root_path.
Instance Method Summary collapse
- #container ⇒ Object
- #destroy(uid) ⇒ Object
- #full_path(filename) ⇒ Object
-
#initialize(opts = {}) ⇒ AzureDataStore
constructor
A new instance of AzureDataStore.
-
#path_for(filename) ⇒ Object
def generate_uid(name) “#‘%Y/%m/%d/%H/%M/%S’/#SecureRandom.uuid/#name” end.
- #read(uid) ⇒ Object
- #storage ⇒ Object
- #write(content, opts = {}) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ AzureDataStore
Returns a new instance of AzureDataStore.
9 10 11 12 13 14 |
# File 'lib/dragonfly/azure_data_store.rb', line 9 def initialize(opts = {}) @account_name = opts[:account_name] @access_key = opts[:access_key] @container_name = opts[:container_name] @root_path = opts[:root_path] end |
Instance Attribute Details
#access_key ⇒ Object
Returns the value of attribute access_key.
7 8 9 |
# File 'lib/dragonfly/azure_data_store.rb', line 7 def access_key @access_key end |
#account_name ⇒ Object
Returns the value of attribute account_name.
7 8 9 |
# File 'lib/dragonfly/azure_data_store.rb', line 7 def account_name @account_name end |
#container_name ⇒ Object
Returns the value of attribute container_name.
7 8 9 |
# File 'lib/dragonfly/azure_data_store.rb', line 7 def container_name @container_name end |
#root_path ⇒ Object
Returns the value of attribute root_path.
7 8 9 |
# File 'lib/dragonfly/azure_data_store.rb', line 7 def root_path @root_path end |
Instance Method Details
#container ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/dragonfly/azure_data_store.rb', line 43 def container @container ||= begin storage.get_container_properties(container_name) rescue Azure::Core::Http::HTTPError => e raise if e.status_code != 404 storage.create_container(container_name) end end |
#destroy(uid) ⇒ Object
32 33 |
# File 'lib/dragonfly/azure_data_store.rb', line 32 def destroy(uid) end |
#full_path(filename) ⇒ Object
61 62 63 |
# File 'lib/dragonfly/azure_data_store.rb', line 61 def full_path(filename) File.join(*[root_path, filename].compact) end |
#path_for(filename) ⇒ Object
def generate_uid(name)
"#{Time.now.strftime '%Y/%m/%d/%H/%M/%S'}/#{SecureRandom.uuid}/#{name}"
end
56 57 58 59 |
# File 'lib/dragonfly/azure_data_store.rb', line 56 def path_for(filename) time = Time.now "#{time.strftime '%Y/%m/%d/'}#{rand(1e15).to_s(36)}_#{filename.gsub(/[^\w.]+/,'_')}" end |
#read(uid) ⇒ Object
29 30 |
# File 'lib/dragonfly/azure_data_store.rb', line 29 def read(uid) end |
#storage ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/dragonfly/azure_data_store.rb', line 35 def storage @storage ||= Azure::Storage::Blob::BlobService.create( storage_account_name: account_name, storage_access_key: access_key ) end |
#write(content, opts = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dragonfly/azure_data_store.rb', line 16 def write(content, opts = {}) blob = nil filename = path_for(content.name || 'file') content.file do |f| blob = azure_blob_service.create_block_blob( container.name, full_path(filename), f ) # storage.put_object(bucket_name, full_path(uid), f, full_storage_headers(headers, content.meta)) end # content = File.open("test.png", "rb") { |file| file.read } filename end |