main-branch/drive_v3

View on GitHub
examples/file_download_content

Summary

Maintainability
Test Coverage
#!/usr/bin/env ruby
# frozen_string_literal: true

# Read the content of a data file

require 'drive_v3'
require 'json'
require 'stringio'

drive_service = DriveV3.drive_service

file_id = ARGV[0] || ENV.fetch('FILE_ID', nil)
raise 'Missing file_id' unless file_id

# download_dest can be an IO object that responds to `write` that will receive
# the content of the file, or a string naming an existing file to be written
# to.
download_dest = StringIO.new

# Indicate that this application supports files in shared drives. An error will result if
# this is false (or omitted) AND `get_file` tries to get a file from s shared drive.
#
supports_all_drives = true

# Read content from file
#
begin
  drive_service.get_file(file_id, download_dest:, supports_all_drives:)
  puts JSON.pretty_generate(download_dest.string)
rescue StandardError => e
  puts "An error occurred: #{e.message}"
end