Spaces:
Sleeping
Sleeping
Create google_drive.py
Browse files- src/google_drive.py +23 -0
src/google_drive.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from googleapiclient.discovery import build
|
| 2 |
+
from googleapiclient.http import MediaFileUpload
|
| 3 |
+
from google.oauth2 import service_account
|
| 4 |
+
import json
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
def upload_to_drive(local_path, filename):
|
| 8 |
+
creds_dict = json.loads(os.environ["GOOGLE_CREDS_JSON"])
|
| 9 |
+
creds = service_account.Credentials.from_service_account_info(creds_dict)
|
| 10 |
+
service = build("drive", "v3", credentials=creds)
|
| 11 |
+
|
| 12 |
+
file_metadata = {
|
| 13 |
+
"name": filename,
|
| 14 |
+
# "parents": ["your-folder-id"] # optional: target folder
|
| 15 |
+
}
|
| 16 |
+
media = MediaFileUpload(local_path, mimetype="application/zip")
|
| 17 |
+
uploaded_file = service.files().create(
|
| 18 |
+
body=file_metadata,
|
| 19 |
+
media_body=media,
|
| 20 |
+
fields="id"
|
| 21 |
+
).execute()
|
| 22 |
+
|
| 23 |
+
return uploaded_file.get("id")
|