from datetime import datetime, timedelta
from concurrent.futures import ThreadPoolExecutor
import os,time,subprocess,tracebackdef run_cmd(command):"""运行命令并返回输出。"""shell = Trueprint('command',command)result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=shell)if result.returncode != 0:print('err command',command)raise Exception("Command failed: ", result.stderr)return str(result)def run_command(command):"""运行命令并返回输出。"""result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)# print('command',command)# print(result)# if result.returncode != 0:# print('err command',command)# raise Exception("Command failed: ", result.stderr)return result.stdoutdef check_folder_exists_in_gdrive(folder_name, parent_id):"""检查给定名称的文件夹在Google Drive中是否存在,如果存在则返回其ID."""# 列出所有匹配名称的文件夹query = f"'{parent_id}' in parents and mimeType='application/vnd.google-apps.folder' and name='{folder_name}' and trashed=false"command = ['/usr/local/bin/gdrive', 'files', 'list', '--query', query, '--skip-header', '--max', '1']try:output = run_command(command)except:return None# 解析命令输出lines = output.strip().split('\n')if lines and lines[0]:# 假设输出格式是: id namefolder_id = lines[0].split(' ')[0]return folder_idreturn Nonedef create_folder_in_gdrive(folder_name, parent_id):"""在Google Drive中创建一个文件夹,返回新创建的文件夹的ID."""command = ['/usr/local/bin/gdrive', 'files', 'mkdir', '--parent', parent_id, folder_name]output = run_command(command)# 解析命令输出lines = output.strip().split('\n')# print("lines",lines)if lines and f"Created directory '{folder_name}' with id" in lines[0]:# 假设输出格式是: Directory <id> created in parent <parent_id>folder_id = lines[0].split(': ')[1]return folder_idelse:raise Exception("Failed to create directory")# 这个函数会在Google Drive中创建相应的文件夹结构,并返回最后一个文件夹的ID
def create_remote_folder_structure(remote_path):# 分割路径为单独的部分folders = remote_path.split(os.sep)parent_id = 'root' # 假设从Drive的根目录开始创建# folders.insert(0,'l2_data')# print('folders',folders)for folder in folders:# 检查该文件夹是否已经在Google Drive中存在existing_folder_id = check_folder_exists_in_gdrive(folder, parent_id)if existing_folder_id:# 如果文件夹已经存在,则下一次循环使用该ID作为父IDparent_id = existing_folder_idelse:# 如果文件夹不存在,则创建它,并使用新ID作为父IDparent_id = create_folder_in_gdrive(folder, parent_id)# 返回最终文件夹的IDreturn parent_iddef upload(local_path,remote_path):if(os.path.isdir(local_path)):for root, dirs, files in os.walk(local_path):for file in files:local_file_path = os.path.join(root, file)relative_path = os.path.relpath(local_file_path, local_path)gdrive_folder_path = os.path.dirname(relative_path)remote_dir = os.path.join(remote_path,local_path.rstrip('/').split('/')[-1],gdrive_folder_path).rstrip('/')print('开始上传:',local_file_path,remote_dir,int(time.time()))folder_id = create_remote_folder_structure(remote_dir)cmd = ['/usr/local/bin/gdrive', 'files', 'upload', '--parent', folder_id, local_file_path]ustatus = run_command(cmd)if('successfully uploaded' in ustatus):print('上传成功',int(time.time()))else:print('上传失败',int(time.time()))elif(os.path.isfile(local_path)):passupload(lpath,rpath)
需要安装gdrive客户端
centos7服务器上的文件上传到谷歌云盘(google drive)_centos上传谷歌-CSDN博客https://blog.csdn.net/qq_37500838/article/details/134980993?spm=1001.2014.3001.5502