Log目次

【Rails】GoogleDriveの利用

作成日 2019-05-12更新日 2019-05-12

はじめに

RailsでGoogleDriveの操作をしてみようと思います。

gem 'google_drive'

gimite/google-drive-ruby

ファイル一覧

Driveのファイルの一覧を取得するには、下記のようにします。

※ブロックで実行しない場合、デフォルトで100件の取得になるようです

require "google_drive" class Tasks::GoogleDrive Tasks::Batch.execute def self.execute session = GoogleDrive::Session.from_config("config.json") # Gets list of remote files. session.files.each do | file | p "#{file.id}:#{file.title}:#{file.resource_type}" end end end

Drive APIでは、フォルダもファイルとしてあつかわれるみたいです。

区別する際には、resource_typeを確認します。

下記は上記コード実行時の出力結果です。

1KVT65DfSEarxaXGqixyhM9ZBQBXJBBmg:Invoices_child:folder” 1o1_-_twxwQcH0c07EBEpPxUaSTYf8Icr:Invoices2:folder” 1TyZLa1QFUYEBNs3P-itZrd-7uNpFJZbC:Invoices=child:file1U4xL0jOpIW9jOceeaQvxIl_uIstXsv_t:Invoices2:folder” 1NmWwodjXvlbR7cbOe48ZoC0Wwt6V4Urt:Invoices2:folder” 103bLUDM3lwajXl6Pqd_pHXBOnPED9gCf:Invoices:folder” 1xSveXapU6BxUKZ_3bMPc6JnusBdOBWPA:Untitled:file” 1xxOkgxoQjkmuzbGs1sgDFWJtoza5OrMy:Untitled:file” 1051SWvOIeEj3dixiTfDyi1m8ysMNQt0N:Untitled:file” 16QCu5wcs_JarcC8F3ZFhH-2JyDuH9z3LQjJomRdX2Xg:家計簿:spreadsheet” 1XavWaato6jWKAQ6XaeuPwpvPEIe-iUI988xTb26rduw:まとめ:spreadsheet” 14-oJECpEdQvNJriaZnbZ8F2m84akkJludnD_3MM1L_w:無題のドキュメント:document”

ファイルの作成

ファイルの作成にはcreate_fileを使用します。

ここでは、documentを作成していますが、ほかにも作成できます。

指定できる、MIME Typesは下記をご確認ください。

MIME Types

require "google_drive" class Tasks::GoogleDrive def self.execute session = GoogleDrive::Session.from_config("config.json") session.create_file('Document_Title', mime_type: 'application/vnd.google-apps.document') end end

ディレクトリ作成

mime_typeをapplication/vnd.google-apps.folderにする必要があります。

# セッション作成 session = GoogleDrive::Session.from_config("config.json") # ディレクトリ作成 file = session.create_file('Invoices', mime_type: 'application/vnd.google-apps.folder')

ディレクトリの中にディレクトリを作成する場合、 metadataとして、parentsを指定する必要があります。

# セッション作成 session = GoogleDrive::Session.from_config("config.json") # ディレクトリ作成 file = session.create_file('Invoices', mime_type: 'application/vnd.google-apps.folder') puts "Folder Id: #{file.id}" # 入れ子のディレクトリ作成 file_metadata = { mime_type: 'application/vnd.google-apps.folder', parents: [file.id] } session.create_file('Invoices_child', file_metadata)

ファイルの取得

ファイルの取得には下記のメソッドが使用できます。

require "google_drive" class Tasks::GoogleDrive def self.execute session = GoogleDrive::Session.from_config("config.json") file = session.file_by_id("14-oJECpEdQvNJriaZnbZ8F2m84akkJludnD_3MM1L_w") p file.title end end

上記コード実行時の出力 => “無題のドキュメント”

ファイルのダウンロード

ローカルにダウンロードするには、下記のようにします。

hello.txtという名前のファイルを/varの直下にダウンロードしています。

require "google_drive" class Tasks::GoogleDrive def self.execute session = GoogleDrive::Session.from_config("config.json") file = session.file_by_title("hello.txt") file.download_to_file("/var/hello.txt") end end

lsでhello.txtが存在することを確認

root@4e98c4f118a4:/var# ls backups cache hello.txt lib local lock log mail opt run spool tmp

ファイルのアップロード

tempfileを作成し、Driveにアップロードしてみます。

require "google_drive" require "tempfile" class Tasks::GoogleDrive def self.execute csv = "aaa,bbb,ccc" title = "test_file" Tempfile.open([title, ".csv"]) do |file| file << csv session = GoogleDrive::Session.from_config("config.json") session.upload_from_file(file, title) end end end

参考