Skip to content

Commit b6fa926

Browse files
authored
Merge pull request #40 from tr4b4nt/main
Use bytesize to get content size
2 parents e43b060 + 5c55f19 commit b6fa926

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

lib/azure_blob/client.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def initialize(account_name:, access_key: nil, principal_id: nil, container:, ho
4949
# [+:block_size+]
5050
# Block size in bytes, can be used to force the method to split the upload in smaller chunk. Defaults to +AzureBlob::DEFAULT_BLOCK_SIZE+ and cannot be bigger than +AzureBlob::MAX_UPLOAD_SIZE+
5151
def create_block_blob(key, content, options = {})
52-
if content.size > (options[:block_size] || DEFAULT_BLOCK_SIZE)
52+
if content_size(content) > (options[:block_size] || DEFAULT_BLOCK_SIZE)
5353
put_blob_multiple(key, content, **options)
5454
else
5555
put_blob_single(key, content, **options)
@@ -309,7 +309,7 @@ def append_blob_block(key, content, options = {})
309309
uri.query = URI.encode_www_form(comp: "appendblock")
310310

311311
headers = {
312-
"Content-Length": content.size,
312+
"Content-Length": content_size(content),
313313
"Content-Type": options[:content_type],
314314
"Content-MD5": options[:content_md5],
315315
}.merge(additional_headers(options))
@@ -333,7 +333,7 @@ def put_blob_block(key, index, content, options = {})
333333
uri.query = URI.encode_www_form(comp: "block", blockid: block_id)
334334

335335
headers = {
336-
"Content-Length": content.size,
336+
"Content-Length": content_size(content),
337337
"Content-Type": options[:content_type],
338338
"Content-MD5": options[:content_md5],
339339
}.merge(additional_headers(options))
@@ -361,7 +361,7 @@ def commit_blob_blocks(key, block_ids, options = {})
361361
uri.query = URI.encode_www_form(comp: "blocklist")
362362

363363
headers = {
364-
"Content-Length": content.size,
364+
"Content-Length": content_size(content),
365365
"Content-Type": options[:content_type],
366366
"x-ms-blob-content-md5": options[:content_md5],
367367
"x-ms-blob-content-disposition": options[:content_disposition],
@@ -384,7 +384,7 @@ def generate_block_id(index)
384384
def put_blob_multiple(key, content, options = {})
385385
content = StringIO.new(content) if content.is_a? String
386386
block_size = options[:block_size] || DEFAULT_BLOCK_SIZE
387-
block_count = (content.size.to_f / block_size).ceil
387+
block_count = (content_size(content).to_f / block_size).ceil
388388
block_ids = block_count.times.map do |i|
389389
put_blob_block(key, i, content.read(block_size))
390390
end
@@ -398,7 +398,7 @@ def put_blob_single(key, content, options = {})
398398

399399
headers = {
400400
"x-ms-blob-type": "BlockBlob",
401-
"Content-Length": content.size,
401+
"Content-Length": content_size(content),
402402
"Content-Type": options[:content_type],
403403
"x-ms-blob-content-md5": options[:content_md5],
404404
"x-ms-blob-content-disposition": options[:content_disposition],
@@ -407,6 +407,14 @@ def put_blob_single(key, content, options = {})
407407
Http.new(uri, headers, signer:, **options.slice(:metadata, :tags)).put(content.read)
408408
end
409409

410+
def content_size(content)
411+
if content.respond_to?(:bytesize)
412+
content.bytesize
413+
else
414+
content.size
415+
end
416+
end
417+
410418
def host
411419
@host ||= "https://#{account_name}.blob.#{CLOUD_REGIONS_SUFFIX[cloud_regions]}"
412420
end

test/client/test_client.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def test_get_blob_properties
247247
blob = client.get_blob_properties(key)
248248

249249
assert blob.present?
250-
assert_equal content.size, blob.size
250+
assert_equal content.bytesize, blob.size
251251
end
252252

253253
def test_get_blob_properties_404
@@ -475,4 +475,25 @@ def test_create_append_blob_additional_headers
475475
dummy.expect :delete_blob, nil, [ key ]
476476
@client = dummy
477477
end
478+
479+
def test_append_blob_block_content_size
480+
content = "Ň"
481+
http_mock = Minitest::Mock.new
482+
http_mock.expect :put, true, [ content ]
483+
484+
stubbed_new = lambda do |uri, headers = {}, signer: nil, **kwargs|
485+
assert_equal "2", headers[:"Content-Length"]
486+
http_mock
487+
end
488+
489+
AzureBlob::Http.stub :new, stubbed_new do
490+
custom_client = AzureBlob::Client.new(account_name: "foo", access_key: "bar", container: "cont")
491+
custom_client.append_blob_block(key, content)
492+
end
493+
494+
http_mock.verify
495+
dummy = Minitest::Mock.new
496+
dummy.expect :delete_blob, nil, [ key ]
497+
@client = dummy
498+
end
478499
end

0 commit comments

Comments
 (0)