Class: Fog::Storage::AzureRM::File

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/azurerm/models/storage/file.rb

Overview

This class is giving implementation of create/save and delete/destroy for Blob.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyFile || String

Get file’s body if exists, else ”.

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/fog/azurerm/models/storage/file.rb', line 118

def body
  return attributes[:body] if attributes[:body]
  return '' unless last_modified

  file = collection.get(identity)
  if file.nil?
    attributes[:body] = ''
    return ''
  end

  attributes[:body] = file.body
end

Instance Method Details

#copy(target_directory_key, target_file_key, options = {}) ⇒ Fog::Storage::AzureRM::File

Copy object from one container to other container.

required attributes: directory, key

Parameters:

  • target_directory_key (String)
  • target_file_key (String)
  • options (Hash) (defaults to: {})

    options for copy_object method

Options Hash (options):

  • timeout (Integer)

    Sets to raise a TimeoutError if the copy does not finish in timeout seconds.

Returns:



152
153
154
155
156
157
158
159
160
161
# File 'lib/fog/azurerm/models/storage/file.rb', line 152

def copy(target_directory_key, target_file_key, options = {})
  requires :directory, :key

  timeout = options.delete(:timeout)
  copy_id, copy_status = service.copy_blob(target_directory_key, target_file_key, directory.key, key, options)
  service.wait_blob_copy_operation_to_finish(target_directory_key, target_file_key, copy_id, copy_status, timeout)

  target_directory = service.directories.new(key: target_directory_key)
  target_directory.files.head(target_file_key)
end

#copy_from_uri(source_uri, options = {}) ⇒ Boolean

Copy object from a uri.

required attributes: directory, key

Parameters:

  • source_uri (String)
  • options (Hash) (defaults to: {})

    options for copy_object method

Options Hash (options):

  • timeout (Integer)

    Sets to raise a TimeoutError if the copy does not finish in timeout seconds.

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/fog/azurerm/models/storage/file.rb', line 173

def copy_from_uri(source_uri, options = {})
  requires :directory, :key

  timeout = options.delete(:timeout)
  copy_id, copy_status = service.copy_blob_from_uri(directory.key, key, source_uri, options)
  service.wait_blob_copy_operation_to_finish(directory.key, key, copy_id, copy_status, timeout)

  blob = service.get_blob_properties(directory.key, key)
  data = parse_storage_object(blob)
  merge_attributes(data)

  true
end

#destroy(options = {}) ⇒ Boolean

Destroy file.

required attributes: directory, key

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • versionId (Object)

Returns:

  • (Boolean)

    true if successful



196
197
198
199
200
201
202
203
# File 'lib/fog/azurerm/models/storage/file.rb', line 196

def destroy(options = {})
  requires :key
  requires :directory
  attributes[:body] = nil
  service.delete_blob(directory.key, key, options)

  true
end

#public?Boolean

Get whether the file can be accessed by anonymous.

Returns:

  • (Boolean)


209
210
211
212
213
214
# File 'lib/fog/azurerm/models/storage/file.rb', line 209

def public?
  requires :directory

  # TBD: The blob can be accessed if read permision is set in one access policy of the container.
  directory.acl == 'container' || directory.acl == 'blob'
end

#public_url(options = {}) ⇒ String

Get publicly accessible url.

required attributes: directory, key

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • scheme (String)

    Sets which URL to get, http or https. Options: https or http. Default is https.

Returns:

  • (String)

    A public url.



225
226
227
228
229
# File 'lib/fog/azurerm/models/storage/file.rb', line 225

def public_url(options = {})
  requires :directory, :key
  options[:scheme] == 'https' if options[:scheme].nil?
  @service.get_blob_url(directory.key, key, options) if public?
end

#save(options = {}) ⇒ Boolean

Save the file to the directory in Azure storage. TODO: Support snapshots.

required attributes: body(Only if update_body is true), directory, key

Parameters:

  • identity (String)

    Name of directory

  • options (Hash) (defaults to: {})

Options Hash (options):

  • update_body (Boolean)

    Sets whether to upload the body of the file. Default is true. Will update metadata and properties when update_body is set to false.

  • worker_thread_num (Integer)

    Sets how many threads will be used to upload the body. Default is 8.

  • blob_type (String)

    or Blob-Type Sets blob type for the file. Options: ‘BlockBlob’ or ‘PageBlob’. Default is ‘BlockBlob’.

  • content_type (String)

    or Content-Type Sets content type for the file. For example, ‘text/plain’.

  • content_md5 (String)

    or Content-MD5 Sets content MD5 hash for the file. Only for store.

    When a block file whose size <= 32 MB, Azure will verify the integrity of the blob during transport.
    Please reference this issue: https://github.com/Azure/azure-storage-ruby/issues/64
    
  • content_encoding (String)

    or Content-Encoding Sets content encoding for the file. For example, ‘x-gzip’.

  • content_language (String)

    or Content-Language Sets the natural languages used by the file.

  • cache_control (String)

    or Cache-Control Sets content encoding for the file. For example, ‘No-cache’.

  • content_disposition (String)

    or Content-Disposition Sets content disposition for the file. For exampple, ‘attachment; filename=testing.txt’.

  • metadata (Hash)

    Sets custom metadata values to store with the file.

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fog/azurerm/models/storage/file.rb', line 66

def save(options = {})
  update_body = options.delete(:update_body)
  update_body = true if update_body.nil?
  requires :directory, :key
  raise ArgumentError.new('body is required when update_body is true') if update_body && attributes[:body].nil?

  remap_attributes(
    options,
    'Blob-Type'           => :blob_type,
    'Content-Type'        => :content_type,
    'Content-MD5'         => :content_md5,
    'Content-Encoding'    => :content_encoding,
    'Content-Language'    => :content_language,
    'Cache-Control'       => :cache_control,
    'Content-Disposition' => :content_disposition
  )
  options = {
    blob_type: blob_type,
    content_type: content_type,
    content_md5: content_md5,
    content_encoding: content_encoding,
    content_language: content_language,
    cache_control: cache_control,
    content_disposition: content_disposition,
    metadata: 
  }.merge!(options)
  options = options.reject { |_key, value| value.nil? || value.to_s.empty? }

  if update_body
    blob = save_blob(options)

    data = parse_storage_object(blob)
    merge_attributes(data)
    attributes[:content_length] = Fog::Storage.get_body_size(body)
    attributes[:content_type] ||= Fog::Storage.get_content_type(body)
  else
    service.(directory.key, key, options[:metadata]) if options[:metadata]
    options.delete(:metadata)
    service.put_blob_properties(directory.key, key, options)

    blob = service.get_blob_properties(directory.key, key)
    data = parse_storage_object(blob)
    merge_attributes(data)
  end

  true
end

#url(expires, options = {}) ⇒ String

Get a url for file.

required attributes: key

Parameters:

  • expires (Time)

    The time at which the shared access signature becomes invalid, in a UTC format.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • scheme (String)

    Sets which URL to get, http or https. Options: https or http. Default is https.

Returns:

  • (String)

    A public url which will expire after the specified time.



241
242
243
244
# File 'lib/fog/azurerm/models/storage/file.rb', line 241

def url(expires, options = {})
  requires :key
  collection.get_url(key, expires, options)
end