Class: Shrine::Storage::AzureBlob

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/storage/azure_blob.rb,
lib/shrine/storage/azure_blob/version.rb

Constant Summary collapse

DEFAULT_URL_TTL =
15 * 60
VERSION =
"0.1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_name:, container:, access_key: nil, host: nil, prefix: nil, public: false, **options) ⇒ AzureBlob

Returns a new instance of AzureBlob.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/shrine/storage/azure_blob.rb', line 16

def initialize(account_name:, container:, access_key: nil, host: nil, prefix: nil, public: false, **options)
  @container = container
  @prefix = prefix
  @public = public
  @client = ::AzureBlob::Client.new(
    account_name: ,
    access_key: access_key,
    container: container,
    host: host,
    **options
  )
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



14
15
16
# File 'lib/shrine/storage/azure_blob.rb', line 14

def client
  @client
end

#containerObject (readonly)

Returns the value of attribute container.



14
15
16
# File 'lib/shrine/storage/azure_blob.rb', line 14

def container
  @container
end

#prefixObject (readonly)

Returns the value of attribute prefix.



14
15
16
# File 'lib/shrine/storage/azure_blob.rb', line 14

def prefix
  @prefix
end

Instance Method Details

#clear!Object



69
70
71
# File 'lib/shrine/storage/azure_blob.rb', line 69

def clear!
  delete_prefixed("")
end

#delete(id) ⇒ Object



59
60
61
62
63
# File 'lib/shrine/storage/azure_blob.rb', line 59

def delete(id)
  client.delete_blob(path_for(id))
rescue ::AzureBlob::Http::FileNotFoundError
  nil
end

#delete_prefixed(prefix) ⇒ Object



65
66
67
# File 'lib/shrine/storage/azure_blob.rb', line 65

def delete_prefixed(prefix)
  client.delete_prefix(path_for(prefix))
end

#ensure_container!Object



95
96
97
98
99
# File 'lib/shrine/storage/azure_blob.rb', line 95

def ensure_container!
  return if client.container_exist?

  client.create_container
end

#exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/shrine/storage/azure_blob.rb', line 55

def exists?(id)
  client.blob_exist?(path_for(id))
end

#open(id, **options) ⇒ Object



37
38
39
40
41
# File 'lib/shrine/storage/azure_blob.rb', line 37

def open(id, **options)
  StringIO.new(fetch_blob(path_for(id), options)).tap(&:binmode)
rescue ::AzureBlob::Http::FileNotFoundError => e
  raise Shrine::FileNotFound, e.message
end

#presign(id, method: "PUT", expires_in: DEFAULT_URL_TTL, content_type: nil, filename: nil, disposition: nil, metadata: {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/shrine/storage/azure_blob.rb', line 73

def presign(id, method: "PUT", expires_in: DEFAULT_URL_TTL, content_type: nil, filename: nil, disposition: nil,
            metadata: {}, **)
  headers = {
    "x-ms-blob-type" => "BlockBlob"
  }
  headers["Content-Type"] = content_type if present?(content_type)

  ().each do |header, value|
    headers[header.to_s] = value
  end

  if (blob_content_disposition = content_disposition(disposition, filename))
    headers["x-ms-blob-content-disposition"] = blob_content_disposition
  end

  {
    method: method,
    url: client.signed_uri(path_for(id), permissions: "cw", expiry: format_expiry(expires_in)).to_s,
    headers: headers
  }
end

#upload(io, id, shrine_metadata: {}, **options) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/shrine/storage/azure_blob.rb', line 29

def upload(io, id, shrine_metadata: {}, **options)
  client.create_block_blob(
    path_for(id),
    io.respond_to?(:rewind) ? rewindable(io) : io,
    **upload_options(, options)
  )
end

#url(id, expires_in: DEFAULT_URL_TTL, filename: nil, disposition: nil, content_type: nil, public: @public) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shrine/storage/azure_blob.rb', line 43

def url(id, expires_in: DEFAULT_URL_TTL, filename: nil, disposition: nil, content_type: nil, public: @public, **)
  return client.generate_uri("#{container}/#{path_for(id)}").to_s if public

  client.signed_uri(
    path_for(id),
    permissions: "r",
    expiry: format_expiry(expires_in),
    content_disposition: content_disposition(disposition, filename),
    content_type: content_type
  ).to_s
end