8

I want to upload my file into my drive. However in Pydrive Documentation I found only upload() function that uploads a file created by drive.CreateFile() function and update it, and not the file in my hard drive (my own file).

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()

I've tried the ansewers of my question here in stackoverflow, but an error accured . here is my code :

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()

The file "big.txt" is in the same folder of my code file. When I run it, I got this traceback:

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">
4

2 回答 2

12

You have to set the content with SetContentFile() instead of SetContentString():

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

As the documentation states, if you haven't set the title and mimeType they will be set automatically from the name and type of the file your give. Therefore if you want to upload the file with the same name it already has on your computer you can do:

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

Regarding your second point, as far as I'm aware GDrive can not convert a file to a different format.

于 2017-10-04T10:15:36.477 回答
0

Based on the documentation of PyDrive, I would say, you need to do the following:

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

Title and content type metadata are set automatically based on the provided file path. If you want to provide a different filename, pass it to CreateFile() like this:

file1 = drive.CreateFile(metadata={"title": "CustomFileName.txt"})
于 2017-10-04T10:15:08.787 回答