[英]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?
有什么想法怎么做?
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-schRXnDFZeX0t0RnhQVXXXXXX
: https://drive.google.com/open?id=0B-schRXnDFZeX0t0RnhQVXXXXXX
id=
https://drive.google.com/open?id=0B-schRXnDFZeX0t0RnhQVXXXXXX
( id=
后面的URL部分)。
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 instructions , Delete / 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.