Skip to content

Commit

Permalink
Merge pull request #534 from stanhu/sh-fix-get-object-binmode
Browse files Browse the repository at this point in the history
Fix get_object not working with binary files
  • Loading branch information
geemus authored Jun 24, 2021
2 parents 388dea0 + f27a861 commit d0741c3
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/fog/storage/google_json/requests/get_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def get_object(bucket_name, object_name,
raise ArgumentError.new("object_name is required") unless object_name

buf = Tempfile.new("fog-google-storage-temp")
buf.binmode
buf.unlink

# Two requests are necessary, first for metadata, then for content.
Expand Down
9 changes: 7 additions & 2 deletions test/integration/storage/storage_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,14 @@ def temp_file_content
"hello world"
end

def some_temp_file
def binary_file_content
"PK\x03\x04\x14\x00\x00\x00\b\x00\x18\x89\x8AM\xE7!\xB7\x1C\x1C\x15j\x00\xB4\xB9".force_encoding(Encoding::ASCII_8BIT)
end

def some_temp_file(content = temp_file_content)
@some_temp_file ||= Tempfile.new("fog-google-storage").tap do |t|
t.write(temp_file_content)
t.binmode
t.write(content)
t.close
end
File.open(@some_temp_file.path, "r")
Expand Down
4 changes: 3 additions & 1 deletion test/integration/storage/test_objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ def test_put_object_file

def test_put_object_paperclip
object_name = new_object_name
paperclip_file = OpenStruct.new(:path => some_temp_file,
paperclip_file = OpenStruct.new(:path => some_temp_file(binary_file_content),
:content_type => "image/png")
@client.put_object(some_bucket_name, object_name, paperclip_file, :content_type => "image/png")

object = @client.get_object(some_bucket_name, object_name)

assert_equal(object_name, object[:name])
assert_equal(Encoding::ASCII_8BIT, object[:body].encoding)
assert_equal(binary_file_content, object[:body])
assert_equal("image/png", object[:content_type])
end

Expand Down

0 comments on commit d0741c3

Please sign in to comment.