Python:使用Pydrive库将我自己的文件上传到我的驱动器中

2024-04-20 10:57:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python发现PyDrive API。 我想上传我的文件到我的驱动器。不过,在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">

你能帮我找出哪里不对劲吗? 提前多谢了。


Tags: inpylibpackageslocallinesitedrive
2条回答

必须用^{}而不是^{}设置内容:

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无法将文件转换为其他格式。

基于documentation of PyDrive,我想说,您需要执行以下操作:

file_path = "path/to/your/file.txt"
file1 = drive.CreateFile()
file1.SetContentFile(file_path)
file1.Upload()

标题和内容类型元数据根据提供的文件路径自动设置。如果要提供其他文件名,请将其传递给CreateFile(),如下所示:

file1 = drive.CreateFile(metadata={"title": "CustomFileName.txt"})

相关问题 更多 >