24
9

More than 5 years have passed since last update.

PythonでGoogle Cloud Storageを使うにはどのライブラリを使えばいいのか

Posted at

…公式ライブラリだけでもいっぱいありすぎて訳がわかりません。なので、どういう要件ならどれが使えるのかを表にまとめました。

とはいえ全ての組み合わせをテストしたわけではないので、間違いがあれば教えてください。

比較表

ライブラリ Python 2 Python 3 App Engine以外 App Engine AppEngine Dev Server 基本機能 ※3 高度な機能 ※3 推奨状況
google-cloud-storage × ※2
GoogleAppEngineCloudStorageClient × ※5 × ×
google-api-python-client + google-auth ※1 × ※2 ※4
google-api-python-client + oauth2client ※1 ※2 × ※4

各ライブラリの呼び方はいろいろありますが、ここではPyPIのパッケージ名で表記します。

※1: google-api-python-client

google-api-python-clientには認証用ライブラリとしてgoogle-authまたはoauth2clientを組み合わせる必要があります。

※2: App Engine Dev Server

App Engine Dev Serverは基本的にはただのローカルPython環境なので、バケット名とそれにアクセス可能なcredentialsをきちんと与えてやれば、どのライブラリも問題なく動作します。

ではGoogleAppEngineCloudStorageClient以外は何が△かというと、App Engineのデフォルトバケットを使っている場合です。App Engineではapp_identity.get_default_gcs_bucket_name()を使ってApp Engineプロジェクトのデフォルトバケット名を取得できるのですが、これが通常Dev Server環境では使えません。

GoogleAppEngineCloudStorageClientだけは、app_identity.get_default_gcs_bucket_name()を使ったコードがDev Serverでもそのまま動作します。これは、Cloud Stoargeをローカルストレージを使ってエミュレーションしてくれるからです。

※3: 基本機能 / 高度な機能

基本機能とは、オブジェクトの作成(アップロード)、更新、ダウンロード、削除、コピー、メタデータの取得、オブジェクト一覧の取得のことです。

高度な機能とは、バケット自体に対する操作(作成、削除、メタデータの取得/設定、一覧の取得)、オブジェクトのcompose(結合)、変更通知など、上の基本機能以外の機能です。

GoogleAppEngineCloudStorageClientは基本機能だけをサポートしています。

※4: 推奨状況

oauth2clientには

oauth2client is now deprecated. ... We recommend you use google-auth and oauthlib.

とあります。しかしgoogle-authは(明記されてないものの、試した限りでは)Python 2では動作しません。Python 2では諦めてoauth2clientを使いましょう。

google-api-python-clientには

While this library is still supported, we suggest trying the newer Cloud Client Library for Cloud Storage JSON, especially for new projects.

とあります。the newer Cloud Client Library for Cloud Storage JSONというのはgoogle-cloud-storageのことです。しかしgoogle-cloud-storageもPython 2では動作しません。Python 2では諦めてgoogle-api-python-clientを使いましょう。

※5: GoogleAppEngineCloudStorageClient on Python 3

対応する気はあるようです。

サンプルコード

サンプルコードは、他の記事や公式ドキュメントに譲ります。公式ドキュメントは、上の表のライブラリ名からリンクしてあります。

cloud-storage: 【GoogleCloudPlatform】pythonでGCSバケットのデータを読み出す/書き出す
GoogleAppEngineCloudStorageClient: Google App Engine + Python アプリから Cloud Storage を利用する

google-api-python-clientを使うサンプルコードは、公式ドキュメントを含めていいものが見当たらなかったので、簡単なサンプルコードを載せておきます。Python 2で、oauth2clientと組み合わせた例です。

from StringIO import StringIO

from apiclient.http import MediaIoBaseUpload
from googleapiclient.discovery import build
from oauth2client.client import GoogleCredentials


# Application default credentialsを使う。
credentials = GoogleCredentials.get_application_default()
service = build('storage', 'v1', credentials=credentials)
# 自分で作ったバケット名に書き換えてください。
bucket_name = 'my-bucket-name'

# オブジェクトのアップロード。
media = MediaIoBaseUpload(StringIO('Hello world'), mimetype='text/plain')
service.objects().insert(
    bucket=bucket_name,
    name='hello.txt',
    media_body=media).execute()

# オブジェクトのダウンロード。
print service.objects().get_media(
    bucket=bucket_name, object='hello.txt').execute()

自分の場合

環境がApp Engine Python 2で、上記「高度な機能」が必要なケースだったので、google-api-python-client + oauth2clientが要件を満たす唯一の選択肢でした。実際には、表の上から順番に試していって、一番下にたどり着いたのでした…。

24
9
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
24
9