Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get_object not working with binary files #534

Merged
merged 1 commit into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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