Module: Paperclip::Storage::AzureStorage

Defined in:
lib/paperclip/storage/azure_storage.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(_base) ⇒ Object

[View source]

4
5
6
7
# File 'lib/paperclip/storage/azure_storage.rb', line 4

def self.extended(_base)
  require 'azure/storage/blob'
  require 'azure/storage/common'
end

Instance Method Details

#azure_url(style_name = default_style) ⇒ Object

[View source]

49
50
51
# File 'lib/paperclip/storage/azure_storage.rb', line 49

def azure_url(style_name = default_style)
  "#{@options[:resource]}/#{@options[:container]}/#{path(style_name)}".squeeze('/')
end

#copy_to_local_file(style, local_dest_path) ⇒ Object

[View source]

43
44
45
46
47
# File 'lib/paperclip/storage/azure_storage.rb', line 43

def copy_to_local_file(style, local_dest_path)
  container = @options[:container]
  _blob, content = storage_client.get_blob(container, path(style), timeout: 60)
  ::File.open(local_dest_path, 'wb') { |local_file| local_file.write(content) }
end

#exists?(style_name = default_style) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

9
10
11
12
13
14
15
16
17
# File 'lib/paperclip/storage/azure_storage.rb', line 9

def exists?(style_name = default_style)
  container = @options[:container]
  storage_client.list_blobs(
    container,
    prefix: path(style_name),
    max_results: 1,
    timeout: 60
  ).present?
end

#flush_deletesObject

[View source]

36
37
38
39
40
41
# File 'lib/paperclip/storage/azure_storage.rb', line 36

def flush_deletes
  container = @options[:container]
  @queued_for_delete.each { |path| storage_client.delete_blob(container, path, timeout: 60) }

  @queued_for_delete = []
end

#flush_writesObject

[View source]

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/paperclip/storage/azure_storage.rb', line 19

def flush_writes
  container = @options[:container]
  @queued_for_write.each do |style_name, file|
    storage_client.create_block_blob(
      container,
      path(style_name),
      file.read,
      timeout: 60,
      content_type: file.content_type,
      content_length: file.size
    )
  end

  after_flush_writes
  @queued_for_write = {}
end