Read/Write operations with google drive | Ram Laxman Yadav

Read/Write operations with google drive

However in practice, we need read/write operations with google drive.In ruby it’s pretty simple to perform read/write operations in google drive.Here is an example of the same using ruby script.


To perform read/write operation use gem google-drive-ruby.

Install gem

Run the following command

 sudo gem install google_drive

Create a developer client_id and client secret

To create client_id and client_secret you have to follow these steps:

  • Login to the Google’s Admin pannel.
  • Create a project and go to Api & auth tab.
  • Click on credentials tab and click on ‘Create new client ID’.
  • Select Application type Web and give the callback url.
  • Click ‘Create Client ID’.

Example to read/write files in Google Drive:

require "google/api_client"
require "google_drive"

#=>Get an google api client
client =
auth = client.authorization
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope = [

auth.redirect_uri = "YOUR_REDIRECT_URL"
print("1. Open this page:\n%s\n\n" % auth.authorization_uri)
#=>This will print an url on screen.Copy this url and open in browser then copy the value of variable code and paste on the stdin.
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
#=> Get the access code
access_token = auth.access_token

#=> Get session
session = GoogleDrive.login_with_oauth(access_token)

#=> Gets list of remote files.
session.files.each do |file|
  p file.title

#=> Uploads a local file.
session.upload_from_file("/path/to/hello.txt", "hello.txt", convert: false)

#=> Downloads to a local file.
file = session.file_by_title("hello.txt")

#=> Updates content of the remote file.

For more details and to see an example please refer this link

Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer