examples/file_upload_content
#!/usr/bin/env ruby
# frozen_string_literal: true
# Replace a file's content
require 'drive_v3'
drive_service = DriveV3.drive_service
file_id = ARGV[0] || ENV.fetch('FILE_ID', nil)
raise 'Missing file_id' unless file_id
# upload_source can be an IO object that responds to `read` and `size` (e.g.
# StringIO) or a string that names an existing file to upload.
#
upload_source = StringIO.new('This is the content that replaced the original content')
# Indicate that this application supports files in shared drives. An error will result if
# this is false (or omitted) AND you are trying to update a file in a shared drive.
#
supports_all_drives = true
# Replace the content of an existing file
#
begin
file = drive_service.update_file(file_id, upload_source:, supports_all_drives:)
puts JSON.pretty_generate(file.to_h)
rescue StandardError => e
puts "An error occurred: #{e.message}"
end