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)