使用PyDrive管理来自公共Google Drive URL的文件 [英] Manage files from public Google Drive URL using PyDrive

查看:196
本文介绍了使用PyDrive管理来自公共Google Drive URL的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PyDrive快速入门脚本列出我的Google云端硬盘文件.

I`m using PyDrive QuickStart script to list my Google Drive files.

代码:

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)

我能够正常列出我的文件,但是我需要从已经通过身份验证的GoogleDrive帐户中的另一个公用驱动器URL(不是我的个人身份验证驱动器)中列出和管理文件,就像我在使用请求库一样. 有什么想法怎么做吗?

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. Any ideas how to do it?

推荐答案

  1. 您需要获取文件夹ID.您可以在文件夹的URL中找到ID.一个例子是: https://drive.google.com/open?id=0B-schRXnDFZeX0t0RnhQVXXXXXX(URL在id=之后的部分).

  1. You need to get the folder ID. You can find the ID in the URL of the folder. An example would be: https://drive.google.com/open?id=0B-schRXnDFZeX0t0RnhQVXXXXXX (the part of the URL after the id=).

根据ID列出文件夹的内容.根据您的代码,将file_list = ...替换为:

List contents of a folder based on ID. Given your code you replace file_list = ... with:

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

如果这不起作用,则可能需要在浏览器中打开共享文件夹时,使用共享文件夹右上角的添加到驱动器"按钮将远程文件夹添加到Google云端硬盘.

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.

2.1可以在文件夹中创建文件,如下所示:

2.1 Creating a file in a folder can be done like so:

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可以使用GitHub上可用的更新版本来完成文件的删除/删除操作: pip安装说明

2.2 Deleting/Trashing a file can be done with the updated version available from GitHub: pip install instructions, Delete/Trash/UnTrash documentation

最后,有一个功能请求,如2.1所述,上传到文件夹,并列出文件夹的文件,如2中所述.要工作,您可以将其作为问题/功能请求添加到存储库中.

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.

这篇关于使用PyDrive管理来自公共Google Drive URL的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆