Class: Azure::Armrest::StorageAccount

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/azure/armrest/model/base_model.rb,
lib/azure/armrest/model/storage_account.rb

Direct Known Subclasses

StorageAccountKey

Defined Under Namespace

Classes: Blob, BlobMetadata, BlobProperty, BlobServiceProperty, BlobServiceStat, BlobSnapshot, Container, ContainerProperty, PrivateImage, Table, TableData

Instance Attribute Summary collapse

Attributes inherited from BaseModel

#resource_group

Instance Method Summary collapse

Methods inherited from BaseModel

#==, #[], #[]=, #eql?, #inspect, #pretty_print, #to_h, #to_hash, #to_json, #to_s, #to_str

Constructor Details

#initialize(json) ⇒ StorageAccount

Returns a new instance of StorageAccount.



36
37
38
39
40
41
42
# File 'lib/azure/armrest/model/storage_account.rb', line 36

def initialize(json)
  super
  @storage_api_version = '2015-02-21'
  @proxy = ENV['http_proxy']
  @ssl_version = 'TLSv1'
  @ssl_verify = nil
end

Instance Attribute Details

#proxyObject

An http proxy to use per request. Defaults to ENV if set.



28
29
30
# File 'lib/azure/armrest/model/storage_account.rb', line 28

def proxy
  @proxy
end

#ssl_verifyObject

The SSL verification method used for each request. The default is VERIFY_PEER.



34
35
36
# File 'lib/azure/armrest/model/storage_account.rb', line 34

def ssl_verify
  @ssl_verify
end

#ssl_versionObject

The SSL version to use per request. Defaults to TLSv1.



31
32
33
# File 'lib/azure/armrest/model/storage_account.rb', line 31

def ssl_version
  @ssl_version
end

#storage_api_versionObject

The version string used in headers sent as part any internal http request. The default is 2015-02-21.



25
26
27
# File 'lib/azure/armrest/model/storage_account.rb', line 25

def storage_api_version
  @storage_api_version
end

Instance Method Details

#all_blobs(key = nil) ⇒ Object

Returns an array of all blobs for all containers.



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/azure/armrest/model/storage_account.rb', line 215

def all_blobs(key = nil)
  key ||= properties.key1
  array = []
  mutex = Mutex.new

  Parallel.each(containers(key), :in_threads => 10) do |container|
    mutex.synchronize { array << blobs(container.name, key) }
  end

  array.flatten
end

#blob_metadata(container, blob, key = nil, options = {}) ⇒ Object

Return metadata for the given blob within container. You may specify a date to retrieve metadata for a specific snapshot.



242
243
244
245
246
247
248
249
250
251
# File 'lib/azure/armrest/model/storage_account.rb', line 242

def (container, blob, key = nil, options = {})
  key ||= properties.key1

  query = "comp=metadata"
  query << "&snapshot=" + options[:date] if options[:date]

  response = blob_response(key, query, container, blob)

  BlobMetadata.new(response.headers)
end

#blob_properties(container, blob, key = nil, options = {}) ⇒ Object

Return the blob properties for the given blob found in container. You may optionally provide a date to get information for a snapshot.



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/azure/armrest/model/storage_account.rb', line 164

def blob_properties(container, blob, key = nil, options = {})
  key ||= properties.key1

  url = File.join(properties.primary_endpoints.blob, container, blob)
  url += "?snapshot=" + options[:date] if options[:date]

  headers = build_headers(url, key, :blob, :verb => 'HEAD')

  response = ArmrestService.send(
    :rest_head,
    :url         => url,
    :headers     => headers,
    :proxy       => proxy,
    :ssl_version => ssl_version,
    :ssl_verify  => ssl_verify
  )

  BlobProperty.new(response.headers)
end

#blob_service_properties(key = nil) ⇒ Object

Returns the blob service properties for the current storage account.



229
230
231
232
233
234
235
236
237
# File 'lib/azure/armrest/model/storage_account.rb', line 229

def blob_service_properties(key = nil)
  key ||= properties.key1

  response = blob_response(key, "restype=service&comp=properties")
  toplevel = 'StorageServiceProperties'

  doc = Nokogiri::XML(response.body).xpath("//#{toplevel}")
  BlobServiceProperty.new(Hash.from_xml(doc.to_s)[toplevel])
end

#blob_service_stats(key = nil) ⇒ Object

Retrieves statistics related to replication for the Blob service. Only available on the secondary location endpoint when read-access geo-redundant replication is enabled for the storage account.



257
258
259
260
261
262
263
264
265
# File 'lib/azure/armrest/model/storage_account.rb', line 257

def blob_service_stats(key = nil)
  key ||= properties.key1

  response = blob_response(key, "restype=service&comp=stats")
  toplevel = 'StorageServiceStats'

  doc = Nokogiri::XML(response.body).xpath("//#{toplevel}")
  BlobServiceStat.new(Hash.from_xml(doc.to_s)[toplevel])
end

#blobs(container, key = nil, include_snapshot = false) ⇒ Object

Return a list of blobs for the given container using the given key or the key1 property of the StorageAccount object.



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/azure/armrest/model/storage_account.rb', line 187

def blobs(container, key = nil, include_snapshot = false)
  key ||= properties.key1

  url = File.join(properties.primary_endpoints.blob, container)
  url += "?restype=container&comp=list"
  url += "&include=snapshots" if include_snapshot

  headers = build_headers(url, key)

  response = ArmrestService.send(
    :rest_get,
    :url         => url,
    :headers     => headers,
    :proxy       => proxy,
    :ssl_version => ssl_version,
    :ssl_verify  => ssl_verify
  )

  doc = Nokogiri::XML(response.body)

  doc.xpath('//Blobs/Blob').map do |node|
    hash = Hash.from_xml(node.to_s)['Blob'].merge(:container => container)
    hash.key?('Snapshot') ? BlobSnapshot.new(hash) : Blob.new(hash)
  end
end

#container_acl(name, key = nil) ⇒ Object

Returns the properties for the given container name using account key. If no key is provided, it is assumed that the StorageAccount object includes the key1 property.

If the returned object does not contain x_ms_blob_public_access then the container is private to the account owner. You can also use the :private? method to determine if the account is public or private.



152
153
154
155
156
157
158
159
# File 'lib/azure/armrest/model/storage_account.rb', line 152

def container_acl(name, key = nil)
  key ||= properties.key1

  response = blob_response(key, "restype=container&comp=acl", name)
  response.headers[:private?] = response.headers.include?(:x_ms_blob_public_access) ? false : true

  ContainerProperty.new(response.headers)
end

#container_properties(name, key = nil) ⇒ Object

Returns the properties for the given container name using account key. If no key is provided, it is assumed that the StorageAccount object includes the key1 property.



136
137
138
139
140
141
142
# File 'lib/azure/armrest/model/storage_account.rb', line 136

def container_properties(name, key = nil)
  key ||= properties.key1

  response = blob_response(key, "restype=container", name)

  ContainerProperty.new(response.headers)
end

#containers(key = nil) ⇒ Object

Return a list of container names for the given storage account key. If no key is provided, it is assumed that the StorageAccount object includes the key1 property.



122
123
124
125
126
127
128
129
130
# File 'lib/azure/armrest/model/storage_account.rb', line 122

def containers(key = nil)
  key ||= properties.key1

  response = blob_response(key, "comp=list")

  Nokogiri::XML(response.body).xpath('//Containers/Container').map do |element|
    Container.new(Hash.from_xml(element.to_s)['Container'])
  end
end

#copy_blob(src_container, src_blob, dst_container, dst_blob = nil, key = nil) ⇒ Object

Copy the blob from the source container/blob to the destination container/blob. If no destination blob name is provided, it will use the same name as the source.

Example:

source = "Microsoft.Compute/Images/your_container/your-img-osDisk.123xyz.vhd"
storage_acct.copy_blob('system', source, 'vhds', nil, your_key)


275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/azure/armrest/model/storage_account.rb', line 275

def copy_blob(src_container, src_blob, dst_container, dst_blob = nil, key = nil)
  key ||= properties.key1
  dst_blob ||= File.basename(src_blob)

  dst_url = File.join(properties.primary_endpoints.blob, dst_container, dst_blob)
  src_url = File.join(properties.primary_endpoints.blob, src_container, src_blob)

  options = {'x-ms-copy-source' => src_url, 'If-None-Match' => '*', :verb => 'PUT'}

  headers = build_headers(dst_url, key, :blob, options)

  response = ArmrestService.send(
    :rest_put,
    :url         => dst_url,
    :payload     => '',
    :headers     => headers,
    :proxy       => proxy,
    :ssl_version => ssl_version,
    :ssl_verify  => ssl_verify
  )

  Blob.new(response.headers)
end

#create_blob(container, blob, data, key = nil) ⇒ Object

Create new blob for a container.

The data parameter is a hash that contains the blob’s information:

data # - Required. Specifies the type of blob to create: block, page or append.

data # - Optional. Set the blob’s content encoding. …



331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/azure/armrest/model/storage_account.rb', line 331

def create_blob(container, blob, data, key = nil)
  key ||= properties.key1

  url = File.join(properties.primary_endpoints.blob, container, blob)

  options = {:verb => 'PUT'}
  options = options.merge(data)
  headers = build_headers(url, key, :blob, options)

  response = ArmrestService.send(
    :rest_put,
    :url         => url,
    :payload     => '',
    :headers     => headers,
    :proxy       => proxy,
    :ssl_version => ssl_version,
    :ssl_verify  => ssl_verify
  )

  Blob.new(response.headers)
end

#create_blob_snapshot(container, blob, key = nil) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/azure/armrest/model/storage_account.rb', line 353

def create_blob_snapshot(container, blob, key = nil)
  key ||= properties.key1

  url = File.join(properties.primary_endpoints.blob, container, blob)
  url += "?comp=snapshot"

  headers = build_headers(url, key, :blob, :verb => 'PUT')

  response = ArmrestService.send(
    :rest_put,
    :url         => url,
    :payload     => '',
    :headers     => headers,
    :proxy       => proxy,
    :ssl_version => ssl_version,
    :ssl_verify  => ssl_verify
  )

  BlobSnapshot.new(
    'name'          => blob,
    'last_modified' => response.headers.fetch(:last_modified),
    'snapshot'      => response.headers.fetch(:x_ms_snapshot)
  )
end

#delete_blob(container, blob, key = nil, options = {}) ⇒ Object

Delete the given blob found in container.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/azure/armrest/model/storage_account.rb', line 301

def delete_blob(container, blob, key = nil, options = {})
  key ||= properties.key1

  url = File.join(properties.primary_endpoints.blob, container, blob)
  url += "?snapshot=" + options[:date] if options[:date]

  headers = build_headers(url, key, :blob, :verb => 'DELETE')

  ArmrestService.send(
    :rest_delete,
    :url         => url,
    :headers     => headers,
    :proxy       => proxy,
    :ssl_version => ssl_version,
    :ssl_verify  => ssl_verify
  )

  true
end

#get_blob_raw(container, blob, key = nil, options = {}) ⇒ Object

Get the contents of the given blob found in container using the given options. This is a low level method to read a range of bytes from the blob directly. The possible options are:

  • range - A range of bytes to collect.

  • start_byte - The starting byte for collection.

  • end_byte - The end byte for collection. Use this or :length with :start_byte.

  • length - The number of bytes to collect starting at start_byte.

  • entire_image - Read all bytes for the blob.

  • md5 - If true, the response headers will include MD5 checksum information.

  • date - Get the blob snapshot for the given date.

If you do not specify a :range or :start_byte, then an error will be raised unless you explicitly set the :entire_image option to true. However, that is not recommended because the blobs can be huge.

Unlike other methods, this method returns a raw response object rather than a wrapper model. Get the information you need using:

  • response.body - blob data.

  • response.headers - blob metadata.

Example:

ret = @storage_acct.get_blob(@container, @blob, key, :start_byte => start_byte, :length => length)
content_md5  = ret.headers[:content_md5].unpack("m0").first.unpack("H*").first
returned_md5 = Digest::MD5.hexdigest(ret.body)
raise "Checksum error: #{range_str}, blob: #{@container}/#{@blob}" unless content_md5 == returned_md5
return ret.body


408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/azure/armrest/model/storage_account.rb', line 408

def get_blob_raw(container, blob, key = nil, options = {})
  key ||= properties.key1

  url = File.join(properties.primary_endpoints.blob, container, blob)
  url += "?snapshot=" + options[:date] if options[:date]

  additional_headers = {
    'verb' => 'GET'
  }

  range_str = nil
  if options[:range]
    range_str = "bytes=#{options[:range].min}-#{options[:range].max}"
  elsif options[:start_byte]
    range_str = "bytes=#{options[:start_byte]}-"
    if options[:end_byte]
      range_str << options[:end_byte].to_s
    elsif options[:length]
      range_str << (options[:start_byte] + options[:length] - 1).to_s
    end
  end

  if range_str
    additional_headers['x-ms-range'] = range_str
    additional_headers['x-ms-range-get-content-md5'] = true if options[:md5]
  else
    raise ArgumentError, "must specify byte range or entire_image flag" unless options[:entire_image]
  end

  headers = build_headers(url, key, :blob, additional_headers)

  ArmrestService.send(
    :rest_get,
    :url         => url,
    :headers     => headers,
    :proxy       => proxy,
    :ssl_version => ssl_version,
    :ssl_verify  => ssl_verify,
  )
end

#table_data(name, key = nil, options = {}) ⇒ Object

Returns a list of TableData objects for the given table name using account key. The exact nature of the TableData object depends on the type of table that it is.

You may specify :filter, :select or :top as options to restrict your result set.

By default you will receive a maximum of 1000 records. If you wish to receive more records, you will need to use the continuation token. You may also set the :all option to true if you want all records, though we recommend using a filter as well if you use that option as there can be thousands of results.

You may also specify a :NextRowKey, :NextPartitionKey or :NextTableset explicitly for paging. Normally you would just pass the collection’s continuation_token, however. See below for an example.

When using continuation tokens, you should retain your original filtering as well, or you may get unexpected results.

Examples:

# Get the first 10 rows of data from the last 3 days
date = (Time.now - (86400 * 3)).iso8601
my_filter = "timestamp ge datetime'#{date}'"
options = {:top => 10, :filter => my_filter}

results = .table_data(table, key, options)

# Now get the next 10 records
if results.continuation_token
  options[:continuation_token] = results.continuation_token
  more_results = .table_data(table, key, options)
end


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/azure/armrest/model/storage_account.rb', line 98

def table_data(name, key = nil, options = {})
  key ||= properties.key1

  query = build_query(options)

  response = table_response(key, query, name)
  json_response = JSON.parse(response.body)

  data = ArmrestCollection.new(json_response['value'].map { |t| TableData.new(t) })
  data.continuation_token = parse_continuation_tokens(response)

  if options[:all] && data.continuation_token
    options[:continuation_token] = data.continuation_token
    data.push(*table_data(name, key, options))
    data.continuation_token = nil # Clear when finished
  end

  data
end

#table_info(table, key = nil) ⇒ Object

Return information about a single table for the given storage account key. If you are looking for the entities within the table, use the table_data method instead.



57
58
59
60
61
# File 'lib/azure/armrest/model/storage_account.rb', line 57

def table_info(table, key = nil)
  key ||= properties.key1
  response = table_response(key, nil, "Tables('#{table}')")
  Table.new(response.body)
end

#tables(key = nil) ⇒ Object

Returns a list of tables for the given storage account key. Note that full metadata is returned.



47
48
49
50
51
# File 'lib/azure/armrest/model/storage_account.rb', line 47

def tables(key = nil)
  key ||= properties.key1
  response = table_response(key, nil, "Tables")
  JSON.parse(response.body)['value'].map{ |t| Table.new(t) }
end