1

I'm uploading a file from one GDrive account to another, using PyDrive. I can download the file fine, but then when I go to upload it, I get a 400.

pydrive.files.ApiRequestError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v2/files?alt=json&uploadType=resumable returned "Bad Request">

This is the ListFolder code:

def ListFolder(parent, drive1, drive2):
    filecount = 0 
    filelist=[]
    file_list = drive1.ListFile({'q': "'%s' in parents and trashed=false" % parent}).GetList()
    for f in file_list:
        filecount +=1
        print("Still working %s" % filecount)
        if f['mimeType']!='application/vnd.google-apps.folder': # if regular file
            print("I've found a file - passing %s to download" % f['title'])
            download_file(drive1, f, drive2) # Download the file

Here is the download file code:

def download_file(drive1, f, drive2):
    """This function will download a file from the source drive1 to the local hard drive, just before uploading it to the destination drive- i.e. drive2"""
    dirpath = tempfile.mkdtemp()
    print ("Downloading a file %s" % f['title'])
    filesource = drive1.CreateFile({'id': f['id']})
    filesource.GetContentFile(f['title'], f['mimeType'])
    upload_file(drive2, f)
    os.remove(f['title'])

drive2 is a GoogleDrive instance (with auth passed). f is an item in a file_list that is not a folder.

def upload_file(drive2, f):
    #Will copy files over until it detects a directory
    """"This function accepts the destination drive, filetitle (which has previously been downloaded from download_file function) and the file mimetype. This function uploads the local file to the destination drive"""
    filename = f['title']
    print("file title: %s" % f['title']) #filetitle)
    file = drive2.CreateFile()
    file.SetContentFile(f['title'])
    file.Upload()

I've tried using a string to upload the file, but still get the same results. What else should I be looking for?

Adam
  • 442
  • 1
  • 6
  • 18
  • 400 Bad Request means a parameter you provided for certain property is not valid. – ReyAnthonyRenacia Jan 24 '18 at 04:09
  • Thanks - I've tried just a regular filename in that parameter (as a string) and get the same effect. – Adam Jan 24 '18 at 08:01
  • It turns out that regular files upload fine (e.g. goats.txt). However, the file I'm trying to work with is a Gdoc - do these have a different mimeType and therefore upload function? – Adam Jan 24 '18 at 08:47
  • Yes. A google doc is not the same as a .txt file with regard to 'mimeType'. The list of google file mimeType is found [here](https://developers.google.com/drive/v3/web/mime-types). That's 'application/vnd.google-apps.document'. – ReyAnthonyRenacia Jan 24 '18 at 08:49
  • 1
    Also, it doesn't like the fact if a file is empty - could this be a bug? – Adam Jan 24 '18 at 10:19
  • Which function is used for download a different mimeType file? I can download .txt files but not GDocs. – Adam Jan 24 '18 at 13:37

1 Answers1

1

here's the download snippet from the export files guide:

file_id = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'
request = drive_service.files().export_media(fileId=file_id,
                                             mimeType='application/vnd.google-apps.document')
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
    status, done = downloader.next_chunk()
    print "Download %d%%." % int(status.progress() * 100)
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56