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, ShareDirectory, ShareFile, Table, TableData

Instance Attribute Summary collapse

Attributes inherited from BaseModel

#resource_group, #response_code, #response_headers, #subscription_id

Instance Method Summary collapse

Methods inherited from BaseModel

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

Constructor Details

#initialize(json, skip_accessors_definition = false) ⇒ StorageAccount

Returns a new instance of StorageAccount.



45
46
47
48
# File 'lib/azure/armrest/model/storage_account.rb', line 45

def initialize(json, skip_accessors_definition = false)
  super
  @storage_api_version = '2016-05-31'
end

Instance Attribute Details

#access_keyObject

The default access key used when creating a signature for internal http requests.



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

def access_key
  @access_key
end

#configurationObject

The parent configuration object



43
44
45
# File 'lib/azure/armrest/model/storage_account.rb', line 43

def configuration
  @configuration
end

#storage_api_versionObject

The version string used in headers sent as part any internal http request. The default is 2016-05-31.



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

def storage_api_version
  @storage_api_version
end

Instance Method Details

#add_file_content(share, file, key = access_key, options = {}) ⇒ Object

Add content to file on share. The options hash supports three options, :content, :timeout and :write.

The :content option is just a string, i.e. the content you want to add to the file. Azure allows you to add a maximum of 4mb worth of content per request.

The :timeout option is nil by default. The :write option defaults to ‘update’. If you want to clear a file, set it to ‘clear’.

Raises:

  • (ArgumentError)


375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/azure/armrest/model/storage_account.rb', line 375

def add_file_content(share, file, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  timeout = options.delete(:timeout)
  content = options.delete(:content)

  url = File.join(properties.primary_endpoints.file, share, file) + "?comp=range"
  url += "&timeout=#{timeout}" if timeout

  hash = options.transform_keys { |okey| 'x-ms-' + okey.to_s.tr('_', '-') }

  hash['verb'] = 'PUT'
  hash['x-ms-write'] ||= 'update'

  if hash['x-ms-write'] == 'clear'
    hash['content-length'] = 0
    hash['x-ms-range'] = "bytes=0-"
  else
    range = 0..(content.size - 1)
    hash['content-length'] = content.size
    hash['x-ms-range'] = "bytes=#{range.min}-#{range.max}"
  end

  headers = build_headers(url, key, :file, hash)

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

  Azure::Armrest::ResponseHeaders.new(response.headers).tap do |rh|
    rh.response_code = response.code
  end
end

#all_blobs(key = access_key, max_threads = 10, options = {}) ⇒ Object

Returns an array of all blobs for all containers. The options hash may contain the same arguments that a call to StorageAccount#blobs would accept.

Raises:

  • (ArgumentError)


639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/azure/armrest/model/storage_account.rb', line 639

def all_blobs(key = access_key, max_threads = 10, options = {})
  raise ArgumentError, "No access key specified" unless key

  array = []
  mutex = Mutex.new
  opts = {
    :skip_accessors_definition => options[:skip_accessors_definition]
  }

  Parallel.each(containers(key, opts), :in_threads => max_threads) do |container|
    begin
      mutex.synchronize { array.concat(blobs(container.name_from_hash, key, options)) }
    rescue Errno::ECONNREFUSED, Azure::Armrest::TimeoutException => err
      msg = "Unable to gather blob information for #{container.name_from_hash}: #{err}"
      Azure::Armrest::Configuration.log.try(:log, Logger::WARN, msg)
      next
    end
  end

  array
end

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

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

Raises:

  • (ArgumentError)


675
676
677
678
679
680
681
682
683
684
# File 'lib/azure/armrest/model/storage_account.rb', line 675

def (container, blob, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  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 = access_key, 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.

Raises:

  • (ArgumentError)


509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/azure/armrest/model/storage_account.rb', line 509

def blob_properties(container, blob, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  url = File.join(blob_endpoint_from_hash, 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       => configuration.proxy,
    :ssl_version => configuration.ssl_version,
    :ssl_verify  => configuration.ssl_verify,
    :timeout     => configuration.timeout
  )

  BlobProperty.new(response.headers.merge(:container => container, :name => blob), options[:skip_accessors_definition])
end

#blob_service_properties(key = access_key) ⇒ Object

Returns the blob service properties for the current storage account.

Raises:

  • (ArgumentError)


663
664
665
666
667
668
669
670
# File 'lib/azure/armrest/model/storage_account.rb', line 663

def blob_service_properties(key = access_key)
  raise ArgumentError, "No access key specified" unless key

  response = blob_response(key, "restype=service&comp=properties")
  hash = Hash.from_xml(response.body)['StorageServiceProperties']

  BlobServiceProperty.new(hash)
end

#blob_service_stats(key = access_key) ⇒ 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.

Raises:

  • (ArgumentError)


690
691
692
693
694
695
696
697
# File 'lib/azure/armrest/model/storage_account.rb', line 690

def blob_service_stats(key = access_key)
  raise ArgumentError, "No access key specified" unless key

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

  hash = Hash.from_xml(response.body)['StorageServiceStats']
  BlobServiceStat.new(hash)
end

#blobs(container, key = access_key, options = {}) ⇒ Object

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

The following options are supported:

  • prefix

  • delimiter

  • maxresults

  • include

  • timeout

By default Azure uses a value of 5000 for :maxresults.

If the :include option is specified, it should contain an array of one or more of the following values: snapshots, metadata, copy or uncommittedblobs.

If the :all option is set to true, then this method will collect all records. Otherwise, it is capped at 5000 records by Azure, or whatever you set :maxresults to. If you set both the :all option and the :maxresults option, then all records will be collected in :maxresults batches.

Example:

sas  = Azure::Armrest::StorageAccountService.new(conf)
key  = sas.['key1']
acct = sas.get('your_storage_account', 'your_resource_group')

p acct.blobs('vhds', key)
p acct.blobs('vhds', key, :timeout => 30)
p acct.blobs('vhds', key, :include => ['snapshots', 'metadata'])

Raises:

  • (ArgumentError)


602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/azure/armrest/model/storage_account.rb', line 602

def blobs(container, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = "restype=container&comp=list"
  skip_defs = options[:skip_accessors_definition]

  options.each do |okey, ovalue|
    unless okey == :skip_accessors_definition
      query += "&#{okey}=#{[ovalue].flatten.join(',')}"
    end
  end

  response = blob_response(key, query, container)

  hash = Hash.from_xml(response.body)['EnumerationResults']['Blobs']
  results = []

  if hash && hash['Blob']
    Array.wrap(hash['Blob']).each do |h|
      h[:container] = container
      object = h.key?('Snapshot') ? BlobSnapshot.new(h, skip_defs) : Blob.new(h, skip_defs)
      results << object
    end
  end

  if options[:all] && hash['NextMarker']
    options[:marker] = hash['NextMarker']
    results.concat(blobs(container, key, options))
  end

  results
end

#container_acl(name, key = access_key) ⇒ Object

Returns the properties for the given container name using account key.

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.

Raises:

  • (ArgumentError)


497
498
499
500
501
502
503
504
# File 'lib/azure/armrest/model/storage_account.rb', line 497

def container_acl(name, key = access_key)
  raise ArgumentError, "No access key specified" unless key

  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 = access_key) ⇒ Object

Returns the properties for the given container name using account key.

Raises:

  • (ArgumentError)


483
484
485
486
487
488
489
# File 'lib/azure/armrest/model/storage_account.rb', line 483

def container_properties(name, key = access_key)
  raise ArgumentError, "No access key specified" unless key

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

  ContainerProperty.new(response.headers)
end

#containers(key = access_key, options = {}) ⇒ 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 access_key property.

# The following options are supported:

  • prefix

  • delimiter

  • maxresults

  • include

  • timeout

  • all

By default Azure uses a value of 5000 for :maxresults.

If the :include option is specified, it should contain an array of one element: metadata. More options may be added by Microsoft at a later date.

If the :all option is set to true, then this method will collect all records. Otherwise, it is capped at 5000 records by Azure, or whatever you set :maxresults to. If you set both the :all option and the :maxresults option, then all records will be collected in :maxresults batches.

Example:

sas  = Azure::Armrest::StorageAccountService.new(conf)
key  = sas.['key1']
acct = sas.get('your_storage_account', 'your_resource_group')

p acct.containers(key)
p acct.containers(key, :include => ['metadata'])
p acct.containers(key, :maxresults => 1)

Raises:

  • (ArgumentError)


452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/azure/armrest/model/storage_account.rb', line 452

def containers(key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = "comp=list"
  skip_defs = options[:skip_accessors_definition]

  options.each do |okey, ovalue|
    unless okey == :skip_accessors_definition
      query += "&#{okey}=#{[ovalue].flatten.join(',')}"
    end
  end

  response = blob_response(key, query)

  hash = Hash.from_xml(response.body)['EnumerationResults']['Containers']
  results = []

  if hash && hash['Container']
    Array.wrap(hash['Container']).each { |c| results << Container.new(c, skip_defs) }
  end

  if options[:all] && hash['NextMarker']
    options[:marker] = hash['NextMarker']
    results.concat(containers(key, options))
  end

  results
end

#copy_blob(src_container, src_blob, dst_container, dst_blob = nil, key = access_key) ⇒ 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)

Raises:

  • (ArgumentError)


707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/azure/armrest/model/storage_account.rb', line 707

def copy_blob(src_container, src_blob, dst_container, dst_blob = nil, key = access_key)
  raise ArgumentError, "No access key specified" unless key

  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       => configuration.proxy,
    :ssl_version => configuration.ssl_version,
    :ssl_verify  => configuration.ssl_verify,
    :timeout     => configuration.timeout
  )

  blob = blob_properties(dst_container, dst_blob, key)
  blob.response_headers = Azure::Armrest::ResponseHeaders.new(response.headers)
  blob.response_code = response.code

  blob
end

#copy_file(src_container, src_file, dst_container = src_container, dst_file = nil, key = access_key) ⇒ Object

Copy a src_file to a destination dst_file within the same storage account.

Raises:

  • (ArgumentError)


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/azure/armrest/model/storage_account.rb', line 337

def copy_file(src_container, src_file, dst_container = src_container, dst_file = nil, key = access_key)
  raise ArgumentError, "No access key specified" unless key

  dst_file ||= File.basename(src_blob)

  dst_url = File.join(properties.primary_endpoints.file, dst_container, dst_file)
  src_url = File.join(properties.primary_endpoints.file, src_container, src_file)

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

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

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

  Azure::Armrest::ResponseHeaders.new(response.headers).tap do |rh|
    rh.response_code = response.code
  end
end

#create_blob(container, blob, key = access_key, options = {}) ⇒ Object

Create new blob for a container.

The options parameter is a hash that contains information used when creating the blob:

  • type - “BlockBlob”, “PageBlob” or “AppendBlob”. Mandatory.

  • content_disposition

  • content_encoding

  • content_language

  • content_md5

  • content_type

  • cache_control

  • lease_id

  • payload (block blobs only)

  • sequence_number (page blobs only)

  • timeout (part of the request)

Returns a ResponseHeaders object since this method is asynchronous.

Raises:

  • (ArgumentError)


784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
# File 'lib/azure/armrest/model/storage_account.rb', line 784

def create_blob(container, blob, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  timeout = options.delete(:timeout)
  payload = options.delete(:payload) || ''

  url = File.join(properties.primary_endpoints.blob, container, blob)
  url += "&timeout=#{timeout}" if timeout

  hash = options.transform_keys do |okey|
    if okey.to_s =~ /^if/i
      okey.to_s.tr('_', '-')
    elsif %w[date meta_name lease_id version].include?(okey.to_s)
      'x-ms-' + okey.to_s.tr('_', '-')
    else
      'x-ms-blob-' + okey.to_s.tr('_', '-')
    end
  end

  unless hash['x-ms-blob-type']
    raise ArgumentError, "The :type option must be specified"
  end

  hash['x-ms-date'] ||= Time.now.httpdate
  hash['x-ms-version'] ||= storage_api_version
  hash['verb'] = 'PUT'

  # Content length must be 0 (blank) for Page or Append blobs
  if %w[pageblob appendblob].include?(hash['x-ms-blob-type'].downcase)
    hash['content-length'] = ''
  else
    hash['content-length'] ||= hash['x-ms-blob-content-length']
  end

  # Override the default empty string
  hash['content-type'] ||= hash['x-ms-blob-content-type'] || 'application/octet-stream'

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

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

  resp_headers = Azure::Armrest::ResponseHeaders.new(response.headers)
  resp_headers.response_code = response.code

  resp_headers
end

#create_blob_snapshot(container, blob, key = access_key, options = {}) ⇒ Object

Create a read-only snapshot of a blob.

Possible options are:

  • meta_name

  • lease_id

  • client_request_id

  • if_modified_since

  • if_unmodified_since

  • if_match

  • if_none_match

  • timeout

Returns a ResponseHeaders object since this is an asynchronous method.

Raises:

  • (ArgumentError)


855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
# File 'lib/azure/armrest/model/storage_account.rb', line 855

def create_blob_snapshot(container, blob, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  timeout = options.delete(:timeout) # Part of request

  url = File.join(properties.primary_endpoints.blob, container, blob) + "?comp=snapshot"
  url += "&timeout=#{timeout}" if timeout

  hash = options.transform_keys do |okey|
    if okey.to_s =~ /^if/i
      okey.to_s.tr('_', '-')
    else
      'x-ms-blob-' + okey.to_s.tr('_', '-')
    end
  end

  hash['verb'] = 'PUT'

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

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

  headers = Azure::Armrest::ResponseHeaders.new(response.headers)
  headers.response_code = response.code

  headers
end

#create_directory(share, directory, key = access_key, options = {}) ⇒ Object

Create a new directory under the specified share or parent directory.

The only supported option at this time is a “timeout” option.

Raises:

  • (ArgumentError)


131
132
133
134
135
136
137
138
139
140
141
# File 'lib/azure/armrest/model/storage_account.rb', line 131

def create_directory(share, directory, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = {:restype => 'directory'}.merge(options).to_query

  response = file_response(key, query, 'put', '', File.join(share, directory))

  Azure::Armrest::ResponseHeaders.new(response.headers).tap do |rh|
    rh.response_code = response.code
  end
end

#create_file(share, file, key = access_key, options = {}) ⇒ Object

Create the specified share file. You may specify any of the following options:

  • cache_control

  • content_disposition

  • content_length (default: 0)

  • content_encoding

  • content_language

  • content_md5

  • content_type (default: application/octet-stream)

  • meta_name

  • timeout

  • version

Note that this does not set the content of the file, it only creates in the file share.

Raises:

  • (ArgumentError)


284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/azure/armrest/model/storage_account.rb', line 284

def create_file(share, file, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  timeout = options.delete(:timeout) # Part of request

  url = File.join(properties.primary_endpoints.file, share, file)
  url += "?timeout=#{timeout}" if timeout

  hash = options.transform_keys { |okey| 'x-ms-' + okey.to_s.tr('_', '-') }

  hash['verb'] = 'PUT'

  # Mandatory and/or sane defaults
  hash['x-ms-type'] = 'file'
  hash['x-ms-content-length'] ||= 0
  hash['x-ms-content-type'] ||= 'application/octet-stream'

  headers = build_headers(url, key, :file, hash)

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

  Azure::Armrest::ResponseHeaders.new(response.headers).tap do |rh|
    rh.response_code = response.code
  end
end

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

Delete the given blob found in container. Pass a :date option if you wish to delete a snapshot.

Raises:

  • (ArgumentError)


740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/azure/armrest/model/storage_account.rb', line 740

def delete_blob(container, blob, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

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

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

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

  headers = Azure::Armrest::ResponseHeaders.new(response.headers)
  headers.response_code = response.code

  headers
end

#delete_directory(share, directory, key = access_key, options = {}) ⇒ Object

Delete the specified share or parent directory.

The only supported option at this time is a “timeout” option.

Raises:

  • (ArgumentError)


147
148
149
150
151
152
153
154
155
156
157
# File 'lib/azure/armrest/model/storage_account.rb', line 147

def delete_directory(share, directory, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = {:restype => 'directory'}.merge(options).to_query

  response = file_response(key, query, 'delete', '', File.join(share, directory))

  Azure::Armrest::ResponseHeaders.new(response.headers).tap do |rh|
    rh.response_code = response.code
  end
end

#delete_file(share, file, key = access_key, options = {}) ⇒ Object

Delete the specified share file.

The only supported option at this time is a “timeout” option.

Raises:

  • (ArgumentError)


323
324
325
326
327
328
329
330
331
332
333
# File 'lib/azure/armrest/model/storage_account.rb', line 323

def delete_file(share, file, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = options.to_query

  response = file_response(key, query, 'delete', '', File.join(share, file))

  Azure::Armrest::ResponseHeaders.new(response.headers).tap do |rh|
    rh.response_code = response.code
  end
end

#directory_metadata(share, directory, key = access_key, options = {}) ⇒ Object

Get metadata for the specified share or parent directory.

The only supported option at this time is a “timeout” option.

Raises:

  • (ArgumentError)


177
178
179
180
181
182
183
184
185
# File 'lib/azure/armrest/model/storage_account.rb', line 177

def (share, directory, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = {:restype => 'directory', :comp => 'metadata'}.merge(options).to_query

  response = file_response(key, query, 'head', '', File.join(share, directory))

  ShareDirectory.new(response.headers)
end

#directory_properties(share, directory, key = access_key, options = {}) ⇒ Object

Get properties for the specified share or parent directory.

The only supported option at this time is a “timeout” option.

Raises:

  • (ArgumentError)


163
164
165
166
167
168
169
170
171
# File 'lib/azure/armrest/model/storage_account.rb', line 163

def directory_properties(share, directory, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = {:restype => 'directory'}.merge(options).to_query

  response = file_response(key, query, 'get', '', File.join(share, directory))

  ShareDirectory.new(response.headers)
end

#file_content(share, file, key = access_key, options = {}) ⇒ Object

Returns the raw contents of the specified file.

The only supported option at this time is a “timeout” option.

Raises:

  • (ArgumentError)


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

def file_content(share, file, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = options.to_query

  response = file_response(key, query, 'get', '', File.join(share, file))
  response.body
end

#file_properties(share, file, key = access_key, options = {}) ⇒ Object

Returns the raw contents of the specified file.

The only supported option at this time is a “timeout” option.

Raises:

  • (ArgumentError)


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

def file_properties(share, file, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = options.to_query

  response = file_response(key, query, 'head', '', File.join(share, file))

  Azure::Armrest::ResponseHeaders.new(response.headers).tap do |rh|
    rh.response_code = response.code
  end
end

#files(share, key = access_key, options = {}) ⇒ Object

Returns a list of files for the specified file-share. You may also optionally specify a directory in “share/directory” format.

You may specify multiple options to limit the result set. The possible options are:

  • prefix

  • marker

  • maxresults

  • timeout

  • all

If the :all option is set to true, then this method will collect all records. Otherwise, it is capped at 5000 records by Azure, or whatever you set :maxresults to. If you set both the :all option and the :maxresults option, then all records will be collected in :maxresults batches.

Raises:

  • (ArgumentError)


204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/azure/armrest/model/storage_account.rb', line 204

def files(share, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = "comp=list&restype=directory"

  skip_defs = options[:skip_accessors_definition]

  options.each do |okey, ovalue|
    unless okey == :skip_accessors_definition
      query += "&#{okey}=#{[ovalue].flatten.join(',')}"
    end
  end

  response = file_response(key, query, 'get', nil, share)

  hash = Hash.from_xml(response.body)['EnumerationResults']['Entries']
  results = []

  if hash && hash['Directory']
    Array.wrap(hash['Directory']).each { |dir| results << ShareDirectory.new(dir, skip_defs) }
  end

  if hash && hash['File']
    Array.wrap(hash['File']).each { |file| results << ShareFile.new(file, skip_defs) }
  end

  if options[:all] && hash['NextMarker']
    options[:marker] = hash['NextMarker']
    results.concat(files(share, key, options))
  end

  results
end

#get_blob_raw(container, blob, key = access_key, 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

Raises:

  • (ArgumentError)


922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
# File 'lib/azure/armrest/model/storage_account.rb', line 922

def get_blob_raw(container, blob, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  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       => configuration.proxy,
    :ssl_version => configuration.ssl_version,
    :ssl_verify  => configuration.ssl_verify,
    :timeout     => configuration.timeout
  )
end

#table_data(name, key = access_key, 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

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/azure/armrest/model/storage_account.rb', line 104

def table_data(name, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

  query = build_query(options)
  response = table_response(key, query, name)

  klass = Azure::Armrest::StorageAccount::TableData
  data  = Azure::Armrest::ArmrestCollection.create_from_response(response, klass)

  # Continuation tokens are parsed differently for storage
  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 = access_key) ⇒ 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.

Raises:

  • (ArgumentError)


63
64
65
66
67
# File 'lib/azure/armrest/model/storage_account.rb', line 63

def table_info(table, key = access_key)
  raise ArgumentError, "No access key specified" unless key
  response = table_response(key, nil, "Tables('#{table}')")
  Table.new(response.body)
end

#tables(key = access_key) ⇒ Object

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

Raises:

  • (ArgumentError)


53
54
55
56
57
# File 'lib/azure/armrest/model/storage_account.rb', line 53

def tables(key = access_key)
  raise ArgumentError, "No access key specified" unless key
  response = table_response(key, nil, "Tables")
  JSON.parse(response.body)['value'].map { |t| Table.new(t) }
end

#update_blob_properties(container, blob, key = access_key, options = {}) ⇒ Object

Update the given blob in container with the provided options. The possible options are:

cache_control content_disposition content_encoding content_language content_length content_md5 content_type lease_id version

The content_length option is only value for page blobs, and is used to resize the blob.

Raises:

  • (ArgumentError)


546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
# File 'lib/azure/armrest/model/storage_account.rb', line 546

def update_blob_properties(container, blob, key = access_key, options = {})
  raise ArgumentError, "No access key specified" unless key

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

  hash = options.transform_keys { |okey| "x-ms-blob-" + okey.to_s.tr('_', '-') }

  hash['verb'] = 'PUT'

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

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

  BlobProperty.new(response.headers.merge(:container => container, :name => blob))
end