我无法使用MS Graph Java SDK将文件上传到OneDrive,也找不到任何基于Java的示例。
我在这里找到了一个类似(但尚未回答)的问题:MS Graph Java SDK: how to upload a large file to OneDrive?
但是,与上面提出的问题(基于.NET的示例)相比,我设法将更多的代码拼凑在一起,但是仍然没有成功。
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);
}
尝试发布上传会话请求时发生错误:
UploadSession session = request.post();
返回以下错误,而不是返回上载会话:
{
"error": {
"code": "invalidRequest",
"message": "The request is malformed or incorrect.",
"innerError": {
"request-id": "b9d4026b-0d8f-44b4-9e9e-8d78a0ea5ea3",
"date": "2019-05-30T16:20:11"
}
}
}
答案 0 :(得分:0)
我不完全理解要求DriveItemUploadableProperties
的原因,但是您不应该设置其任何属性。
尝试使用此代替:
UploadSession session = this.graphServiceClient
.me()
.drive()
.root
.itemWithPath("_hamilton.jpg")
.createUploadSession(new DriveItemUploadableProperties())
.buildRequest()
.post();
我个人发现,最有用的示例是SDK本身中的单元测试。在这种情况下,您可以在OneDriveTests.Java
中找到它。