azure-storage-file 1.0.0 → 2.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,343 +1,346 @@
1
- # frozen_string_literal: true
2
-
3
- #-------------------------------------------------------------------------
4
- # # Copyright (c) Microsoft and contributors. All rights reserved.
5
- #
6
- # The MIT License(MIT)
7
-
8
- # Permission is hereby granted, free of charge, to any person obtaining a copy
9
- # of this software and associated documentation files(the "Software"), to deal
10
- # in the Software without restriction, including without limitation the rights
11
- # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12
- # copies of the Software, and to permit persons to whom the Software is
13
- # furnished to do so, subject to the following conditions :
14
-
15
- # The above copyright notice and this permission notice shall be included in
16
- # all copies or substantial portions of the Software.
17
-
18
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
- # THE SOFTWARE.
25
- #--------------------------------------------------------------------------
26
- require "azure/storage/file/file"
27
-
28
- module Azure::Storage
29
- include Azure::Storage::Common::Service
30
- StorageService = Azure::Storage::Common::Service::StorageService
31
-
32
- module File
33
- class FileService < StorageService
34
- include Azure::Storage::Common::Core::Utility
35
- include Azure::Storage::File::Share
36
- include Azure::Storage::File::Directory
37
- include Azure::Storage::File
38
-
39
- class << self
40
- # Public: Creates an instance of [Azure::Storage::File::FileService]
41
- #
42
- # ==== Attributes
43
- #
44
- # * +options+ - Hash. Optional parameters.
45
- #
46
- # ==== Options
47
- #
48
- # Accepted key/value pairs in options parameter are:
49
- #
50
- # * +:use_development_storage+ - TrueClass|FalseClass. Whether to use storage emulator.
51
- # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
52
- # * +:storage_account_name+ - String. The name of the storage account.
53
- # * +:storage_access_key+ - Base64 String. The access key of the storage account.
54
- # * +:storage_sas_token+ - String. The signed access signature for the storage account or one of its service.
55
- # * +:storage_file_host+ - String. Specified File service endpoint or hostname
56
- # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Service, to
57
- # * +:default_endpoints_protocol+ - String. http or https
58
- # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
59
- # * +:ca_file+ - String. File path of the CA file if having issue with SSL
60
- # * +:user_agent_prefix+ - String. The user agent prefix that can identify the application calls the library
61
- #
62
- # The valid set of options include:
63
- # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
64
- # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
65
- # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
66
- # * Specified hosts and SAS token: At least one of the service host and SAS token. It's up to user to ensure the SAS token is suitable for the serivce
67
- # * Anonymous File: only +:storage_file_host+, if it is to only access files within a container
68
- #
69
- # Additional notes:
70
- # * Specified hosts can be set when use account name with access key or sas token
71
- # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
72
- # * Storage emulator always use path style URI
73
- # * +:ca_file+ is independent.
74
- #
75
- # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::Common::ClientOptions.env_vars_mapping] for the mapping relationship
76
- #
77
- # @return [Azure::Storage::File::FileService]
78
- def create(options = {}, &block)
79
- service_options = { client: Azure::Storage::Common::Client::create(options, &block), api_version: Azure::Storage::File::Default::STG_VERSION }
80
- service_options[:user_agent_prefix] = options[:user_agent_prefix] if options[:user_agent_prefix]
81
- Azure::Storage::File::FileService.new(service_options, &block)
82
- end
83
-
84
- # Public: Creates an instance of [Azure::Storage::File::FileService] with Storage Emulator
85
- #
86
- # ==== Attributes
87
- #
88
- # * +proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
89
- #
90
- # @return [Azure::Storage::File::FileService]
91
- def create_development(proxy_uri = nil, &block)
92
- service_options = { client: Azure::Storage::Common::Client::create_development(proxy_uri, &block), api_version: Azure::Storage::File::Default::STG_VERSION }
93
- Azure::Storage::File::FileService.new(service_options, &block)
94
- end
95
-
96
- # Public: Creates an instance of [Azure::Storage::File::FileService] from Environment Variables
97
- #
98
- # @return [Azure::Storage::File::FileService]
99
- def create_from_env(&block)
100
- service_options = { client: Azure::Storage::Common::Client::create_from_env(&block), api_version: Azure::Storage::File::Default::STG_VERSION }
101
- Azure::Storage::File::FileService.new(service_options, &block)
102
- end
103
-
104
- # Public: Creates an instance of [Azure::Storage::File::FileService] from Environment Variables
105
- #
106
- # ==== Attributes
107
- #
108
- # * +connection_string+ - String. Please refer to https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/.
109
- #
110
- # @return [Azure::Storage::File::FileService]
111
- def create_from_connection_string(connection_string, &block)
112
- service_options = { client: Azure::Storage::Common::Client::create_from_connection_string(connection_string, &block), api_version: Azure::Storage::File::Default::STG_VERSION }
113
- Azure::Storage::File::FileService.new(service_options, &block)
114
- end
115
- end
116
-
117
- # Public: Initializes an instance of [Azure::Storage::File::FileService]
118
- #
119
- # ==== Attributes
120
- #
121
- # * +options+ - Hash. Optional parameters.
122
- #
123
- # ==== Options
124
- #
125
- # Accepted key/value pairs in options parameter are:
126
- #
127
- # * +:use_development_storage+ - TrueClass|FalseClass. Whether to use storage emulator.
128
- # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
129
- # * +:storage_connection_string+ - String. The storage connection string.
130
- # * +:storage_account_name+ - String. The name of the storage account.
131
- # * +:storage_access_key+ - Base64 String. The access key of the storage account.
132
- # * +:storage_sas_token+ - String. The signed access signature for the storage account or one of its service.
133
- # * +:storage_file_host+ - String. Specified File serivce endpoint or hostname
134
- # * +:storage_table_host+ - String. Specified Table serivce endpoint or hostname
135
- # * +:storage_queue_host+ - String. Specified Queue serivce endpoint or hostname
136
- # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Serivce, to
137
- # * +:default_endpoints_protocol+ - String. http or https
138
- # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
139
- # * +:ca_file+ - String. File path of the CA file if having issue with SSL
140
- # * +:user_agent_prefix+ - String. The user agent prefix that can identify the application calls the library
141
- # * +:client+ - Azure::Storage::Common::Client. The common client used to initalize the service.
142
- #
143
- # The valid set of options include:
144
- # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
145
- # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
146
- # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
147
- # * Specified hosts and SAS token: At least one of the service host and SAS token. It's up to user to ensure the SAS token is suitable for the serivce
148
- # * Azure::Storage::Common::Client: The common client used to initalize the service. This client can be initalized and used repeatedly.
149
- # * Anonymous File: only +:storage_file_host+, if it is to only access files within a container
150
- #
151
- # Additional notes:
152
- # * Specified hosts can be set when use account name with access key or sas token
153
- # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
154
- # * Storage emulator always use path style URI
155
- # * +:ca_file+ is independent.
156
- #
157
- # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::Common::ClientOptions.env_vars_mapping] for the mapping relationship
158
- def initialize(options = {}, &block)
159
- service_options = options.clone
160
- client_config = service_options[:client] ||= Azure::Storage::Common::Client::create(service_options, &block)
161
- @user_agent_prefix = service_options[:user_agent_prefix] if service_options[:user_agent_prefix]
162
- @api_version = service_options[:api_version] || Azure::Storage::File::Default::STG_VERSION
163
- signer = service_options[:signer] || client_config.signer || Azure::Storage::Common::Core::Auth::SharedKey.new(client_config.storage_account_name, client_config.storage_access_key)
164
- signer.api_ver = @api_version if signer.is_a? Azure::Storage::Common::Core::Auth::SharedAccessSignatureSigner
165
- super(signer, client_config.storage_account_name, service_options, &block)
166
- @storage_service_host[:primary] = client.storage_file_host
167
- @storage_service_host[:secondary] = client.storage_file_host true
168
- end
169
-
170
- def call(method, uri, body = nil, headers = {}, options = {})
171
- content_type = get_or_apply_content_type(body, headers[Azure::Storage::Common::HeaderConstants::FILE_CONTENT_TYPE])
172
- headers[Azure::Storage::Common::HeaderConstants::FILE_CONTENT_TYPE] = content_type if content_type
173
- headers["x-ms-version"] = @api_version ? @api_version : Default::STG_VERSION
174
- headers["User-Agent"] = @user_agent_prefix ? "#{@user_agent_prefix}; #{Default::USER_AGENT}" : Default::USER_AGENT
175
-
176
- response = super
177
-
178
- # Force the response.body to the content charset of specified in the header.
179
- # Content-Type is echo'd back for the file and is used to store the encoding of the octet stream
180
- if !response.nil? && !response.body.nil? && response.headers["Content-Type"]
181
- charset = parse_charset_from_content_type(response.headers["Content-Type"])
182
- response.body.force_encoding(charset) if charset && charset.length > 0
183
- end
184
-
185
- response
186
- end
187
-
188
- # Public: Get a list of Shares from the server.
189
- #
190
- # ==== Attributes
191
- #
192
- # * +options+ - Hash. Optional parameters.
193
- #
194
- # ==== Options
195
- #
196
- # Accepted key/value pairs in options parameter are:
197
- # * +:prefix+ - String. Filters the results to return only shares
198
- # whose name begins with the specified prefix. (optional)
199
- #
200
- # * +:marker+ - String. An identifier the specifies the portion of the
201
- # list to be returned. This value comes from the property
202
- # Azure::Storage::Common::EnumerationResults.continuation_token when there
203
- # are more shares available than were returned. The
204
- # marker value may then be used here to request the next set
205
- # of list items. (optional)
206
- #
207
- # * +:max_results+ - Integer. Specifies the maximum number of shares to return.
208
- # If max_results is not specified, or is a value greater than
209
- # 5,000, the server will return up to 5,000 items. If it is set
210
- # to a value less than or equal to zero, the server will return
211
- # status code 400 (Bad Request). (optional)
212
- #
213
- # * +:metadata+ - Boolean. Specifies whether or not to return the share metadata.
214
- # (optional, Default=false)
215
- #
216
- # * +:timeout+ - Integer. A timeout in seconds.
217
- #
218
- # * +:request_id+ - String. Provides a client-generated, opaque value with a 1 KB character limit that is recorded
219
- # in the analytics logs when storage analytics logging is enabled.
220
- #
221
- # * +:location_mode+ - LocationMode. Specifies the location mode used to decide
222
- # which location the request should be sent to.
223
- #
224
- # See: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/list-shares
225
- #
226
- # Returns an Azure::Storage::Common::EnumerationResults
227
- #
228
- def list_shares(options = {})
229
- query = {}
230
- if options
231
- StorageService.with_query query, "prefix", options[:prefix]
232
- StorageService.with_query query, "marker", options[:marker]
233
- StorageService.with_query query, "maxresults", options[:max_results].to_s if options[:max_results]
234
- StorageService.with_query query, "include", "metadata" if options[:metadata] == true
235
- StorageService.with_query query, "timeout", options[:timeout].to_s if options[:timeout]
236
- end
237
-
238
- options[:request_location_mode] = Azure::Storage::Common::RequestLocationMode::PRIMARY_OR_SECONDARY
239
- uri = shares_uri(query, options)
240
- response = call(:get, uri, nil, {}, options)
241
-
242
- Serialization.share_enumeration_results_from_xml(response.body)
243
- end
244
-
245
- # Protected: Generate the URI for the collection of shares.
246
- #
247
- # ==== Attributes
248
- #
249
- # * +query+ - A Hash of key => value query parameters.
250
- #
251
- # Returns a URI.
252
- #
253
- protected
254
- def shares_uri(query = {}, options = {})
255
- query = { "comp" => "list" }.merge(query)
256
- generate_uri("", query, options)
257
- end
258
-
259
- # Protected: Generate the URI for a specific share.
260
- #
261
- # ==== Attributes
262
- #
263
- # * +name+ - The share name. If this is a URI, we just return this.
264
- # * +query+ - A Hash of key => value query parameters.
265
- #
266
- # Returns a URI.
267
- #
268
- protected
269
- def share_uri(name, query = {}, options = {})
270
- return name if name.kind_of? ::URI
271
- query = { restype: "share" }.merge(query)
272
- generate_uri(name, query, options)
273
- end
274
-
275
- # Protected: Generate the URI for a specific directory.
276
- #
277
- # ==== Attributes
278
- #
279
- # * +share+ - String representing the name of the share.
280
- # * +directory_path+ - String representing the path to the directory.
281
- # * +directory+ - String representing the name to the directory.
282
- # * +query+ - A Hash of key => value query parameters.
283
- #
284
- # Returns a URI.
285
- #
286
- protected
287
- def directory_uri(share, directory_path, query = {}, options = {})
288
- path = directory_path.nil? ? share : ::File.join(share, directory_path)
289
- query = { restype: "directory" }.merge(query)
290
- options = { encode: true }.merge(options)
291
- generate_uri(path, query, options)
292
- end
293
-
294
- # Protected: Generate the URI for a specific file.
295
- #
296
- # ==== Attributes
297
- #
298
- # * +share+ - String representing the name of the share.
299
- # * +directory_path+ - String representing the path to the directory.
300
- # * +file+ - String representing the name to the file.
301
- # * +query+ - A Hash of key => value query parameters.
302
- #
303
- # Returns a URI.
304
- #
305
- protected
306
- def file_uri(share, directory_path, file, query = {}, options = {})
307
- if directory_path.nil?
308
- path = ::File.join(share, file)
309
- else
310
- path = ::File.join(share, directory_path, file)
311
- end
312
- options = { encode: true }.merge(options)
313
- generate_uri(path, query, options)
314
- end
315
-
316
- # Get the content type according to the content type header and request body.
317
- #
318
- # headers - The request body
319
- # content_type - The request content type
320
- protected
321
- def get_or_apply_content_type(body, content_type = nil)
322
- unless body.nil?
323
- if (body.is_a? String) && body.encoding.to_s != "ASCII_8BIT" && !body.empty?
324
- if content_type.nil?
325
- content_type = "text/plain; charset=#{body.encoding}"
326
- else
327
- # Force the request.body to the content encoding of specified in the header
328
- charset = parse_charset_from_content_type(content_type)
329
- body.force_encoding(charset) if charset
330
- end
331
- else
332
- # It is either that the body is not a string, or that the body's encoding is ASCII_8BIT, which is a binary
333
- # In this case, set the content type to be default content-type
334
- content_type = Default::CONTENT_TYPE_VALUE unless content_type
335
- end
336
- end
337
- content_type
338
- end
339
- end
340
- end
341
- end
342
-
343
- Azure::Storage::FileService = Azure::Storage::File::FileService
1
+ # frozen_string_literal: true
2
+
3
+ #-------------------------------------------------------------------------
4
+ # # Copyright (c) Microsoft and contributors. All rights reserved.
5
+ #
6
+ # The MIT License(MIT)
7
+
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
9
+ # of this software and associated documentation files(the "Software"), to deal
10
+ # in the Software without restriction, including without limitation the rights
11
+ # to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12
+ # copies of the Software, and to permit persons to whom the Software is
13
+ # furnished to do so, subject to the following conditions :
14
+
15
+ # The above copyright notice and this permission notice shall be included in
16
+ # all copies or substantial portions of the Software.
17
+
18
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
+ # THE SOFTWARE.
25
+ #--------------------------------------------------------------------------
26
+ require "azure/storage/file/file"
27
+
28
+ module Azure::Storage
29
+ include Azure::Storage::Common::Service
30
+ StorageService = Azure::Storage::Common::Service::StorageService
31
+
32
+ module File
33
+ class FileService < StorageService
34
+ include Azure::Storage::Common::Core::Utility
35
+ include Azure::Storage::File::Share
36
+ include Azure::Storage::File::Directory
37
+ include Azure::Storage::File
38
+
39
+ class << self
40
+ # Public: Creates an instance of [Azure::Storage::File::FileService]
41
+ #
42
+ # ==== Attributes
43
+ #
44
+ # * +options+ - Hash. Optional parameters.
45
+ #
46
+ # ==== Options
47
+ #
48
+ # Accepted key/value pairs in options parameter are:
49
+ #
50
+ # * +:use_development_storage+ - TrueClass|FalseClass. Whether to use storage emulator.
51
+ # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
52
+ # * +:storage_account_name+ - String. The name of the storage account.
53
+ # * +:storage_access_key+ - Base64 String. The access key of the storage account.
54
+ # * +:storage_sas_token+ - String. The signed access signature for the storage account or one of its service.
55
+ # * +:storage_file_host+ - String. Specified File service endpoint or hostname
56
+ # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Service, to
57
+ # * +:default_endpoints_protocol+ - String. http or https
58
+ # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
59
+ # * +:ca_file+ - String. File path of the CA file if having issue with SSL
60
+ # * +:ssl_version+ - Symbol. The ssl version to be used, sample: :TLSv1_1, :TLSv1_2, for the details, see https://github.com/ruby/openssl/blob/master/lib/openssl/ssl.rb
61
+ # * +:ssl_min_version+ - Symbol. The min ssl version supported, only supported in Ruby 2.5+
62
+ # * +:ssl_max_version+ - Symbol. The max ssl version supported, only supported in Ruby 2.5+
63
+ # * +:user_agent_prefix+ - String. The user agent prefix that can identify the application calls the library
64
+ #
65
+ # The valid set of options include:
66
+ # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
67
+ # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
68
+ # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
69
+ # * Specified hosts and SAS token: At least one of the service host and SAS token. It's up to user to ensure the SAS token is suitable for the serivce
70
+ # * Anonymous File: only +:storage_file_host+, if it is to only access files within a container
71
+ #
72
+ # Additional notes:
73
+ # * Specified hosts can be set when use account name with access key or sas token
74
+ # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
75
+ # * Storage emulator always use path style URI
76
+ # * +:ca_file+ is independent.
77
+ #
78
+ # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::Common::ClientOptions.env_vars_mapping] for the mapping relationship
79
+ #
80
+ # @return [Azure::Storage::File::FileService]
81
+ def create(options = {}, &block)
82
+ service_options = { client: Azure::Storage::Common::Client::create(options, &block), api_version: Azure::Storage::File::Default::STG_VERSION }
83
+ service_options[:user_agent_prefix] = options[:user_agent_prefix] if options[:user_agent_prefix]
84
+ Azure::Storage::File::FileService.new(service_options, &block)
85
+ end
86
+
87
+ # Public: Creates an instance of [Azure::Storage::File::FileService] with Storage Emulator
88
+ #
89
+ # ==== Attributes
90
+ #
91
+ # * +proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
92
+ #
93
+ # @return [Azure::Storage::File::FileService]
94
+ def create_development(proxy_uri = nil, &block)
95
+ service_options = { client: Azure::Storage::Common::Client::create_development(proxy_uri, &block), api_version: Azure::Storage::File::Default::STG_VERSION }
96
+ Azure::Storage::File::FileService.new(service_options, &block)
97
+ end
98
+
99
+ # Public: Creates an instance of [Azure::Storage::File::FileService] from Environment Variables
100
+ #
101
+ # @return [Azure::Storage::File::FileService]
102
+ def create_from_env(&block)
103
+ service_options = { client: Azure::Storage::Common::Client::create_from_env(&block), api_version: Azure::Storage::File::Default::STG_VERSION }
104
+ Azure::Storage::File::FileService.new(service_options, &block)
105
+ end
106
+
107
+ # Public: Creates an instance of [Azure::Storage::File::FileService] from Environment Variables
108
+ #
109
+ # ==== Attributes
110
+ #
111
+ # * +connection_string+ - String. Please refer to https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/.
112
+ #
113
+ # @return [Azure::Storage::File::FileService]
114
+ def create_from_connection_string(connection_string, &block)
115
+ service_options = { client: Azure::Storage::Common::Client::create_from_connection_string(connection_string, &block), api_version: Azure::Storage::File::Default::STG_VERSION }
116
+ Azure::Storage::File::FileService.new(service_options, &block)
117
+ end
118
+ end
119
+
120
+ # Public: Initializes an instance of [Azure::Storage::File::FileService]
121
+ #
122
+ # ==== Attributes
123
+ #
124
+ # * +options+ - Hash. Optional parameters.
125
+ #
126
+ # ==== Options
127
+ #
128
+ # Accepted key/value pairs in options parameter are:
129
+ #
130
+ # * +:use_development_storage+ - TrueClass|FalseClass. Whether to use storage emulator.
131
+ # * +:development_storage_proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
132
+ # * +:storage_connection_string+ - String. The storage connection string.
133
+ # * +:storage_account_name+ - String. The name of the storage account.
134
+ # * +:storage_access_key+ - Base64 String. The access key of the storage account.
135
+ # * +:storage_sas_token+ - String. The signed access signature for the storage account or one of its service.
136
+ # * +:storage_file_host+ - String. Specified File serivce endpoint or hostname
137
+ # * +:storage_table_host+ - String. Specified Table serivce endpoint or hostname
138
+ # * +:storage_queue_host+ - String. Specified Queue serivce endpoint or hostname
139
+ # * +:storage_dns_suffix+ - String. The suffix of a regional Storage Serivce, to
140
+ # * +:default_endpoints_protocol+ - String. http or https
141
+ # * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
142
+ # * +:ca_file+ - String. File path of the CA file if having issue with SSL
143
+ # * +:user_agent_prefix+ - String. The user agent prefix that can identify the application calls the library
144
+ # * +:client+ - Azure::Storage::Common::Client. The common client used to initalize the service.
145
+ #
146
+ # The valid set of options include:
147
+ # * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
148
+ # * Storage account name and key: +:storage_account_name+ and +:storage_access_key+ required, set +:storage_dns_suffix+ necessarily
149
+ # * Storage account name and SAS token: +:storage_account_name+ and +:storage_sas_token+ required, set +:storage_dns_suffix+ necessarily
150
+ # * Specified hosts and SAS token: At least one of the service host and SAS token. It's up to user to ensure the SAS token is suitable for the serivce
151
+ # * Azure::Storage::Common::Client: The common client used to initalize the service. This client can be initalized and used repeatedly.
152
+ # * Anonymous File: only +:storage_file_host+, if it is to only access files within a container
153
+ #
154
+ # Additional notes:
155
+ # * Specified hosts can be set when use account name with access key or sas token
156
+ # * +:default_endpoints_protocol+ can be set if the scheme is not specified in hosts
157
+ # * Storage emulator always use path style URI
158
+ # * +:ca_file+ is independent.
159
+ #
160
+ # When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::Common::ClientOptions.env_vars_mapping] for the mapping relationship
161
+ def initialize(options = {}, &block)
162
+ service_options = options.clone
163
+ client_config = service_options[:client] ||= Azure::Storage::Common::Client::create(service_options, &block)
164
+ @user_agent_prefix = service_options[:user_agent_prefix] if service_options[:user_agent_prefix]
165
+ @api_version = service_options[:api_version] || Azure::Storage::File::Default::STG_VERSION
166
+ signer = service_options[:signer] || client_config.signer || Azure::Storage::Common::Core::Auth::SharedKey.new(client_config.storage_account_name, client_config.storage_access_key)
167
+ signer.api_ver = @api_version if signer.is_a? Azure::Storage::Common::Core::Auth::SharedAccessSignatureSigner
168
+ super(signer, client_config.storage_account_name, service_options, &block)
169
+ @storage_service_host[:primary] = client.storage_file_host
170
+ @storage_service_host[:secondary] = client.storage_file_host true
171
+ end
172
+
173
+ def call(method, uri, body = nil, headers = {}, options = {})
174
+ content_type = get_or_apply_content_type(body, headers[Azure::Storage::Common::HeaderConstants::FILE_CONTENT_TYPE])
175
+ headers[Azure::Storage::Common::HeaderConstants::FILE_CONTENT_TYPE] = content_type if content_type
176
+ headers["x-ms-version"] = @api_version ? @api_version : Default::STG_VERSION
177
+ headers["User-Agent"] = @user_agent_prefix ? "#{@user_agent_prefix}; #{Default::USER_AGENT}" : Default::USER_AGENT
178
+
179
+ response = super
180
+
181
+ # Force the response.body to the content charset of specified in the header.
182
+ # Content-Type is echo'd back for the file and is used to store the encoding of the octet stream
183
+ if !response.nil? && !response.body.nil? && response.headers["Content-Type"]
184
+ charset = parse_charset_from_content_type(response.headers["Content-Type"])
185
+ response.body.force_encoding(charset) if charset && charset.length > 0
186
+ end
187
+
188
+ response
189
+ end
190
+
191
+ # Public: Get a list of Shares from the server.
192
+ #
193
+ # ==== Attributes
194
+ #
195
+ # * +options+ - Hash. Optional parameters.
196
+ #
197
+ # ==== Options
198
+ #
199
+ # Accepted key/value pairs in options parameter are:
200
+ # * +:prefix+ - String. Filters the results to return only shares
201
+ # whose name begins with the specified prefix. (optional)
202
+ #
203
+ # * +:marker+ - String. An identifier the specifies the portion of the
204
+ # list to be returned. This value comes from the property
205
+ # Azure::Storage::Common::EnumerationResults.continuation_token when there
206
+ # are more shares available than were returned. The
207
+ # marker value may then be used here to request the next set
208
+ # of list items. (optional)
209
+ #
210
+ # * +:max_results+ - Integer. Specifies the maximum number of shares to return.
211
+ # If max_results is not specified, or is a value greater than
212
+ # 5,000, the server will return up to 5,000 items. If it is set
213
+ # to a value less than or equal to zero, the server will return
214
+ # status code 400 (Bad Request). (optional)
215
+ #
216
+ # * +:metadata+ - Boolean. Specifies whether or not to return the share metadata.
217
+ # (optional, Default=false)
218
+ #
219
+ # * +:timeout+ - Integer. A timeout in seconds.
220
+ #
221
+ # * +:request_id+ - String. Provides a client-generated, opaque value with a 1 KB character limit that is recorded
222
+ # in the analytics logs when storage analytics logging is enabled.
223
+ #
224
+ # * +:location_mode+ - LocationMode. Specifies the location mode used to decide
225
+ # which location the request should be sent to.
226
+ #
227
+ # See: https://docs.microsoft.com/en-us/rest/api/storageservices/fileservices/list-shares
228
+ #
229
+ # Returns an Azure::Storage::Common::EnumerationResults
230
+ #
231
+ def list_shares(options = {})
232
+ query = {}
233
+ if options
234
+ StorageService.with_query query, "prefix", options[:prefix]
235
+ StorageService.with_query query, "marker", options[:marker]
236
+ StorageService.with_query query, "maxresults", options[:max_results].to_s if options[:max_results]
237
+ StorageService.with_query query, "include", "metadata" if options[:metadata] == true
238
+ StorageService.with_query query, "timeout", options[:timeout].to_s if options[:timeout]
239
+ end
240
+
241
+ options[:request_location_mode] = Azure::Storage::Common::RequestLocationMode::PRIMARY_OR_SECONDARY
242
+ uri = shares_uri(query, options)
243
+ response = call(:get, uri, nil, {}, options)
244
+
245
+ Serialization.share_enumeration_results_from_xml(response.body)
246
+ end
247
+
248
+ # Protected: Generate the URI for the collection of shares.
249
+ #
250
+ # ==== Attributes
251
+ #
252
+ # * +query+ - A Hash of key => value query parameters.
253
+ #
254
+ # Returns a URI.
255
+ #
256
+ protected
257
+ def shares_uri(query = {}, options = {})
258
+ query = { "comp" => "list" }.merge(query)
259
+ generate_uri("", query, options)
260
+ end
261
+
262
+ # Protected: Generate the URI for a specific share.
263
+ #
264
+ # ==== Attributes
265
+ #
266
+ # * +name+ - The share name. If this is a URI, we just return this.
267
+ # * +query+ - A Hash of key => value query parameters.
268
+ #
269
+ # Returns a URI.
270
+ #
271
+ protected
272
+ def share_uri(name, query = {}, options = {})
273
+ return name if name.kind_of? ::URI
274
+ query = { restype: "share" }.merge(query)
275
+ generate_uri(name, query, options)
276
+ end
277
+
278
+ # Protected: Generate the URI for a specific directory.
279
+ #
280
+ # ==== Attributes
281
+ #
282
+ # * +share+ - String representing the name of the share.
283
+ # * +directory_path+ - String representing the path to the directory.
284
+ # * +directory+ - String representing the name to the directory.
285
+ # * +query+ - A Hash of key => value query parameters.
286
+ #
287
+ # Returns a URI.
288
+ #
289
+ protected
290
+ def directory_uri(share, directory_path, query = {}, options = {})
291
+ path = directory_path.nil? ? share : ::File.join(share, directory_path)
292
+ query = { restype: "directory" }.merge(query)
293
+ options = { encode: true }.merge(options)
294
+ generate_uri(path, query, options)
295
+ end
296
+
297
+ # Protected: Generate the URI for a specific file.
298
+ #
299
+ # ==== Attributes
300
+ #
301
+ # * +share+ - String representing the name of the share.
302
+ # * +directory_path+ - String representing the path to the directory.
303
+ # * +file+ - String representing the name to the file.
304
+ # * +query+ - A Hash of key => value query parameters.
305
+ #
306
+ # Returns a URI.
307
+ #
308
+ protected
309
+ def file_uri(share, directory_path, file, query = {}, options = {})
310
+ if directory_path.nil?
311
+ path = ::File.join(share, file)
312
+ else
313
+ path = ::File.join(share, directory_path, file)
314
+ end
315
+ options = { encode: true }.merge(options)
316
+ generate_uri(path, query, options)
317
+ end
318
+
319
+ # Get the content type according to the content type header and request body.
320
+ #
321
+ # headers - The request body
322
+ # content_type - The request content type
323
+ protected
324
+ def get_or_apply_content_type(body, content_type = nil)
325
+ unless body.nil?
326
+ if (body.is_a? String) && body.encoding.to_s != "ASCII_8BIT" && !body.empty?
327
+ if content_type.nil?
328
+ content_type = "text/plain; charset=#{body.encoding}"
329
+ else
330
+ # Force the request.body to the content encoding of specified in the header
331
+ charset = parse_charset_from_content_type(content_type)
332
+ body.force_encoding(charset) if charset
333
+ end
334
+ else
335
+ # It is either that the body is not a string, or that the body's encoding is ASCII_8BIT, which is a binary
336
+ # In this case, set the content type to be default content-type
337
+ content_type = Default::CONTENT_TYPE_VALUE unless content_type
338
+ end
339
+ end
340
+ content_type
341
+ end
342
+ end
343
+ end
344
+ end
345
+
346
+ Azure::Storage::FileService = Azure::Storage::File::FileService