When uploading file using LargeFileUploadTask (streaming and not in one part) the file getting corrupted in the SharePoint server

Hudedi Itay 0 Reputation points
2024-12-24T06:17:04.8433333+00:00

I am using latest version of graph api - 6.24.0 When I uploading any file using graphServiceClient.drives().byDriveId(driveId).items().byDriveItemId(getDriveItemIdFromRootPath(filePath)).content().put(fileContent) file is uploading correct.

When I upload via the LargeUploadTask - file is corrupted for any file such as MSOffice file or dat file or jar file. For MSOffice the file can't be opened and for the dat file null values are being inserted to the file. The code that I am using for the uploading in streaming is:

LargeFileUploadTask largeFileUploadTask = null; 
IProgressCallback callback = null; 
UploadResult uploadResult = null;
try { 
     int spoPartSize = 5242880;
    // Set body of the upload session request
    // This is used to populate the request to create an upload session
    DriveItemUploadableProperties driveItemUploadableProperties = new DriveItemUploadableProperties();
    driveItemUploadableProperties.getAdditionalData().put("@microsoft.graph.conflictBehavior", "replace");

    // Finish setting up the request body
    CreateUploadSessionPostRequestBody uploadSessionPostRequestBody = new CreateUploadSessionPostRequestBody();
    uploadSessionPostRequestBody.setItem(driveItemUploadableProperties);

    // Create the upload session
    UploadSession uploadSession = graphServiceClient.drives()
            .byDriveId(driveId)
            .items()
            .byDriveItemId(getDriveItemIdFromRootPath(filePath))
            .createUploadSession().post(uploadSessionPostRequestBody);

    if (null == uploadSession) {
        throw new SPOException("SPOWrapper::uploadObjectInParallel: Could not create upload session");
    }

    // Create the large file upload task
    largeFileUploadTask = new LargeFileUploadTask(graphServiceClient.getRequestAdapter(),
            uploadSession,
            fileContent,
            fileSize,
            spoPartSize,
            DriveItem::createFromDiscriminatorValue);

    if (null == largeFileUploadTask) {
        throw new SPOException("SPOWrapper::uploadObjectInParallel: Could not create upload task");
    }

    // Create a callback used by the upload provider
    callback = new SPOProgressCallback(spoFileCopy);

    // Do the upload
    uploadResult = largeFileUploadTask.upload(spoRequestMaxAttempts, callback);
    if (uploadResult == null || !uploadResult.isUploadSuccessful()) {
        throw new SPOException(String.format("SPOWrapper::uploadObjectInParallel: filePath= '%s' wasn't uploaded successfully via upload method", originalFilePath));
    } else {
        logger.debug(String.format("SPOWrapper::uploadObjectInParallel: filePath= '%s' uploaded successfully via upload method", originalFilePath));
    }
} catch (Exception e) {
    boolean spoIgnoreFailureWhenUploadingFileInParts = Boolean.valueOf(hostProperties.getParameterValueFromAdditionalParamsOrFromAftConfig(mftPropertiesConfig, PropertyData.spoIgnoreFailureWhenUploadingFileInParts, AdditionalParametersConsts.spoIgnoreFailureWhenUploadingFileInParts));
    logger.error(String.format("Error in SPOWrapper::uploadObjectInParallel, filePath = '%s', fileSize = '%s' : %s", originalFilePath, fileSize, e.getMessage()), e);
   /* try {
        if (uploadTask != null && callback != null) {
            uploadResult = uploadTask.resume(spoRequestMaxAttempts, callback);
            if (uploadResult == null || !uploadResult.isUploadSuccessful()) {
                throw new SPOException(String.format("SPOWrapper::uploadObjectInParallel: filePath= '%s' wasn't uploaded successfully via resume method", originalFilePath));
            } else {
                logger.debug(String.format("SPOWrapper::uploadObjectInParallel: filePath= '%s' uploaded successfully via resume method", originalFilePath));
                return;
            }
        }
    } catch (Exception ex) {
              logger.error(String.format("Error in SPOWrapper::uploadObjectInParallel, filePath = '%s', fileSize = '%s' : %s", originalFilePath, fileSize, e.getMessage()), e);
    throw e;
} finally {
    logger.debug(String.format("End SPOWrapper::uploadObjectInParallel: filePath= '%s', fileSize= %s", originalFilePath, fileSize));
}
```Thanks, Itay
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,651 questions
0 comments No comments
{count} votes

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.