File size: 1,073 Bytes
9cb7f7f da4daf3 c32bc1c da4daf3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import os
from google.cloud import storage
# Check if you're using Hugging Face Secrets or a local file
service_account_secret = os.getenv("SERVICE_ACCOUNT_JSON")
if service_account_secret:
# Save the service account from the secret to a file
with open("service-account.json", "w") as f:
f.write(service_account_secret)
# Set the environment variable to point to the service account file
#os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-account.json"
# Function to list Google Cloud Storage buckets
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)
# Flask app to display bucket names
app = Flask(__name__)
@app.route("/")
def home():
buckets = list_gcs_buckets()
if isinstance(buckets, str): # If an error occurred
return jsonify({"error": buckets}), 500
return jsonify({"buckets": buckets})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
|