1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
|
module Fog
module AzureRM
class Storage
# This class is giving implementation of listing blobs.
class Files < Fog::Collection
include Fog::AzureRM::Utilities::General
attribute :directory
attribute :delimiter, aliases: 'Delimiter'
attribute :marker, aliases: 'Marker'
attribute :max_results, aliases: %w(max-results MaxResults max_keys MaxKeys max-keys)
attribute :next_marker, aliases: %w(NextMarker next-marker)
attribute :prefix, aliases: 'Prefix'
model Fog::AzureRM::Storage::File
# List all files(blobs) under the directory.
#
# required attributes: directory
#
# @param options [Hash]
# @option options [String] max_keys or
# max_results Sets the maximum number of files to return.
# @option options [String] delimiter Sets to cause that the operation returns a BlobPrefix element in the response body that acts
# as a placeholder for all files whose names begin with the same substring up to the appearance
# of the delimiter character. The delimiter may be a single character or a string.
# @option options [String] marker Sets the identifier that specifies the portion of the list to be returned.
# @option options [String] prefix Sets filters the results to return only files whose name begins with the specified prefix.
#
# @return [Fog::AzureRM::Storage::Files] Return nil if the directory does not exist.
#
def all(options = {})
requires :directory
options = {
max_results: max_results,
delimiter: delimiter,
marker: marker,
prefix: prefix
}.merge!(options)
options = options.reject { |_key, value| value.nil? || value.to_s.empty? }
merge_attributes(options)
parent = directory.collection.get(
directory.key,
options
)
return nil unless parent
merge_attributes(parent.files.attributes)
load(parent.files.map(&:attributes))
end
# Enumerate every file under the directory if block_given?
#
# @return [Fog::AzureRM::Storage::Files]
#
alias each_file_this_page each
def each
if block_given?
subset = dup.all
subset.each_file_this_page { |f| yield f }
while subset.next_marker
subset = subset.all(marker: subset.next_marker)
subset.each_file_this_page { |f| yield f }
end
end
self
end
# Get the file(blob) with full content as :body with the given name.
#
# required attributes: directory
#
# @param key [String] Name of file
# @param options [Hash]
# @option options [String] block_size Sets buffer size when block_given? is true. Default is 32 MB
#
# @return [Fog::AzureRM::Storage::File] A file. Return nil if the file does not exist.
#
def get(key, options = {}, &block)
requires :directory
blob, content = service.get_blob(directory.key, key, options, &block)
data = parse_storage_object(blob)
file_data = data.merge(
body: content,
key: key
)
new(file_data)
rescue => error
return nil if error.message == 'NotFound'
raise error
end
# Get the URL of the file(blob) with the given name.
#
# required attributes: directory
#
# @param key [String] Name of file
# @param expires [Time] The time at which the shared access signature becomes invalid, in a UTC format.
# @param options [Hash]
# @option options [String] scheme Sets which URL to get, http or https. Options: https or http. Default is https.
#
# @return [String] A URL.
#
def get_url(key, expires, options = {})
requires :directory
if options[:scheme] == 'http'
get_http_url(key, expires, options)
else
get_https_url(key, expires, options)
end
end
# Get the http URL of the file(blob) with the given name.
#
# required attributes: directory
#
# @param key [String] Name of file
# @param expires [Time] The time at which the shared access signature becomes invalid, in a UTC format.
# @param options [Hash] Unused. To keep same interface as other providers.
#
# @return [String] A http URL.
#
def get_http_url(key, expires, options = {})
requires :directory
service.get_blob_http_url(directory.key, key, expires, options)
end
# Get the https URL of the file(blob) with the given name.
#
# required attributes: directory
#
# @param key [String] Name of file
# @param expires [Time] The time at which the shared access signature becomes invalid, in a UTC format.
# @param options [Hash] Unused. To keep same interface as other providers.
#
# @return [String] A https URL.
#
def get_https_url(key, expires, options = {})
requires :directory
service.get_blob_https_url(directory.key, key, expires, options)
end
# Get the file(blob) without content with the given name.
#
# required attributes: directory
#
# @param key [String] Name of file
# @param options [Hash]
#
# @return [Fog::AzureRM::Storage::File] A file. Return nil if the file does not exist.
#
def head(key, options = {})
requires :directory
blob = service.get_blob_properties(directory.key, key, options)
data = parse_storage_object(blob)
file_data = data.merge(key: key)
new(file_data)
rescue => error
return nil if error.message == 'NotFound'
raise error
end
# Create a new file.
#
# required attributes: directory
#
# @return [Fog::AzureRM::Storage::File] A file. You need to use File.save to upload this new file.
#
def new(attributes = {})
requires :directory
super({ directory: directory }.merge!(attributes))
end
end
end
end
end
|