I am trying to upload large attachments (>4 Mb). For that, the documentation suggests the following steps:
Creating a draft message -> starting upload session -> uploading attachment in chunks -> sending the mail
When executing the 3rd step, the code keeps running forever with no response (only started
is printed, but not done
). If I understand correctly, the requests.put()
should give me the NextExpectedRange
bytes to feed into the next put
iteration. But as I said, it doesnt give me any response. I tried hardcoding the upload process of the chunks as you can see in the code.
In some sites, it was suggested that the chunk length should be dividable by 327680
, thats why I have these 9 iterations. But even trying to upload the whole attachment as one chunk give me the same (no)reaction.
Messing with the header(e.g. taking out the Content-Length
) gives me an InvalidContentRange
error, changing put
to post
or the Content-Type
gives me the same (no)reaction.
Needless to say, I have all required permissions.
Would appreciate any kind of help :)
#1. create draft
payload=json.dumps({
"subject":subject,
"importance":"Low",
"body":{
"contentType":"Text",
"content":content},
"toRecipients":setup_recipients(recipients),
#"ccRecipients":setup_cc_recipients(ccrecipients)
})
mail_data_draft = requests.post(url=ms_graph_endpoint_draft, data=payload, headers={'Authorization': 'Bearer ' result['access_token'], 'Content-Type': 'application/json'})
message_id=json.loads(mail_data_draft.text)["id"]
#2. creating upload session with message id
upload_session_endpoint="https://graph.microsoft.com/v1.0/users/" email_account "/messages/" message_id "/attachments/createUploadSession"
up_session_payload= json.dumps({
"AttachmentItem":{
"attachmentType":"file",
"name":attachmentName,
"size": contentSize
}
})
upload_session_info= requests.post(url=upload_session_endpoint, data=up_session_payload, headers={'Authorization': 'Bearer ' result['access_token'], 'Content-Type': 'application/json'})
opaque_url =json.loads(upload_session_info.text)["uploadUrl"]
next_expected_range=json.loads(upload_session_info.text)["nextExpectedRanges"][0]
#####################################################################Tested till here
#3. uploading attachment
#problem
print("started")
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 0-327679/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 327680-655359/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 655360-983039/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 983040-1310719/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 1310720-1638399/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 16383400-1966079/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 1966080-2293759/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 2293760-2621439/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'327680','Content-Range':'bytes 2621440-2949119/3232962'})
requests.put(url=opaque_url,headers={'Content-Type': 'application/octet-stream','Content-Length':'283842','Content-Range':'bytes 2949120-3232961/3232962'})
print("done")
# sending draft
global ms_graph_endpoint
ms_graph_endpoint="https://graph.microsoft.com/v1.0/users/" email_account "/messages/" message_id "/send"
CodePudding user response:
Solved. Apparently I never told it which exact bytes to upload ^^