Skip to content

Commit

Permalink
Unlink file temp file immediately
Browse files Browse the repository at this point in the history
In case the download is terminated, it's best to have unlinked the file
to avoid large files lingering. Since we're passing in an IO buffer
instead of a path, We need to seek to the beginning of the file in order
to read from it.
  • Loading branch information
stanhu committed May 12, 2021
1 parent 7f0727b commit e83c58e
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 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.unlink

# Two requests are necessary, first for metadata, then for content.
# google-api-ruby-client doesn't allow fetching both metadata and content
Expand All @@ -57,14 +58,15 @@ def get_object(bucket_name, object_name,
@storage_json.get_object(
bucket_name,
object_name,
**all_opts.merge(:download_dest => buf.path)
**all_opts.merge(:download_dest => buf)
)

buf.seek(0)

if block_given?
yield buf.read, nil, nil
else
object[:body] = buf.read
buf.unlink
end

object
Expand Down

0 comments on commit e83c58e

Please sign in to comment.