Search code examples
javamicrosoft-graph-apimicrosoft-graph-sdksmicrosoft-graph-files

Cannot Upload File to OneDrive with Java SDK


I cannot upload a file to OneDrive using the MS Graph Java SDK, nor can I find any Java-based examples.

I have found a similar (but unanswered) question here: MS Graph Java SDK: how to upload a large file to OneDrive?

However, I've managed to cobble together a bit more code that was shown in the question raised above (based on a .NET-based example), but still without success.

FileSystemInfo fileSystemInfo = new FileSystemInfo();
fileSystemInfo.createdDateTime = new GregorianCalendar();
fileSystemInfo.lastModifiedDateTime = new GregorianCalendar();

DriveItemUploadableProperties properties = new DriveItemUploadableProperties();
properties.name = "test.jpeg";
properties.description = "TEST_DESCRIPTION";
properties.fileSystemInfo = fileSystemInfo;

IDriveItemCreateUploadSessionRequest request = this.graphServiceClient
        .me()
        .drive()
        .root()
        .createUploadSession(properties)
        .buildRequest();

UploadSession session = request.post();

if(Objects.nonNull(session)) {
    int maxSizeChunk = (320 * 1024) * 4;
    String macOsPath = "[ADD YOUR VALID FILE PATH]";
    File file = new File(macOsPath);
    FileInputStream inputStream = new FileInputStream(file);

    ChunkedUploadProvider<File> uploadProvider = 
        new ChunkedUploadProvider(session, this.graphServiceClient, 
            inputStream, maxSizeChunk, File.class);

    IProgressCallback<File> callback = new IProgressCallback<File>() {        
        @Override
        public void progress(long l, long l1) {
            log.info("making progress: " + l + ", " + l1);
        }

        @Override
        public void success(File file) {
            log.info("Success! " + file.getName());
        }

        @Override
        public void failure(ClientException e) {
            log.info("Failure! " + e.getMessage());
        }
    };
    uploadProvider.upload(callback, 0);
}

The error occurs when attempting to post the upload session request:

UploadSession session = request.post();

Rather than returning an upload session, the following error is returned:

{
  "error": {
    "code": "invalidRequest",
    "message": "The request is malformed or incorrect.",
    "innerError": {
      "request-id": "b9d4026b-0d8f-44b4-9e9e-8d78a0ea5ea3",
      "date": "2019-05-30T16:20:11"
    }
  }
}

Solution

  • I don't fully understand the reason for requiring DriveItemUploadableProperties but you shouldn't be setting any of its properties.

    Try using this instead:

    UploadSession session = this.graphServiceClient
        .me()
        .drive()
        .root
        .itemWithPath("_hamilton.jpg")
        .createUploadSession(new DriveItemUploadableProperties())
        .buildRequest()
        .post();
    

    I personally find the most useful examples are the unit tests in the SDK itself. In this case, you can find this in OneDriveTests.Java.