中野智文のブログ

データ・マエショリストのメモ

python ライブラリの gcloud(google.cloud) の storage の blob_name とは

背景

python ライブラリの gcloud(google.cloud) の storage の get_blob において、

https://googlecloudplatform.github.io/google-cloud-python/0.20.0/storage-buckets.html#google.cloud.storage.bucket.Bucket.get_blob

とあるが、試してみると、google cloud storage 上の

gs://my-bucket に、/path/to/blob.txt

は存在するのに、なぜか、 None が返ってくる。ほえ?

blob_name には 始めのスラッシュはいりません

すなわち、blob_name

path/to/blob.txt

ということです。だったらそのように例を(次のように)

>>> from google.cloud import storage
>>> client = storage.Client()
>>> bucket = client.get_bucket('my-bucket')
>>> print(bucket.get_blob('path/to/blob.txt'))
<Blob: my-bucket, path/to/blob.txt>
>>> print(bucket.get_blob('does-not-exist.txt'))
None

と書いてよ、と思う。