1

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"
    }
  }
}
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
etovian
  • 23
  • 7

1 Answers1

1

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.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • 1
    Nailed it! Thanks so much for the help. I'll be sure to have a better look at the tests next time I'm stumped. Thanks again! – etovian Jun 03 '19 at 22:28
  • Do note that OneDriveTest.java is in @Ignored state since "2018-04-25 11:05:32" and it does no work with me either. Still looking for a better/working solution. – gkephorus Sep 29 '20 at 15:58