I am trying to upload a large file to gdrive using the google-api-client. I am using the resumable upload. The problem here is I don't want the file object to be saved/write in my file system. I want it to read it in chunks and therefore upload the same chunks to Gdrive using resumable upload. Is there anyway where this can be achieved where i can send chunks via the google api python client.
Here is my sample Code which works but it gets the entire file object from client.
@app.route('/upload', methods = ["GET", "POST"])
def upload_buffer():
drive = cred()
if request.method == "POST":
mime_type = request.headers['Content-Type']
body = {
'name': "op.pdf",
'mimeType': mime_type,
}
chunk = BytesIO(request.stream.read()) # as you can see here the entire file stream is obtained
#I want to read in chunks and simultaneously send that chunk to GDrive
#chunk = BytesIO(request.stream.read(1024))
#if I send like the above only some part of the file is uploaded in Gdrive
media_body = MediaIoBaseUpload(chunk, chunksize = 1024, mimetype=mime_type,
resumable=True)
return drive.files().create(body=body,
media_body=media_body,
fields='id,name,mimeType,createdTime,modifiedTime').execute()
return render_template("upload_image.html")
<-------------------------------------------------------------------> This is how I approached using the Google Rest APIs
@app.route('/upload3', methods=["GET", "POST"])
def upload_buff():
if request.method == "POST":
Content_Length = request.headers['Content-Length']
access_token = '####'
headers = {"Authorization": "Bearer " + access_token, "Content-Type": "application/json", "Content-Length": Content_Length}
params = {
"name": "file_name.pdf",
"mimeType": "application/pdf"
}
r = requests.post("https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable", headers=headers, data=json.dumps(params))
location = r.headers['Location']
print("----------------------------------------------------")
print("GDrive Upload url : ", location)
print("----------------------------------------------------")
start = str(0)
while True:
chunk = request.stream.read(1024 * 1024)
chunk_size = len(chunk)
print("----------------------------------------------------")
print("Size of Received Chunk From Client: ", chunk_size)
print("----------------------------------------------------")
if chunk_size == 0:
break
end = str(int(start)+chunk_size-1)
headers = {'Content-Range': 'bytes '+start+'-' + end + '/' +str(Content_Length), 'Content-Length': str(chunk_size)}
start = str(int(end)+1)
print("The headers set for the chunk upload : ", headers)
r = requests.put(location, headers=headers, data=chunk)
print("----------------------------------------------------")
print("Response content : ", r.content)
print("Response headers : ", r.headers)
print("Response status : ", r.status_code)
print("----------------------------------------------------")
return r.content
return render_template("upload_image.html")