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(filename, folder_id = ""): # Get the current date and time now = datetime.now() # filename = str(now).replace(":","").replace(" ","").replace("-","").replace(".","")+".txt" # with open(filename, 'w') as file: # file.write(to_save) # Path to the service account key file SERVICE_ACCOUNT_FILE = 'secret_google_service_account.json' # Define the required scopes SCOPES = ['https://www.googleapis.com/auth/drive.file'] # Authenticate using the service account key file credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_FILE, scopes=SCOPES) # Build the Google Drive API client service = build('drive', 'v3', credentials=credentials) # Specify the folder ID where you want to upload the file # Metadata of the file to be uploaded file_metadata = { 'name': filename, # Name of the file to be uploaded 'parents': [folder_id] # Folder ID } # Path to the file you want to upload file_path = filename # Create a MediaFileUpload object to upload the file media = MediaFileUpload(file_path) # Use the Drive API to upload the file 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'))