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

Elr*_*oum 5 python file-upload pydrive

我正在使用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()
Run Code Online (Sandbox Code Playgroud)

我已经在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()
Run Code Online (Sandbox Code Playgroud)

文件"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">
Run Code Online (Sandbox Code Playgroud)

你能帮我找到什么问题吗?非常感谢提前.

Pac*_* H. 6

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

file1 = drive.CreateFile({'title': 'Hello.txt'})
file1.SetContentFile(path_to_your_file)
file1.Upload()
Run Code Online (Sandbox Code Playgroud)

正如文档所述,如果您尚未设置title,mimeType则将根据您提供的文件的名称和类型自动设置它们.因此,如果您要上传与计算机上已有的名称相同的文件,您可以执行以下操作:

file1 = drive.CreateFile()
file1.SetContentFile(path_to_your_file)
file1.Upload()
Run Code Online (Sandbox Code Playgroud)

关于你的第二点,据我所知,GDrive无法将文件转换为其他格式.

  • @gented 您可以指定父文件夹的 ID,如下所示 `file =drive.CreateFile({'parents': [{'kind': 'drive#fileLink', 'id':folder_id}]})`。您可以在此处检查所有属性 https://developers.google.com/drive/api/v2/reference/files#resource-representations (2认同)