Google drive API part2 : uploading large file
different example on how to upload big file to google drive
Apparently there are use cases that I want to upload file not from my laptop, but from my servers in data center.
As usual, the google documentation for their drive API is chunky and incomplete. Had to go through stackoverflow again.
If you followed my previous post about downloading file, the upload part is pretty straightforward.
First we need to import MediaFileUpload:
from googleapiclient.http import MediaFileUpload
Then you can replace the download part into
# get filename from argumentthe_file_to_upload = sys.argv[1]metadata = {'name': the_file_to_upload}media = MediaFileUpload(the_file_to_upload, chunksize=5 * 1024 * 1024, mimetype='application/octet-stream', resumable=True)request = service.files().create(body=metadata, media_body=media)response = Nonewhile response is None: status, response = request.next_chunk() if status: print("Uploaded %d%%." % int(status.progress() * 100))
print("Upload of {} is complete.".format(the_file_to_upload))
For complete example, please download from here:
Filename to be uploaded is supplied by argument when running python. Chunk size is the size per upload request. There I set 5 MB (40mbps), therefore only use this number if your ISP speed is ≥ 40mbps for uploading.
I have tested this by uploading 15GB file, and then re-download it. Then I tested their checksum, matching or not. While uploading, I got constant speed of 200mbps from my server.
Thank you for reading! 🍻