Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.4k views
in Technique[技术] by (71.8m points)

Microsoft Graph (OneDrive) API - Resumable Upload Content-Type

I am trying to create the upload PUT request for the OneDrive API. It's the large file "resumable upload" version which requires the createUploadSession.

I have read the Microsoft docs here: As a warning the docs are VERY inaccurate and full of factual errors...

The docs simply say:

PUT https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337
Content-Length: 26
Content-Range: bytes 0-25/128

<bytes 0-25 of the file>

I am authenticated and have the upload session created, however when I pass the JSON body containing my binary file I receive this error:

{ "error": { "code": "BadRequest", "message": "Property file in payload has a value that does not match schema.", .....

Can anyone point me at the schema definition? Or explain how the JSON should be constructed?

As a side question, am I right in using "application/json" for this at all? What format should the request use?

Just to confirm, I am able to see the temp file created ready and waiting on OneDrive for the upload, so I know I'm close.

Thanks for any help!

question from:https://stackoverflow.com/questions/65868460/microsoft-graph-onedrive-api-resumable-upload-content-type

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're uploading the entire file in a single request then why do you use upload session when you can use the simple PUT request?

url = https://graph.microsoft.com/v1.0/{user_id}/items/{parent_folder_ref_id}:/{filename}:/content and "Content-Type": "text/plain" header and in body simply put the file bytes.

If for some reason I don't understand you have to use single-chunk upload session then:

  1. Create upload session (you didn't specified any problems here so i'm not elaborating)

  2. Get uploadUrl from createUploadSession response and send PUT request with the following headers:

    2.1 "Content-Length": str(file_size_in_bytes)

    2.2 "Content-Range": "bytes 0-{file_size_in_bytes - 1}/{file_size_in_bytes}"

    2.3 "Content-Type": "text/plain"

  3. Pass the file bytes in body.

Note that in the PUT request the body is not json but simply bytes (as specified by the content-type header.

Also note that max chuck size is 4MB so if your file is larger than that, you will have to split into more than one chunks.

Goodlcuk


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...