Spaces:
Building
Building
Update google_drive_upload.py
Browse files- google_drive_upload.py +4 -4
google_drive_upload.py
CHANGED
@@ -9,22 +9,22 @@ SCOPES = ['https://www.googleapis.com/auth/drive.file']
|
|
9 |
|
10 |
def authenticate_google_drive():
|
11 |
"""Autentica usando una cuenta de servicio."""
|
12 |
-
# Cargar las credenciales desde el secreto
|
13 |
service_account_info = json.loads(os.getenv('GOOGLE_SERVICE_ACCOUNT', '{}'))
|
14 |
if not service_account_info:
|
15 |
raise ValueError("No se encontr贸 la informaci贸n de la cuenta de servicio.")
|
16 |
-
|
17 |
-
# Crear credenciales
|
18 |
creds = Credentials.from_service_account_info(service_account_info, scopes=SCOPES)
|
19 |
return creds
|
20 |
|
21 |
-
def upload_to_google_drive(file_path):
|
22 |
"""Sube un archivo a Google Drive."""
|
23 |
try:
|
24 |
creds = authenticate_google_drive()
|
25 |
service = build('drive', 'v3', credentials=creds)
|
26 |
|
27 |
file_metadata = {'name': os.path.basename(file_path)}
|
|
|
|
|
|
|
28 |
media = MediaFileUpload(file_path, resumable=True)
|
29 |
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
30 |
print(f"Archivo subido con ID: {file.get('id')}")
|
|
|
9 |
|
10 |
def authenticate_google_drive():
|
11 |
"""Autentica usando una cuenta de servicio."""
|
|
|
12 |
service_account_info = json.loads(os.getenv('GOOGLE_SERVICE_ACCOUNT', '{}'))
|
13 |
if not service_account_info:
|
14 |
raise ValueError("No se encontr贸 la informaci贸n de la cuenta de servicio.")
|
|
|
|
|
15 |
creds = Credentials.from_service_account_info(service_account_info, scopes=SCOPES)
|
16 |
return creds
|
17 |
|
18 |
+
def upload_to_google_drive(file_path, folder_id=None):
|
19 |
"""Sube un archivo a Google Drive."""
|
20 |
try:
|
21 |
creds = authenticate_google_drive()
|
22 |
service = build('drive', 'v3', credentials=creds)
|
23 |
|
24 |
file_metadata = {'name': os.path.basename(file_path)}
|
25 |
+
if folder_id:
|
26 |
+
file_metadata['parents'] = [folder_id] # Especifica la carpeta de destino
|
27 |
+
|
28 |
media = MediaFileUpload(file_path, resumable=True)
|
29 |
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
|
30 |
print(f"Archivo subido con ID: {file.get('id')}")
|