用 Graphic API 。
``` python
def get_upload_session_url(path, filename):
url = f"
https://graph.microsoft.com/v1.0/me/drive/root:/{config['root_path']}/{path}/{filename}:/createUploadSession"
payload = {
"item" : {
"@microsoft.graph.conflictBehavior": "replace",
"name": filename
}
}
resp =
r.post(url, headers=add_token_header({'Content-Type': 'application/json'}), data=json.dumps(payload))
return json.loads(resp.content)['uploadUrl']
def upload_file_body(upload_url, localfile):
f = open(localfile, 'rb')
data = f.read()
headers = {
'Content-Length':f'{len(data)}',
'Content-Range': f'bytes 0-{len(data)-1}/{len(data)}'
}
resp = r.put(upload_url, data=data, headers=headers)
f.close()
return json.loads(resp.content)['id']
```
上传的核心代码。是适合大文件的那种。