Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,35 @@
|
|
| 1 |
-
import requests
|
| 2 |
import os
|
| 3 |
-
from
|
| 4 |
-
from google.
|
| 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 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# Example usage of the authenticated service
|
| 39 |
-
drive_service = authenticate_google_drive()
|
| 40 |
-
|
| 41 |
-
# Example API call: List files in Google Drive
|
| 42 |
-
def list_drive_files():
|
| 43 |
-
"""List files in Google Drive."""
|
| 44 |
-
results = drive_service.files().list(pageSize=10).execute()
|
| 45 |
-
items = results.get('files', [])
|
| 46 |
-
if not items:
|
| 47 |
-
print("No files found.")
|
| 48 |
-
else:
|
| 49 |
-
print("Files:")
|
| 50 |
-
for item in items:
|
| 51 |
-
print(f"{item['name']} ({item['id']})")
|
| 52 |
-
|
| 53 |
-
# Call the function to list files
|
| 54 |
-
list_drive_files()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from flask import Flask, jsonify
|
| 3 |
+
from google.cloud import storage
|
| 4 |
+
|
| 5 |
+
# Check if you're using Hugging Face Secrets or a local file
|
| 6 |
+
service_account_secret = os.getenv("SERVICE_ACCOUNT_JSON")
|
| 7 |
+
if service_account_secret:
|
| 8 |
+
# Save the service account from the secret to a file
|
| 9 |
+
with open("service-account.json", "w") as f:
|
| 10 |
+
f.write(service_account_secret)
|
| 11 |
+
|
| 12 |
+
# Set the environment variable to point to the service account file
|
| 13 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "service-account.json"
|
| 14 |
+
|
| 15 |
+
# Function to list Google Cloud Storage buckets
|
| 16 |
+
def list_gcs_buckets():
|
| 17 |
+
try:
|
| 18 |
+
client = storage.Client()
|
| 19 |
+
buckets = list(client.list_buckets())
|
| 20 |
+
return [bucket.name for bucket in buckets]
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return str(e)
|
| 23 |
+
|
| 24 |
+
# Flask app to display bucket names
|
| 25 |
+
app = Flask(__name__)
|
| 26 |
+
|
| 27 |
+
@app.route("/")
|
| 28 |
+
def home():
|
| 29 |
+
buckets = list_gcs_buckets()
|
| 30 |
+
if isinstance(buckets, str): # If an error occurred
|
| 31 |
+
return jsonify({"error": buckets}), 500
|
| 32 |
+
return jsonify({"buckets": buckets})
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
app.run(host="0.0.0.0", port=8080)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|