'MS Graph SDK for Java failed to upload large Word files

We are having problem uploading large Office files (Word, Excel) to Teams using MS Graph SDK for Java. Just wondering if anyone experienced the similar issue.

Here are some interesting points:

  1. The problem happens constantly when the file size is over 200M. I don't see problem with file smaller than 100M.
  2. The problem only happens for MS Office files(Word, Excel for example), we have no problem uploading text files or zip files up to 800M;
  3. Most of the time, the exception happens at the end of uploading, and, the file actually was uploaded to MS Teams. Although the exception was thrown, we can see files in Teams and files can be opened without problem.
  4. We did see the exception happened in the middle of uploading, then the file was not uploaded to Teams.

Here is the code:

public static void main(String[] args) {
        List<String> scopes = Arrays.asList("https://graph.microsoft.com/.default");


        String fileName = "270MBwith_video.docx";
        String localFilePath = "C:\\temp\\testfiles\\"+fileName;
        // Get an input stream for the file
        File file = new File(localFilePath);
        InputStream fileStream = null;
        try {
            fileStream = new FileInputStream(file);

            long streamSize = file.length();

            // Create a callback used by the upload provider
            IProgressCallback callback = new IProgressCallback() {
                @Override
                // Called after each slice of the file is uploaded
                public void progress(final long current, final long max) {
                    System.out.println(String.format("Uploaded %d bytes of %d total bytes", current, max));
                }
            };

            DriveItemCreateUploadSessionParameterSet uploadParams = DriveItemCreateUploadSessionParameterSet
                    .newBuilder().withItem(new DriveItemUploadableProperties()).build();

            TokenCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                    .clientId("61117459-d25c-4a98-a8bb-**********").clientSecret("0lyT.5zoqn*********")
                    .tenantId("******.onmicrosoft.com").build();

            TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes,
                    clientSecretCredential);

            GraphServiceClient<Request> graphClient = GraphServiceClient.builder()
                    .authenticationProvider(tokenCredentialAuthProvider).buildClient();

            // Create an upload session
            UploadSession uploadSession = graphClient
                    .drives("b!Mca1ElNvN0C6oM-T4Tln0L0SJAZimN9Dkdbe****")
                    .items("017N4JGSDWBUSNV4CPWZDJMLJZJFQZZS6X").itemWithPath(fileName).createUploadSession(null)
                    .buildRequest().post();

            LargeFileUploadTask<DriveItem> largeFileUploadTask = new LargeFileUploadTask<DriveItem>(uploadSession,
                    graphClient, fileStream, streamSize, DriveItem.class);

            // Do the upload
            LargeFileUploadResult<DriveItem> item = largeFileUploadTask.upload(10 * 320 * 1024, null, callback);
            System.out.println("Uploaded to: " + item.responseBody.webUrl);
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

Here is the exception:

Uploaded 271974400 bytes of 278847403 total bytes
Uploaded 275251200 bytes of 278847403 total bytes
com.microsoft.graph.core.ClientException: Request failed with error, retry if necessary.
    at com.microsoft.graph.tasks.LargeFileUploadRequest.upload(LargeFileUploadRequest.java:100)
    at com.microsoft.graph.tasks.LargeFileUploadTask.uploadAsync(LargeFileUploadTask.java:165)
    at com.microsoft.graph.tasks.LargeFileUploadTask.upload(LargeFileUploadTask.java:253)
    at testGraphSDK.largeFileUpload.main(largeFileUpload.java:72)
Caused by: com.microsoft.graph.core.ClientException: Error executing the request
    at com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:406)
    at com.microsoft.graph.http.CoreHttpProvider.send(CoreHttpProvider.java:222)
    at com.microsoft.graph.tasks.LargeFileUploadRequest.upload(LargeFileUploadRequest.java:98)
    ... 3 more
Caused by: java.net.SocketTimeoutException: Read timed out
    at java.base/java.net.SocketInputStream.socketRead0(Native Method)
    at java.base/java.net.SocketInputStream.socketRead(SocketInputStream.java:115)
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:168)
    at java.base/java.net.SocketInputStream.read(SocketInputStream.java:140)
    at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:448)
    at java.base/sun.security.ssl.SSLSocketInputRecord.bytesInCompletePacket(SSLSocketInputRecord.java:68)
    at java.base/sun.security.ssl.SSLSocketImpl.readApplicationRecord(SSLSocketImpl.java:1103)
    at java.base/sun.security.ssl.SSLSocketImpl$AppInputStream.read(SSLSocketImpl.java:823)
    at okio.Okio$2.read(Okio.java:140)
    at okio.AsyncTimeout$2.read(AsyncTimeout.java:237)
    at okio.RealBufferedSource.indexOf(RealBufferedSource.java:358)
    at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.java:230)
    at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.java:242)
    at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.java:213)
    at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.java:115)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:94)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:43)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:94)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:88)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at com.microsoft.graph.httpcore.RedirectHandler.intercept(RedirectHandler.java:137)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at com.microsoft.graph.httpcore.RetryHandler.intercept(RetryHandler.java:176)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at com.microsoft.graph.httpcore.AuthenticationHandler.intercept(AuthenticationHandler.java:57)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at com.microsoft.graph.httpcore.TelemetryHandler.intercept(TelemetryHandler.java:68)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:142)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:117)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:229)
    at okhttp3.RealCall.execute(RealCall.java:81)
    at com.microsoft.graph.http.CoreHttpProvider.sendRequestInternal(CoreHttpProvider.java:403)
    ... 5 more


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source