Class: Dragonfly::AzureDataStore

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly/azure_data_store.rb,
lib/dragonfly/azure_data_store/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ AzureDataStore

Returns a new instance of AzureDataStore.



12
13
14
15
16
17
18
19
20
21
# File 'lib/dragonfly/azure_data_store.rb', line 12

def initialize(opts = {})
  @account_name = opts[:account_name]
  @access_key = opts[:access_key]
  @container_name = opts[:container_name]
  @root_path = opts[:root_path]
  @url_scheme = opts[:url_scheme] || 'http'
  @url_host = opts[:url_host]
  @store_meta = opts[:store_meta].nil? ? true : opts[:store_meta]
  @legacy_meta = opts[:legacy_meta]
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



9
10
11
# File 'lib/dragonfly/azure_data_store.rb', line 9

def access_key
  @access_key
end

#account_nameObject

Returns the value of attribute account_name.



9
10
11
# File 'lib/dragonfly/azure_data_store.rb', line 9

def 
  @account_name
end

#container_nameObject

Returns the value of attribute container_name.



9
10
11
# File 'lib/dragonfly/azure_data_store.rb', line 9

def container_name
  @container_name
end

#legacy_metaObject

Returns the value of attribute legacy_meta.



9
10
11
# File 'lib/dragonfly/azure_data_store.rb', line 9

def legacy_meta
  @legacy_meta
end

#root_pathObject

Returns the value of attribute root_path.



9
10
11
# File 'lib/dragonfly/azure_data_store.rb', line 9

def root_path
  @root_path
end

#store_metaObject

Returns the value of attribute store_meta.



9
10
11
# File 'lib/dragonfly/azure_data_store.rb', line 9

def store_meta
  @store_meta
end

#url_hostObject

Returns the value of attribute url_host.



9
10
11
# File 'lib/dragonfly/azure_data_store.rb', line 9

def url_host
  @url_host
end

#url_schemeObject

Returns the value of attribute url_scheme.



9
10
11
# File 'lib/dragonfly/azure_data_store.rb', line 9

def url_scheme
  @url_scheme
end

Instance Method Details

#destroy(uid) ⇒ Object



54
55
56
57
58
59
# File 'lib/dragonfly/azure_data_store.rb', line 54

def destroy(uid)
  storage.delete_blob(container.name, full_path(uid))
  true
rescue Azure::Core::Http::HTTPError
  false
end

#read(uid) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dragonfly/azure_data_store.rb', line 34

def read(uid)
  path = full_path(uid)
  blob = storage.get_blob(container.name, path)
  meta = nil
  if store_meta
    meta = blob[0].
    if legacy_meta && (meta.nil? || meta.empty?)
      begin
        meta_blob = storage.get_blob(container.name, meta_path(path))
        meta = YAML.safe_load(meta_blob[1])
      rescue Azure::Core::Http::HTTPError
        meta = {}
      end
    end
  end
  [blob[1], meta]
rescue Azure::Core::Http::HTTPError
  nil
end

#url_for(uid, opts = {}) ⇒ Object



61
62
63
64
65
66
# File 'lib/dragonfly/azure_data_store.rb', line 61

def url_for(uid, opts = {})
  scheme = opts[:scheme] || url_scheme
  host   = opts[:host]   || url_host ||
           "#{}.blob.core.windows.net"
  "#{scheme}://#{host}/#{container_name}/#{full_path(uid)}"
end

#write(content, _opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/dragonfly/azure_data_store.rb', line 23

def write(content, _opts = {})
  filename = path_for(content.name || 'file')
  path = full_path(filename)
  options = {}
  options[:metadata] = content.meta if store_meta
  content.file do |f|
    storage.create_block_blob(container.name, path, f, options)
  end
  filename
end