From e83c58efa1c7031ad82997607d739b672b9b3be1 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 6 Apr 2021 12:40:14 +0800 Subject: [PATCH] Unlink file temp file immediately 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. --- lib/fog/storage/google_json/requests/get_object.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/fog/storage/google_json/requests/get_object.rb b/lib/fog/storage/google_json/requests/get_object.rb index 39e9d025e..21e21b137 100644 --- a/lib/fog/storage/google_json/requests/get_object.rb +++ b/lib/fog/storage/google_json/requests/get_object.rb @@ -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 @@ -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