Skip to content

Commit 937f5f3

Browse files
committed
Add additional headers to all endpoints
1 parent 378c5ce commit 937f5f3

File tree

2 files changed

+61
-17
lines changed

2 files changed

+61
-17
lines changed

lib/azure_blob/client.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get_blob(key, options = {})
7272

7373
headers = {
7474
"x-ms-range": options[:start] && "bytes=#{options[:start]}-#{options[:end]}",
75-
}
75+
}.merge(additional_headers(options))
7676

7777
Http.new(uri, headers, signer:).get
7878
end
@@ -97,7 +97,7 @@ def copy_blob(key, source_key, options = {})
9797
headers = {
9898
"x-ms-copy-source": source_uri.to_s,
9999
"x-ms-requires-sync": "true",
100-
}
100+
}.merge(additional_headers(options))
101101

102102
Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put
103103
end
@@ -116,7 +116,7 @@ def delete_blob(key, options = {})
116116

117117
headers = {
118118
"x-ms-delete-snapshots": options[:delete_snapshots] || "include",
119-
}
119+
}.merge(additional_headers(options))
120120

121121
Http.new(uri, headers, signer:).delete
122122
end
@@ -157,7 +157,7 @@ def list_blobs(options = {})
157157
query[:marker] = marker
158158
query.reject! { |key, value| value.to_s.empty? }
159159
uri.query = URI.encode_www_form(**query)
160-
response = Http.new(uri, signer:).get
160+
response = Http.new(uri, additional_headers(options), signer:).get
161161
end
162162

163163
BlobList.new(fetcher)
@@ -172,7 +172,7 @@ def list_blobs(options = {})
172172
def get_blob_properties(key, options = {})
173173
uri = generate_uri("#{container}/#{key}")
174174

175-
response = Http.new(uri, signer:).head
175+
response = Http.new(uri, additional_headers(options), signer:).head
176176

177177
Blob.new(response)
178178
end
@@ -193,9 +193,9 @@ def blob_exist?(key, options = {})
193193
# Takes a key (path) of the blob.
194194
#
195195
# Returns a hash of the blob's tags.
196-
def get_blob_tags(key)
196+
def get_blob_tags(key, options = {})
197197
uri = generate_uri("#{container}/#{key}?comp=tags")
198-
response = Http.new(uri, signer:).get
198+
response = Http.new(uri, additional_headers(options), signer:).get
199199

200200
Tags.from_response(response).to_h
201201
end
@@ -208,7 +208,7 @@ def get_blob_tags(key)
208208
def get_container_properties(options = {})
209209
uri = generate_uri(container)
210210
uri.query = URI.encode_www_form(restype: "container")
211-
response = Http.new(uri, signer:, raise_on_error: false).head
211+
response = Http.new(uri, additional_headers(options), signer:, raise_on_error: false).head
212212

213213
Container.new(response)
214214
end
@@ -228,6 +228,7 @@ def create_container(options = {})
228228
headers = {}
229229
headers[:"x-ms-blob-public-access"] = "blob" if options[:public_access]
230230
headers[:"x-ms-blob-public-access"] = options[:public_access] if [ "container", "blob" ].include?(options[:public_access])
231+
headers.merge!(additional_headers(options))
231232

232233
uri.query = URI.encode_www_form(restype: "container")
233234
response = Http.new(uri, headers, signer:).put
@@ -239,7 +240,7 @@ def create_container(options = {})
239240
def delete_container(options = {})
240241
uri = generate_uri(container)
241242
uri.query = URI.encode_www_form(restype: "container")
242-
response = Http.new(uri, signer:).delete
243+
response = Http.new(uri, additional_headers(options), signer:).delete
243244
end
244245

245246
# Return a URI object to a resource in the container. Takes a path.
@@ -282,7 +283,7 @@ def create_append_blob(key, options = {})
282283
"Content-Type": options[:content_type],
283284
"Content-MD5": options[:content_md5],
284285
"x-ms-blob-content-disposition": options[:content_disposition],
285-
}
286+
}.merge(additional_headers(options))
286287

287288
Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put(nil)
288289
end
@@ -304,7 +305,7 @@ def append_blob_block(key, content, options = {})
304305
"Content-Length": content.size,
305306
"Content-Type": options[:content_type],
306307
"Content-MD5": options[:content_md5],
307-
}
308+
}.merge(additional_headers(options))
308309

309310
Http.new(uri, headers, signer:).put(content)
310311
end
@@ -328,7 +329,7 @@ def put_blob_block(key, index, content, options = {})
328329
"Content-Length": content.size,
329330
"Content-Type": options[:content_type],
330331
"Content-MD5": options[:content_md5],
331-
}
332+
}.merge(additional_headers(options))
332333

333334
Http.new(uri, headers, signer:).put(content)
334335

@@ -357,13 +358,17 @@ def commit_blob_blocks(key, block_ids, options = {})
357358
"Content-Type": options[:content_type],
358359
"x-ms-blob-content-md5": options[:content_md5],
359360
"x-ms-blob-content-disposition": options[:content_disposition],
360-
**(options[:headers] || {}).map { |k, v| [ :"x-ms-#{k}", v.to_s ] }.to_h
361-
}
361+
}.merge(additional_headers(options))
362362

363363
Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put(content)
364364
end
365365

366-
private
366+
private
367+
368+
def additional_headers(options)
369+
(options[:headers] || {}).transform_keys { |k| "x-ms-#{k}".to_sym }.
370+
transform_values(&:to_s)
371+
end
367372

368373
def generate_block_id(index)
369374
Base64.urlsafe_encode64(index.to_s.rjust(6, "0"))
@@ -390,8 +395,7 @@ def put_blob_single(key, content, options = {})
390395
"Content-Type": options[:content_type],
391396
"x-ms-blob-content-md5": options[:content_md5],
392397
"x-ms-blob-content-disposition": options[:content_disposition],
393-
**(options[:headers] || {}).map { |k, v| [ :"x-ms-#{k}", v.to_s ] }.to_h
394-
}
398+
}.merge(additional_headers(options))
395399

396400
Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put(content.read)
397401
end

test/client/test_client.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,44 @@ def test_copy_between_containers
432432
rescue AzureBlob::Http::FileNotFoundError
433433
end
434434
end
435+
436+
def test_get_blob_additional_headers
437+
http_mock = Minitest::Mock.new
438+
http_mock.expect :get, ""
439+
440+
stubbed_new = lambda do |uri, headers = {}, signer: nil, **kwargs|
441+
assert_equal "bar", headers[:"x-ms-foo"]
442+
http_mock
443+
end
444+
445+
AzureBlob::Http.stub :new, stubbed_new do
446+
custom_client = AzureBlob::Client.new(account_name: "foo", access_key: "bar", container: "cont")
447+
custom_client.get_blob(key, headers: { foo: "bar" })
448+
end
449+
450+
http_mock.verify
451+
dummy = Minitest::Mock.new
452+
dummy.expect :delete_blob, nil, [key]
453+
@client = dummy
454+
end
455+
456+
def test_create_append_blob_additional_headers
457+
http_mock = Minitest::Mock.new
458+
http_mock.expect :put, true, [nil]
459+
460+
stubbed_new = lambda do |uri, headers = {}, signer: nil, **kwargs|
461+
assert_equal "bar", headers[:"x-ms-foo"]
462+
http_mock
463+
end
464+
465+
AzureBlob::Http.stub :new, stubbed_new do
466+
custom_client = AzureBlob::Client.new(account_name: "foo", access_key: "bar", container: "cont")
467+
custom_client.create_append_blob(key, headers: { foo: "bar" })
468+
end
469+
470+
http_mock.verify
471+
dummy = Minitest::Mock.new
472+
dummy.expect :delete_blob, nil, [key]
473+
@client = dummy
474+
end
435475
end

0 commit comments

Comments
 (0)