isac322 / OneDrive-SDK-java

OneDrive SDK for Java
MIT License
62 stars 20 forks source link
java onedrive onedrive-sdk sdk sdk-java

한국어 (korean) | English

OneDrive API for Java

purse fast, easy to use, intuitive API.

Supported Operation

TODO

Environment

Dependency

These are already included in gradle configuration file 'build.gradle'.

Build

jar files will be located on build/libs after build

Windows

gradlew.bat build

Linux, MacOS

./gradlew build

if gradle is installed in your computer

gradle build

Simple example

You can see little bit more complicated examples in TestCode.java

1. Construct Client object

import com.bhyoo.onedrive.client.Client;

String clientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
String[] scope = {"files.readwrite.all", "offline_access"};
String redirectURL = "http://localhost:8080/";
String clientSecret = "xxxxxxxxxxxxxxxxxxxxxxx";

// with login
Client client = new Client(clientId, scope, redirectURL, clientSecret);
// without login
Client client = new Client(clientId, scope, redirectURL, clientSecret, false);
client.login();

// With tokens provided from outside source 
String accessToken = "<access_token>";
String refreshToken = "<refresh_token>"
String tokenType = "bearer";
long expiresIn = 0;

Client client = new Client(clientId, scope, redirectURL, clientSecret, accessToken, refreshToken, tokenType, expiresIn);

2. Folder, file fetch

import com.bhyoo.onedrive.container.items.DriveItem;
import com.bhyoo.onedrive.container.items.FileItem;
import com.bhyoo.onedrive.container.items.FolderItem;

// assume that Client object is already constructed

// get root directory
FolderItem root = client.getRootDir();

// get folder by ID
FolderItem folder = client.getFolder("XXXXXXXXXXXXXXXX!XXXX");

// get folder by path
FolderItem folder1 = client.getFolder(new PathPointer("/{item-path}"));

// get file by ID
FileItem file = client.getFile("XXXXXXXXXXXXXXXX!XXXX");

// get file by path
FileItem file1 = client.getFile(new PathPointer("/{item-path}/{file-name}"));

// or if you don't know whether ID is file or folder
DriveItem item = client.getItem("XXXXXXXXXXXXXXXX!XXXX");

// or if you don't know whether path is file or folder
DriveItem item1 = client.getItem(new PathPointer("/{item-path}"));

3. get children of a folder

import com.bhyoo.onedrive.container.items.DriveItem;
import com.bhyoo.onedrive.container.items.FileItem;
import com.bhyoo.onedrive.container.items.FolderItem;

// assume that Client object is already constructed
FolderItem root = client.getRootDir();

DriveItem[] children = root.allChildren();
FolderItem[] folderChildren = root.folderChildren();
FileItem[] fileChildren = root.fileChildren();

4. Create folder

import com.bhyoo.onedrive.container.items.FolderItem;
import com.bhyoo.onedrive.container.items.pointer.PathPointer;

// assume that Client object is already constructed

FolderItem root = client.getRootDir();

// create folder by parent folder object
FolderItem newFolder = root.createFolder("test");

// create folder by client with parent folder id
FolderItem newFolder1 = client.createFolder("XXXXXXXXXXXXXXXX!XXXX", "test1");

// create folder by client with parent folder path
FolderItem newFolder2 = client.createFolder(new PathPointer("/"), "test2");

5. Copy folder or file

import com.bhyoo.onedrive.container.items.*;
import com.bhyoo.onedrive.container.items.pointer.*;

// assume that Client object is already constructed

FileItem item = (FileItem) client.getItem("XXXXXXXXXXXXXXXX!XXXX");
FolderItem destination = client.getFolder("XXXXXXXXXXXXXXXX!XXXX");

// direct copy
item.copyTo(destination);
// direct copy with new name
item.copyTo(destination, "newName");

// copy by reference object
item.copyTo(destination.newReference());
// copy by reference object with new name
item.copyTo(destination.newReference(), "newName");

// copy by path string
item.copyTo(destination.getPathPointer());
// copy by path string with new name
item.copyTo(destination.getPathPointer(), "newName");

// copy by id string
item.copyTo(destination.getId());
// copy by id string with new name
item.copyTo(destination.getId(), "newName");

// using `Client`, copy by path
client.copyItem(new PathPointer("/{item-path}"), new IdPointer("XXXXXXXXXXXXXXXX!XXXX"));

6. Download file synchronously

import java.nio.file.Paths;
import com.bhyoo.onedrive.container.items.FileItem;

// assume that Client object is already constructed
FileItem file = client.getFile("XXXXXXXXXXXXXXXX!XXXX");

String path = "/home/isac322/download";

// download by system path string with original file name
file.download(path);

// download by system path string with new name
file.download(path, "newName");

// download by path object with original file name
file.download(Paths.get(path));

// download by path object with new name
file.download(Paths.get(path), "newName");

client.download(new PathPointer("/{item-path}"), Paths.get(path));

7. Download file asynchronously

import java.nio.file.Paths;
import com.bhyoo.container.items.FileItem;
import com.bhyoo.network.async.DownloadFuture;

// assume that Client object is already constructed

FileItem file = client.getFile("XXXXXXXXXXXXXXXX!XXXX");
String path = "/home/isac322/download";

// download by path object with original file name
file.downloadAsync(Paths.get(path));

// download by path object with new name
file.downloadAsync(Paths.get(path), "newName");

DownloadFuture future = client.downloadAsync("{file-id}", Paths.get(path), "newName");

// wait until download is done
future.sync();

8. Move folder or file

import com.bhyoo.onedrive.container.items.FileItem;
import com.bhyoo.onedrive.container.items.FolderItem;
import com.bhyoo.onedrive.container.items.pointer.*;

// assume that Client object is already constructed

FileItem item = client.getFile("XXXXXXXXXXXXXXXX!XXXX");
FolderItem destination = client.getFolder("XXXXXXXXXXXXXXXX!XXXX");

// direct move
item.moveTo(destination);

// move by reference object
item.moveTo(destination.newReference());

// move by path string
item.moveTo(destination.getPathPointer());

// move by id string
item.moveTo(destination.getId());

// using `Client` object, move by folder path
client.moveItem(new PathPointer("/{item-path}"), new IdPointer("XXXXXXXXXXXXXXXX!XXXX"));

9. Update folder or file's metadata & Refresh

import com.bhyoo.onedrive.container.items.DriveItem;

// assume that Client object is already constructed
DriveItem item = client.getItem("XXXXXXXXXXXXXXXX!XXXX");

// change item's name and flush to server.
item.rename("new name");

// change item's description and flush to server.
item.updateDescription("blah blah");

// refresh item's all variable to latest value
item.refresh();

10. Upload file (asynchronously)

import java.nio.file.Path;
import com.bhyoo.onedrive.network.async.UploadFuture;

// assume that Client object is already constructed

UploadFuture future;

// start to upload file
future = client.uploadFile("{remote-folder-id}", Paths.get("local-file-path"));
// wait until uploading is done
future.syncUninterruptibly();

*Item diagram

diagram