File size: 1,133 Bytes
411ca77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
import os
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from datetime import datetime

def save_logs(path_to_data_to_save, name_to_save, folder_id = "16Vv728HPW2J0BYzgTaBV00nUEc5pRKT-"):

    filename = path_to_data_to_save
    SERVICE_ACCOUNT_FILE = 'secret_google_service_account.json'

    SCOPES = ['https://www.googleapis.com/auth/drive.file']

    credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

    service = build('drive', 'v3', credentials=credentials)


    file_metadata = {
        'name': name_to_save,  # Name of the file to be uploaded
        'parents': [folder_id]  # Folder ID
    }

    file_path = filename

    # Create a MediaFileUpload object to upload the file
    media = MediaFileUpload(file_path)

    file = service.files().create(
        body=file_metadata,
        media_body=media,
        fields='id'
    ).execute()

    # Print the file ID of the uploaded file
    print('Saved in Google Drive - File ID: %s' % file.get('id'))
    return file