MS Graph Java SDK: how to upload a large file to O

2020-07-23 07:06发布

问题:

I'm trying to upload file to a OneDrive from my Java application, but i dont understand the flow and dont see any documentation or methods within SKD for file upload.

The only flow i've found is:

driveClient.me().drive().root().createUploadSession(uploadProperties).buildRequest().post()

But this results in NPE: source.

how can i set the content as InputStream ?

回答1:

You must use the ChunkedUploadProvider. Here is the official sample code for uploading large files: https://docs.microsoft.com/en-us/graph/sdks/large-file-upload?tabs=java

For me this sample worked for files with size less than 1.9921875 GB. Unfortunately larger files failed with "Upload session failed too many times" error. The solution is to get the file size using File.length() method:

long streamSize = file.length();

instead of

long streamSize = (long)fileStream.available();

as it is shown in the sample.

This happens because fileStream.available() returns the available bytes as integer and integer's max value in bytes is ~2GB.

I hope this helps you.