diff --git a/lib/google/apis/core/upload.rb b/lib/google/apis/core/upload.rb index 723dbc94f..f02e8ddb0 100644 --- a/lib/google/apis/core/upload.rb +++ b/lib/google/apis/core/upload.rb @@ -82,7 +82,7 @@ module Google # @raise [Google::Apis::ClientError] if upload source is invalid def prepare! super - if upload_source.is_a?(IO) || upload_source.is_a?(StringIO) + if streamable?(upload_source) self.upload_io = UploadIO.from_io(upload_source, content_type: upload_content_type) @close_io_on_finish = false elsif upload_source.is_a?(String) @@ -97,6 +97,12 @@ module Google def release! upload_io.close if @close_io_on_finish end + + private + + def streamable?(upload_source) + upload_source.is_a?(IO) || upload_source.is_a?(StringIO) || upload_source.is_a?(Tempfile) + end end # Implementation of the raw upload protocol diff --git a/spec/google/apis/core/upload_spec.rb b/spec/google/apis/core/upload_spec.rb index e18c60119..99039f8f0 100644 --- a/spec/google/apis/core/upload_spec.rb +++ b/spec/google/apis/core/upload_spec.rb @@ -129,6 +129,20 @@ RSpec.describe Google::Apis::Core::RawUploadCommand do end end + context('with Tempfile input') do + let(:file) do + temp_file = Tempfile.new("tempfile") + temp_file.write("Hello world\n") + temp_file.rewind + temp_file + end + include_examples 'should upload' + + it 'should not close stream' do + expect(file.closed?).to be false + end + end + context('with file path input') do let(:file) { File.join(FIXTURES_DIR, 'files', 'test.txt') } include_examples 'should upload'