10

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">
vvvvv
  • 25,404
  • 19
  • 49
  • 81
Elroum
  • 327
  • 1
  • 3
  • 18

2 Answers2

15

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.

Paco H.
  • 2,034
  • 7
  • 18
  • thank you so much for your replay. i've tested this code , but it shows an error . such : googleapiclient.errors.HttpError: . can you hepl me know where is the problem ? – Elroum Oct 04 '17 at 11:12
  • setContentString works perfectly . but when i replace it with setContentFile , it shows the 400 ERROR bad request. – Elroum Oct 04 '17 at 12:04
  • Can you update the question with your new code and the full traceback? – Paco H. Oct 04 '17 at 12:07
  • Can you make sure that you can read the file, `with open('big.txt') as file: print(file.readlines())` – Paco H. Oct 04 '17 at 12:29
  • i tested and , yes i can read the file using open('big.txt') . – Elroum Oct 04 '17 at 12:43
  • 1
    it's okey, i've found that the error accured because my file was emty. when i wrote some text in the file, it was aploaded to my drive. THANK YOU SO MUCH for your answers. about my second point , manualy in GD we can open a pdf file as docx that's why i was wondring if i can do it using Pydrive – Elroum Oct 04 '17 at 13:17
  • Is there a way to specify where exactly to upload the file (namely the upload path)? It is very unclear from the `pydrive` documentation where the file is going to be uploaded. – gented Nov 08 '19 at 16:28
  • 2
    @gented You can specify the ID of a parent folder like this `file = drive.CreateFile({'parents': [{'kind': 'drive#fileLink', 'id': folder_id}]})`. You can check all the attributes here https://developers.google.com/drive/api/v2/reference/files#resource-representations – Paco H. Nov 12 '19 at 09:13
1

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"})
ingofreyer
  • 1,086
  • 15
  • 27
  • thank you very much for your reply. i've tested the code but it always show an error . i guess there is a problem in the Upload the errore is: function.googleapiclient.errors.HttpError: do you have any ideo how to fix it – Elroum Oct 04 '17 at 11:09
  • I see that Paco was faster than me. Your problem is solved with his answers, right? – ingofreyer Oct 05 '17 at 12:08
  • you give me a correct answer Too, but i've got an error , and asked you both about it . Paco was the one who answered me , an kept trying to help me.. that's why when the problem was solved, i've marked up his answer as correct one. is there the possibility to mark two answers as correct ? – Elroum Oct 05 '17 at 13:23
  • It is not and that is completely fine. He was faster and deserves it :-) – ingofreyer Oct 05 '17 at 13:48
  • 1
    i really wish i could give it to you too. anyways, i'm so gratful for your help Both. – Elroum Oct 05 '17 at 13:56