File Corruption in SharePoint Upload via Microsoft Graph API - Random Issue

Vivek Buddhadev 51 Reputation points
2025-03-24T13:20:30.3933333+00:00

Issue Description:

I am using the Microsoft Graph API 5.80.0 to upload files to SharePoint from a Java Spring Boot application. The upload works fine in normal conditions, but some files become corrupted while uploading & so unable to download from SharePoint.

Most of the upload files are smaller than 4 MB, so I am using the simple upload method (PUT request).
The upload process completes successfully (returns 200 OK with a valid file ID). When I receive the webhook for the same the file size is 0 & not able to open the file as the file is corrupted. It happens randomly for a few of the requests.

private fun uploadFile(document: DocumentUploadStream) {
    val timeMillis = System.currentTimeMillis()
    val buildDriveItemRequest : DriveItemRequestBuilder = microsoftGraphClient.drives("driveId").root().itemWithPath(document.path)

    
    if (document.size < 4 * 1024 * 1024) { 
        // Simple upload for small files
        val driveItem = retry {
            refreshToken { 
                buildDriveItemRequest.content().buildRequest()
                    .put(document.contentToStream.readAllBytes()) 
            }
        }
        logger.info { "Upload done ${document.path}, id: ${driveItem?.id}, time: ${(System.currentTimeMillis() - timeMillis)/1000}s" }
        document.id = driveItem?.id
    } else { 
        // Large files use upload sessions (this part is working fine)
    }
}

data class DocumentUploadStream(
        val path: DocumentPath,
        val contentToStream: InputStream,
        val size: Long
)

Any insights or recommendations would be greatly appreciated!

Thanks!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,483 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.