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**AppDataLocalProgramsPythonPython36-32libsite- packagespydrivefiles.py", line 369, in _FilesInsert http=self.http) File "C:Users**AppDataLocalProgramsPythonPython36-32libsite- packagesoauth2client_helpers.py", line 133, in positional_wrapper return wrapped(*args, **kwargs) File "C:Users**AppDataLocalProgramsPythonPython36-32libsite- packagesgoogleapiclienthttp.py", line 813, in execute _, body = self.next_chunk(http=http, num_retries=num_retries) File "C:Users**AppDataLocalProgramsPythonPython36-32libsite- packagesoauth2client_helpers.py", line 133, in positional_wrapper return wrapped(*args, **kwargs) File "C:Users**AppDataLocalProgramsPythonPython36-32libsite- packagesgoogleapiclienthttp.py", line 981, in next_chunk return self._process_response(resp, content) File "C:Users**AppDataLocalProgramsPythonPython36-32libsite- packagesgoogleapiclienthttp.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**AppDataLocalProgramsPythonPython36-32libsite- packagespydrivefiles.py", line 285, in Upload self._FilesInsert(param=param) File "C:Users**AppDataLocalProgramsPythonPython36-32libsite- packagespydriveauth.py", line 75, in _decorated return decoratee(self, *args, **kwargs) File "C:Users**AppDataLocalProgramsPythonPython36-32libsite- packagespydrivefiles.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">
Advertisement
Answer
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.