|
|
import os |
|
|
from google.cloud import storage |
|
|
|
|
|
|
|
|
service_account_secret = os.getenv("SERVICE_ACCOUNT_JSON") |
|
|
if service_account_secret: |
|
|
|
|
|
with open("service-account.json", "w") as f: |
|
|
f.write(service_account_secret) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def list_gcs_buckets(): |
|
|
try: |
|
|
client = storage.Client() |
|
|
buckets = list(client.list_buckets()) |
|
|
return [bucket.name for bucket in buckets] |
|
|
except Exception as e: |
|
|
return str(e) |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
@app.route("/") |
|
|
def home(): |
|
|
buckets = list_gcs_buckets() |
|
|
if isinstance(buckets, str): |
|
|
return jsonify({"error": buckets}), 500 |
|
|
return jsonify({"buckets": buckets}) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
app.run(host="0.0.0.0", port=8080) |
|
|
|