简体   繁体   English

PyDrive - 擦除文件的内容

[英]PyDrive - Erase contents of a file

Consider the following code that uses the PyDrive module:考虑以下使用PyDrive模块的代码:

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

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

file = drive.CreateFile({'title': 'test.txt'})
file.Upload()

file.SetContentString('hello')
file.Upload()

file.SetContentString('')
file.Upload()    # This throws an exception.

Creating file and changing its contents works fine until I try to erase the contents by setting the content string to an empty one.创建文件并更改其内容工作正常,直到我尝试通过将内容字符串设置为空字符串来擦除内容。 Doing so throws this exception:这样做会引发此异常:

pydrive.files.ApiRequestError
<HttpError 400 when requesting
https://www.googleapis.com/upload/drive/v2/files/{LONG_ID}?alt=json&uploadType=resumable
returned "Bad Request">

When I look at my Drive, I see the test.txt file successfully created with text hello in it.当我查看我的 Drive 时,我看到test.txt文件已成功创建,其中包含文本hello However I expected that it would be empty.但是我预计它会是空的。

If I change the empty string to any other text, the file is changed twice without errors.如果我将空字符串更改为任何其他文本,则文件会更改两次而不会出错。 Though this doesn't clear the contents so it's not what I want.虽然这并没有清除内容,所以这不是我想要的。

When I looked up the error on the Internet, I found this issue on PyDrive github that may be related though it remains unsolved for almost a year.我在网上查了这个错误,在PyDrive github上发现了这个问题,这个问题可能是相关的,虽然已经快一年没有解决了。

If you want to reproduce the error, you have to create your own project that uses Google Drive API following this tutorial from the PyDrive docs.如果要重现该错误,则必须按照 PyDrive 文档中的本教程创建自己的使用 Google Drive API 的项目。

How can one erase the contents of a file through PyDrive?如何通过 PyDrive 擦除文件的内容?

Issue and workaround:问题和解决方法:

When resumable=True is used, it seems that the data of 0 byte cannot be used.使用resumable=True的时候,好像不能使用0字节的数据。 So in this case, it is required to upload the empty data without using resumable=True .所以在这种情况下,需要在不使用resumable=True情况下上传空数据。 But when I saw the script of PyDrive, it seems that resumable=True is used as the default.但是当我看到PyDrive的脚本时,似乎默认使用了resumable=True Ref So in this case, as a workaround, I would like to propose to use the requests module. Ref所以在这种情况下,作为一种解决方法,我想建议使用requests模块。 The access token is retrieved from gauth of PyDrive.访问令牌是从gauth的 gauth 中检索的。

When your script is modified, it becomes as follows.当你的脚本被修改时,它变成如下。

Modified script:修改后的脚本:

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

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

file = drive.CreateFile({'title': 'test.txt'})
file.Upload()

file.SetContentString('hello')
file.Upload()

# file.SetContentString()
# file.Upload()    # This throws an exception.

# I added below script.
res = requests.patch(
    "https://www.googleapis.com/upload/drive/v3/files/" + file['id'] + "?uploadType=multipart",
    headers={"Authorization": "Bearer " + gauth.credentials.token_response['access_token']},
    files={
        'data': ('metadata', '{}', 'application/json'),
        'file': io.BytesIO()
    }
)
print(res.text)

References:参考:

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM