gpt4 book ai didi

python : upload my own files into my drive using Pydrive library

转载 作者:太空狗 更新时间:2023-10-30 00:58:58 28 4
gpt4 key购买 nike

我想将我的文件上传到我的驱动器中。但是在 Pydrive 文档中我发现只有 upload() 函数上传由 drive.CreateFile() 函数创建的文件并更新它,而不是我硬盘中的文件(我自己的文件)。

file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile 
instance with title 'Hello.txt'.
file1.SetContentString('Hello World!') # Set content of the file from given
string.
file1.Upload()

我已经在 stackoverflow 中尝试了我的问题的答案,但是出现了一个错误。这是我的代码:

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

#1st authentification
gauth = GoogleAuth()
gauth.LocalWebserverAuth() # Creates local webserver and auto handles
#authentication.
drive = GoogleDrive(gauth)

file1 = drive.CreateFile(metadata={"title": "big.txt"})
file1.SetContentFile('big.txt')
file1.Upload()

文件“big.txt”与我的代码文件位于同一文件夹中。当我运行它时,我得到了这个回溯:

Traceback (most recent call last):
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\pydrive\files.py", line 369, in _FilesInsert
http=self.http)
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\googleapiclient\http.py", line 813, in execute
_, body = self.next_chunk(http=http, num_retries=num_retries)
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\oauth2client\_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\googleapiclient\http.py", line 981, in next_chunk
return self._process_response(resp, content)
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\googleapiclient\http.py", line 1012, in _process_response
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting
https://www.googleapis.com/upload/drive/v2/files?
alt=json&uploadType=resumable returned "Bad Request">

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:/Users/**/AppData/Local/Programs/Python/Python36-
32/quickstart.py", line 13, in <module>
file1.Upload()
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\pydrive\files.py", line 285, in Upload
self._FilesInsert(param=param)
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\pydrive\auth.py", line 75, in _decorated
return decoratee(self, *args, **kwargs)
File "C:\Users\**\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\pydrive\files.py", line 371, in _FilesInsert
raise ApiRequestError(error)
pydrive.files.ApiRequestError: <HttpError 400 when requesting
https://www.googleapis.com/upload/drive/v2/files?
alt=json&uploadType=resumable returned "Bad Request">

最佳答案

您必须使用 SetContentFile() 设置内容而不是 SetContentString() :

file1 = drive.CreateFile({'title': 'Hello.txt'})
file1.SetContentFile(path_to_your_file)
file1.Upload()

如文档所述,如果您没有设置 titlemimeType,它们将根据您提供的文件的名称和类型自动设置。因此,如果您想上传与计算机上已有的同名文件,您可以执行以下操作:

file1 = drive.CreateFile()
file1.SetContentFile(path_to_your_file)
file1.Upload()

关于您的第二点,据我所知,GDrive 无法将文件转换为不同的格式。

关于 python : upload my own files into my drive using Pydrive library,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46562255/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com