1
0

More than 1 year has passed since last update.

Python3: Google Cloud Storage にアクセス

Posted at

GCE の Ubuntu 21.10 で確認した方法です。GCE で実行するので、認証の部分のコードを省略できます。

スクリプトでアクセスする方法はこちら
Google Cloud Storage にスクリプトでアクセス

バケットの一覧

list_bucket.py
#! /usr/bin/python
#
#   list_bucket.py
#
from google.cloud import storage
#
client = storage.Client()
#
for bucket in client.list_buckets():
    print(vars(bucket)['name'])
    print()
#

ファイルの一覧

list_files.py
#! /usr/bin/python
#
#   list_files.py
#
from google.cloud import storage
#
bucket_name = 'bucket001'
client = storage.Client()
#
for file in client.list_blobs(bucket_name):
    print(file.name)
#

ファイルのダウンロード

get_file.py
#! /usr/bin/python
#
#   get_file.py
#
from google.cloud import storage
#
bucket_name = 'bucket01'
fname = 'file01.txt'
gcs_path='folder_aa/' + fname
local_path='./' + fname
client = storage.Client()
#
bucket=client.get_bucket(bucket_name)
blob=bucket.blob(gcs_path)
#print(blob.download_as_string())
#
blob.download_to_filename(local_path)
1
0
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
1
0