srinija / app.py
Ajay98's picture
Update app.py
a5a758a verified
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)