LoginSignup
10
3

More than 3 years have passed since last update.

はじめに

この記事はLinkbal_Advent_Calendar_2020の20日目の記事です。
今日はRubyでGCS(Google Cloud Storage)でのデータのいくつかの操作を簡単に紹介していただきます。

実装

Gem設定

まずはgoogle-cloud-storagegemをinstallしなければならないです。最低限必要なルビーバージョンは2.4であることを忘れないでくださいね。
方法1:
Gemfileのファイルに以下の行を追加したら、bundle installを実行する。

gem 'google-cloud-storage', '~> 1.29', '>= 1.29.2'

方法2:
直接コマンドで実行することができます。

$ gem install google-cloud-storage

認証

使用する前に、認証手順が必要です。認証情報はproject_idcredentialsを含みます。

require "google/cloud/storage"

storage = Google::Cloud::Storage.new(
  project_id: "my-project",
  credentials: "/path/to/keyfile.json"
)
  • project_idはプロジェクトのIDです。
  • credentialsは認証情報を格納するJSONファイルへのパスです。
    普通はAPI Managerを選んで、Credentialsをクリックします。その後、Add credentialsの選択ボックスを見つけて、Service accountを選びます。作成したら、新しいJsonキーファイルをダウンロードします。どこかに保存して、パスを認証情報の部分に記入します。
    以外に、GCP(Google Cloud Platform)でcredentialsのパスを取り方: IAM&Admin => Service Accountsで新しいService Accountを作り、新しいKeysを作成してから、Jsonの形でダウンロードします。そのKeysはバケットにアクセスするのに使います。ダウンロードしたら、どこかで置いて、そのファイルへのパスを記入します。

認証に関するのを詳しく知りたい方は以下のリンクに参考できます。
https://googleapis.dev/ruby/google-cloud-storage/latest/file.AUTHENTICATION.html

データ操作

バケット

バケットは、データのコンテナです。プロジェクトで作成できるバケットの数に制限はありません。 バケットを使用して、データへのアクセスを整理および制御できます。

指定バケットに取得する時には、storage.bucket "bucket_name"のステートメントを使います。
例:

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my_bucket"

プロジェクト中には全てバケットを取りたい祭に

buckets = storage.buckets

API呼び出しの数を制限したい時に

buckets = storage.buckets
buckets.all(request_limit: 10) do |bucket|
  # do something
end

以外に、新しいバケットを作るために、storage.create_bucket "bucket_name"を使います。
例:

bucket = storage.create_bucket "my_new_bucket"
ファイル

ファイルは、Google CloudStorageに保存する個々のデータオブジェクトです。
バケットでの指定ファイルに取得する時にはファイル名が要ります。

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my_bucket"
file = bucket.file "avatars/image1.png"

バケットでの全てファイルを取りたい祭に

all_files = bucket.files

または、指定したパスにあるすべてのファイルを取得できます。

all_files = bucket.files prefix: "avatars/"

他に、バケットに新しいファイルを作成する際にローカルで保存しているファイルへのパスを指定しなければなりません。

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my_bucket"
bucket.create_file "/tmp/images/avatars/image1.png",
                   "avatars/image1.png"
ダウンロードファイル

ファイルをダウンロードしたい時にfile.downloadを使用します。

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my_bucket"
file = bucket.file "avatars/image1.png"
file.download "/tmp/images/avatars/image1.png" # path to save file at local

また、StringIOオブジェクトにダウンロードすることもできます。

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my_bucket"
file = bucket.file "hello_world.txt"

downloaded = file.download
downloaded.rewind
downloaded.read # => "Hello World!"

クライアントで認証されていないで公開ファイルをダウンロードしたい際に、skip_lookup: trueを使ってください。

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "public_bucket", skip_lookup: true
file = bucket.file "path/to/public_file.ext", skip_lookup: true

downloaded = file.download
アクセス制御
require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my_bucket"
file = bucket.file "avatars/image1.png"

email = "test@example.co.jp"

バケットやファイルへのアクセスはユーザーに許可や限定できます。

bucket.acl.add_reader "user-#{email}"
file.acl.add_reader "user-#{email}"

同じに、バケットやファイルへのアクセスはユーザーに許可や限定できます。

bucket.acl.add_reader "group-#{email}"
file.acl.add_reader "group-#{email}"

また、バケットやファイルへのアクセスは事前定義された権限のリストに付与することもできます。

bucket.acl.public!
file.acl.public!

終わりに

以上です。上記には何か間違った点があればコメントでご意見頂けると幸いです。記事を閲覧いただき誠にありがとうございます。:smile:
また次の記事に。

参考

10
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
3