【问题标题】:Deleting file from google share drive using pydrive, file exist, but API return 404 File not found error使用 pydrive 从 google 共享驱动器中删除文件,文件存在,但 API 返回 404 File not found 错误
【发布时间】:2021-02-20 06:17:37
【问题描述】:

我正在尝试使用 pydrive 删除共享驱动器上的文件,但我无法删除该文件。 当我获得有关驱动器上文件的信息时,它会显示文件 ID,但是当我使用“Trash() 函数”时,它会显示错误为“找不到文件:XXX”。

开发环境

ubuntu 20.04 on docker
python 3.7
pydrive 1.3.1

代码内容

from pydrive.auth import GoogleAuth
from oauth2client.service_account import ServiceAccountCredentials
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
scope = ['https://www.googleapis.com/auth/drive']
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name("client_secrets.json", scope)
drive = GoogleDrive(gauth)

for f in drive.ListFile({'driveId': 'AAA', 'corpora': 'drive', 'includeItemsFromAllDrives': 'true',
 'supportsAllDrives': 'true', 'q': 'title = "lock" and trashed = false'}).GetList():
    print(f['title'], '  \t', f['id'])

    f_txt = drive.CreateFile(f)
    f_txt.Trash()

错误内容

root@test:~/reman/src/For_lambda/refer# python3 delete_lockfile.py 
lock     10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/pydrive/files.py", line 410, in _FilesTrash
    http=self.http)
  File "/usr/local/lib/python3.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/googleapiclient/http.py", line 915, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError:
 <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I/trash?alt=json returned 
"File not found: 10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I". 
Details: "File not found: 10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I">

如上所示,我可以得到[fileid: 10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I],但是当我删除文件时,它显示为未找到。

我不知道发生了什么。请帮忙!谢谢,

【问题讨论】:

标签: python pydrive


【解决方案1】:

您似乎没有上传文件。使用drive.CreateFile(f),您正在创建一个GoogleDriveFile 实例,但您必须使用Upload() 方法来执行此操作。这就是为什么它向您显示File not found 异常。你可以在 for 中尝试这样的事情:

    f_txt = drive.CreateFile(f)
    f_txt.Upload()
    f_txt.Trash()

【讨论】:

  • 感谢您的支持。我试过上面的内容。但在 上传功能 发生类似错误。错误如下。 Traceback (most recent call last): File "delete_lockfile.py", line 18, in &lt;module&gt; f_txt.Upload() File "/usr/local/lib/python3.7/site-packages/pydrive/files.py", line 239, in FetchMetadata raise ApiRequestError(error) pydrive.files.ApiRequestError: &lt;HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I?alt=json returned "File not found: 10xM-FIkMoyRIb7PC1VBxZZJn_QePxy-I".
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-04
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
相关资源
最近更新 更多