0

I'm trying to set up file upload using flask and then transfer the file to google drive.

It works most of the time but occasionally the transfer to google drive will hang after uploading the file using flask. The problem only occurs with larger files and not every time.

        filename = secure_filename(file.filename)
        filename=str(time.time()).replace('.','_')+'_____' + filename
        file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))


        file_metadata = {'name': displayName,
                     'parents': [foldertID] }

        media = MediaFileUpload('/var/flocation/' + filename)


        Gfile = service.files().create(body=file_metadata,
                                        media_body=media,
                                        fields='id').execute()
        fileID=Gfile.get('id')

        os.remove(os.path.join(app.config['UPLOAD_FOLDER'], filename))

Is there a problem with the file not completely being uploaded before starting the transfer to goggle drive? There is no error message, it just hangs. If I resubmit the upload request, it usually fixes the problem.

Thanks for the help.

Maddreddy
  • 11
  • 4
  • Have you checked and tried the solution from this [SO post](https://stackoverflow.com/questions/18796283/uploading-files-with-flask-running-on-app-engine-to-google-drive)? You can also check this similar [SO post](https://stackoverflow.com/questions/26346481/uploading-files-to-google-cloud-storage-with-flask) for more info. – Jessica Rodriguez Jan 08 '19 at 12:19
  • It looks like the same problem but I’m not using google app engine. I’m using ec2 taking to google docs. I don’t have access to blob store. – Maddreddy Jan 08 '19 at 23:16

1 Answers1

0

From the Resumable Media (chunked upload) section of the Google API Client Libraries (Python) - Media Upload Docs

For large media files, you can use resumable media uploads to send files, which allows files to be uploaded in smaller chunks. This is especially useful if you are transferring large files, and the likelihood of a network interruption or some other transmission failure is high. It can also reduce your bandwidth usage in the event of network failures because you don't have to restart large file uploads from the beginning.

And it suggests this method

media = MediaFileUpload('pig.png', mimetype='image/png', resumable=True)
request = farm.animals().insert(media_body=media, body={'name': 'Pig'})
response = None
while response is None:
  status, response = request.next_chunk()
  if status:
    print "Uploaded %d%%." % int(status.progress() * 100)
print "Upload Complete!"
Sam Hollenbach
  • 652
  • 4
  • 19
  • Do I have to use chunked upload to transfer the file initially to my server running the flask application also? I'm getting the following error messages below. line 346, in upload_file fields='id').execute() File "/usr/lib/python2.7/httplib.py", line 1123, in getresponse raise ResponseNotReady() – Maddreddy Jan 08 '19 at 08:19
  • Possibly. You may also want to do a check to see if the file is completely uploaded before you start the GDrive upload process. This could potentially help https://stackoverflow.com/a/21746775/6685140 – Sam Hollenbach Jan 08 '19 at 08:24
  • That solved it. I used chunked upload to transfer the file to my flask server initially and I’m not getting any errors uploading to google drive anymore. – Maddreddy Jan 13 '19 at 16:53