Created
October 27, 2021 14:00
-
-
Save goodbyegangster/6217f16b95fb08c9a924799619561fc4 to your computer and use it in GitHub Desktop.
Get a list of bucket information from GCS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Google Cloud Storage から Bucket 情報の一覧を取得するスクリプト | |
- Python 3.9.6 | |
- google-cloud-storage==1.42.3 | |
""" | |
import csv | |
from google.cloud import storage | |
def run(): | |
storage_client = storage.Client() | |
buckets = storage_client.list_buckets() | |
data_list = [] | |
header = ["name", "class", "location_type", "location", "versioning", "logging", "lifecycle_rule", "retention_period"] | |
data_list.append(header) | |
for bucket in buckets: | |
data = [] | |
data.append(bucket.name) | |
data.append(bucket.storage_class) | |
data.append(bucket.location_type) | |
data.append(bucket.location) | |
data.append(bucket.versioning_enabled) | |
if bucket.get_logging(): | |
data.append("True") | |
else: | |
data.append("False") | |
if list(bucket.lifecycle_rules): | |
data.append(str(list(bucket.lifecycle_rules))) | |
else: | |
data.append("None") | |
if bucket.retention_period: | |
data.append(bucket.retention_period) | |
else: | |
data.append("None") | |
data_list.append(data) | |
with open("./bucket_list.csv", "w") as f: | |
writer = csv.writer(f) | |
writer.writerows(data_list) | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment