centos7指定目录上传到google云盘

from datetime import datetime, timedelta
from concurrent.futures import ThreadPoolExecutor
import os,time,subprocess,traceback

def run_cmd(command):
    """运行命令并返回输出。"""
    shell = True
    print('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.stdout


def 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 name
        folder_id = lines[0].split(' ')[0]
        return folder_id
    return None

def 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  created in parent 
        folder_id = lines[0].split(': ')[1]
        return folder_id
    else:
        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作为父ID
            parent_id = existing_folder_id
        else:
            # 如果文件夹不存在,则创建它,并使用新ID作为父ID
            parent_id = create_folder_in_gdrive(folder, parent_id)
    
    # 返回最终文件夹的ID
    return parent_id

def 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)):
        pass


upload(lpath,rpath)

需要安装gdrive客户端

centos7服务器上的文件上传到谷歌云盘(google drive)_centos上传谷歌-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/qq_37500838/article/details/134980993?spm=1001.2014.3001.5502

你可能感兴趣的:(python,google,云盘)