简体   繁体   English

使用PyDrive管理来自公共Google云端硬盘URL的文件

[英]Manage files from public Google Drive URL using PyDrive

I`m using PyDrive QuickStart script to list my Google Drive files. 我正在使用PyDrive QuickStart脚本列出我的Google Drive文件。

Code: 码:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()

print(file_list)

I'm able to list my files normally, but I need to list and manage files from another public drive URL (which is not the my personal authenticated drive) from my already authenticated GoogleDrive account like if I was using requests lib. 我能够正常列出我的文件,但是我需要从我已经过身份验证的GoogleDrive帐户列出并管理来自另一个公共驱动器URL(不是我个人认证的驱动器)的文件,就像我使用请求lib一样。 Any ideas how to do it? 有什么想法怎么做?

  1. You need to get the folder ID. 您需要获取文件夹ID。 You can find the ID in the URL of the folder. 您可以在文件夹的URL中找到ID。 An example would be: https://drive.google.com/open?id=0B-schRXnDFZeX0t0RnhQVXXXXXX (the part of the URL after the id= ). 一个例子是: https://drive.google.com/open?id=0B-schRXnDFZeX0t0RnhQVXXXXXXhttps://drive.google.com/open?id=0B-schRXnDFZeX0t0RnhQVXXXXXX id= https://drive.google.com/open?id=0B-schRXnDFZeX0t0RnhQVXXXXXXid=后面的URL部分)。

  2. List contents of a folder based on ID. 列出基于ID的文件夹的内容。 Given your code you replace file_list = ... with: 鉴于您的代码,您将file_list = ...替换为:

     file_id = '<Your folder id here.>' file_list = drive.ListFile({'q': "'%s' in parents and trashed=false" % file_id}).GetList() 

    If this does not work, you may have to add the remote folder to your Google Drive using the "Add to Drive" button in the top right corner of the shared folder when opened in a browser. 如果这不起作用,您可能需要在浏览器中打开时使用共享文件夹右上角的“添加到云端硬盘”按钮将远程文件夹添加到Google云端硬盘。

    2.1 Creating a file in a folder can be done like so: 2.1在文件夹中创建文件可以这样完成:

     file_object = drive.CreateFile({ "parents": [{"kind": "drive#fileLink", "id": parent_id}], 'title': file_name, # (Only!) If the new 'file' object is going be a folder: 'mimeType': "application/vnd.google-apps.folder" }) file_object.Upload() 

    If this fails check whether you have write permissions to the folder. 如果失败,请检查您是否具有该文件夹的写入权限。

    2.2 Deleting/Trashing a file can be done with the updated version available from GitHub: pip install instructions , Delete/Trash/UnTrash documentation 2.2删除/删除文件可以使用GitHub提供的更新版本完成: pip install instructionsDelete / Trash / UnTrash documentation

Finally, there is a feature request to Upload to folders as described in 2.1, and listing files of a folder , as described in 2. - if you find the above not to work you can add this as an issue / feature request to the repository. 最后,有一个功能请求上传到文件夹,如2.1所述,并列出文件夹的文件 ,如2所述。 - 如果您发现上述不起作用,您可以将此作为问题/功能请求添加到存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 PyDrive 列出共享的 Google Team Drive 文件夹中的所有文件 - List all files in folder in shared Google Team Drive using PyDrive pydrive:尝试将文件从远程服务器上传到 Google Drive - pydrive: trying to upload files to Google Drive from a remote server 使用 PyDrive 按扩展名删除 google drive 文件 - Delete google drive files by extension with PyDrive 使用 pydrive 将图像字符串上传到 Google Drive - Uploading image string to Google Drive using pydrive 使用PyDrive将大文件上传到Google云端硬盘 - Upload a huge file to Google Drive using PyDrive 使用 PyDrive 将图像上传到 Google Drive - Upload Image to Google Drive using PyDrive Pydrive 从 Google Drive 删除文件 - Pydrive deleting file from Google Drive 使用 pydrive 将文件上传到共享 Google Drive 中的共享文件夹时出现“不允许的方法” - 'Method Not Allowed' when using pydrive to upload files to a shared folder in a shared Google Drive 使用pydrive从谷歌共享驱动器下载文件,文件存在,但API返回404文件未找到错误 - Download file from google share drive using pydrive, file exist, but API return 404 File not found error 使用pydrive从谷歌共享驱动器中删除文件,文件存在,但API返回404 File not found错误 - Deleting file from google share drive using pydrive, file exist, but API return 404 File not found error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM