PyDrive服务器远程文件管理| 如何用Python从Linux服务器自动上传文件/文件夹到Google Drive
创始人
2024-11-10 05:04:33
0

用服务器批量生成的文件是HTML,每次同步到本地查看尊嘟好麻烦,所以想了个办法直接一键上传谷歌云,然后到本地Drive客户端文件夹里直接打开就行了。

步骤如下,可供诸君参考~

Step 1 注册Google Cloud 获得秘钥

  • 进入Google Cloud Console.
  • 注册新工程“New Project”, 输入你的Project Name.
    在这里插入图片描述
  • 点击“ENABLE API AND SERVICE”, 进入新页面.
    在这里插入图片描述
  • 在新页面的搜索栏目里搜索“Google Drive”, 选择下图中这个,将会进入新页面。
    在这里插入图片描述
  • 在新的页面中点击ENABLE按钮(这步忘记截图了QAQ)。此时会弹出一个页面让你创建一个新API,有两种类型,只要记得选择“OAuth 2.0 Client IDs”就行。后面的表单按照你的需求填写。
  • 都填写完毕点击确定后会获得一个下图这样的界面,点击下图下载按钮获得一个.json文件,里面存储的就是你的API和相关秘钥信息。
    在这里插入图片描述
  • 把下载好的秘钥文件上传到你的server的某个地方,秘钥下载成就达成!

Step 2 下载PyDrive

  • 你需要在你的server的环境中安装一个Python包‘PyDrive’, 并通过它来交互你的Google Drive。安装命令如下:
pip install pydrive 
  • OK, Python相关包下载成就达成!

Step 3 撰写Python脚本

  • 在你的服务器上上传下列Python脚本‘submit2drive.py’,修改Main函数部分的三个相关参数,包括你的秘钥存储地址、本地文件地址、目标存储地址等。
  • 注意:
    • 我写了两个函数,第一个是上传单个文件,第二个是批量上传文件夹里的所有文件,按需选择嘻嘻~
    • 每个函数的思路是1-用秘钥通过谷歌用户认证获取进入你的Drive的权限;2-获取你的目标云存储的具体地址链接;3-读取本地文件地址,是单个文件就上传到目标链接中!是文件夹就遍历,然后挨个上传!
    • gauth.CommandLineAuth() 这一步特别重要,它设置认证方式为命令行而非图形界面(毕竟大部分Linux Server不支持图形界面QAQ)。
import os from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive   def get_folder_id(drive, parent_id, folder_name):     # List folders in the parent folder     file_list = drive.ListFile(         {             'q': f"'{parent_id}' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false"}).GetList()      for file in file_list:         if file['title'] == folder_name:             return file['id']      # Folder not found, create it     folder_metadata = {         'title': folder_name,         'mimeType': 'application/vnd.google-apps.folder',         'parents': [{'id': parent_id}]     }     folder = drive.CreateFile(folder_metadata)     folder.Upload()      return folder['id']   def upload_file_to_google_drive(local_path, drive_path, client_secrets_path):     # Authenticate and initialize PyDrive     gauth = GoogleAuth()     gauth.LoadClientConfigFile(client_secrets_path)     gauth.CommandLineAuth()  # Use command-line authentication     drive = GoogleDrive(gauth)      # Split the drive path into components     path_parts = drive_path.strip('/').split('/')      # Set up folder structure     parent_id = 'root'  # Start from the root folder     for part in path_parts[:-1]:  # Go through each part of the path except the last one         parent_id = get_folder_id(drive, parent_id, part)      # Upload file     file_title = path_parts[-1]  # The last part of the path is the file title     file_to_upload = drive.CreateFile({'title': file_title, 'parents': [{'id': parent_id}]})     file_to_upload.SetContentFile(local_path)     file_to_upload.Upload()      print(f'File uploaded successfully to {drive_path}')   def upload_folder_to_google_drive(local_folder, drive_path, client_secrets_path):     # Authenticate and initialize PyDrive     gauth = GoogleAuth()     gauth.LoadClientConfigFile(client_secrets_path)     gauth.CommandLineAuth()  # Use command-line authentication     drive = GoogleDrive(gauth)      # Split the drive path into components     path_parts = drive_path.strip('/').split('/')      # Set up folder structure     parent_id = 'root'  # Start from the root folder     for part in path_parts:  # Go through each part of the path         parent_id = get_folder_id(drive, parent_id, part)      # Iterate over all files in the local folder     for root, _, files in os.walk(local_folder):         for file_name in files:             file_path = os.path.join(root, file_name)             file_to_upload = drive.CreateFile({'title': file_name, 'parents': [{'id': parent_id}]})             file_to_upload.SetContentFile(file_path)             file_to_upload.Upload()             print(f'File {file_name} uploaded successfully to {drive_path}')   if __name__ == "__main__":     client_secrets_path = '.../client_secrets.json'  # Replace with the correct path     local_path = '.../result' # Local Path     drive_path = '.../test/'  # Desired path on Google Drive      upload_folder_to_google_drive(local_path, drive_path, client_secrets_path) 
  • 脚本成就达成!只差临门一脚!

Step 4 运行你的脚本!

  • 在你的Server上运行你的脚本,成功的话会出现如下回答,输出一个网页链接,并要求你输入一个秘钥。
    在这里插入图片描述
  • 你把这个网页链接复制到有显示器的机器(比如你的本地台式机)上打开,会是一个谷歌登录认证。一路点击Continue,最后可以获得如下图的界面:
    在这里插入图片描述
  • 点击复制按钮即可复制这个Code!把这段Code输入到你的服务器命令行。
    在这里插入图片描述
  • 芜湖搞定!去查看你的Drive对应位置,大概已经上传成功啦!

相关内容

热门资讯

透视研究!wpk微扑克辅助ai... 1、透视研究!wpk微扑克辅助ai,线上德州辅助工具有哪些(其实真的有挂)(UU poker、、xp...
线上教程!扑克世界app辅助(... 线上教程!扑克世界app辅助(wepoke有辅助挂)原来确实真的有挂(有挂技巧)1、每一步都需要思考...
9分钟透明挂!aapoker有... 9分钟透明挂!aapoker有手游版吗"微扑克辅助机器人(原来真的有挂)-哔哩哔哩1、首先打开微扑克...
透视科普!云扑克辅助透视,德扑... 透视科普!云扑克辅助透视,德扑之星真破解套路(其实真的有挂)1、玩家可以在软件透明挂俱乐部对游戏的模...
透视辅助!微扑克有挂么(德州a... 透视辅助!微扑克有挂么(德州ai)辅助透视(其实真的有挂)是一款可以让一直输的玩家,快速成为一个“必...
2024新版教程!德扑胜率计算... 2024新版教程!德扑胜率计算的软件(aapoker有挂)其实真的有挂(有挂插件)1、点击下载安装,...
4分钟总结!pokermast... 4分钟总结!pokermaster有挂吗"德州ai机器人(原来真的有挂)-哔哩哔哩1、实时poker...
力荐教程!德扑ai软件(德州a... 力荐教程!德扑ai软件(德州ai人工智能)原来真的是有挂(有挂插件)是一款可以让一直输的玩家,快速成...
透视内幕!wpk微扑克模拟器,... 透视内幕!wpk微扑克模拟器,wpk有修改器(原来真的有挂)小薇(透视辅助)致您一封信;亲爱玩家:《...
2分钟了解!智星德州菠萝在哪下... 1、2分钟了解!智星德州菠萝在哪下载"aapoker透明挂(其实真的有挂)-哔哩哔哩;详细教程。2、...