Skip to content

Commit e4b3efd

Browse files
author
Caitlin Bales
authored
Merge pull request #68 from microsoftgraph/chunked-upload-sample
Functional test/sample code for chunked upload
2 parents 562c7ed + f7a48c3 commit e4b3efd

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/main/java/com/microsoft/graph/concurrency/ChunkedUploadProvider.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,19 @@ public void upload(final List<Option> options,
193193
this.readSoFar += read;
194194
}
195195
}
196+
197+
/**
198+
* Uploads content to remote upload session based on the input stream
199+
*
200+
* @param callback the progress callback invoked during uploading
201+
* @param configs the optional configurations for the upload options. [0] should be the customized chunk
202+
* size and [1] should be the maxRetry for upload retry.
203+
* @throws IOException the IO exception that occurred during upload
204+
*/
205+
public void upload(final IProgressCallback<UploadType> callback,
206+
final int...configs)
207+
throws IOException {
208+
upload(null, callback, configs);
209+
210+
}
196211
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.microsoft.graph.functional;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Ignore;
9+
import org.junit.Test;
10+
11+
import com.microsoft.graph.concurrency.ChunkedUploadProvider;
12+
import com.microsoft.graph.concurrency.IProgressCallback;
13+
import com.microsoft.graph.core.ClientException;
14+
import com.microsoft.graph.models.extensions.DriveItem;
15+
import com.microsoft.graph.models.extensions.DriveItemUploadableProperties;
16+
import com.microsoft.graph.models.extensions.UploadSession;
17+
18+
@Ignore
19+
public class OneDriveTests {
20+
private TestBase testBase;
21+
22+
@Before
23+
public void setUp() {
24+
testBase = new TestBase();
25+
}
26+
27+
/**
28+
* Test large file upload.
29+
* https://github.com/OneDrive/onedrive-sdk-csharp/blob/master/docs/chunked-uploads.md
30+
*
31+
* @throws IOException if the input file is not found
32+
* @throws InterruptedException if the chunked upload fails
33+
*/
34+
@Test
35+
public void testLargeFileUpload() throws IOException, InterruptedException {
36+
String itemId = "01BQHXQL5GQVAGCFJLYRH3EAG2YHGERMQA"; //Test upload folder
37+
38+
//Get resource file from file system
39+
InputStream uploadFile = OneDriveTests.class.getClassLoader().getResourceAsStream("hamilton.jpg");
40+
int fileSize = uploadFile.available();
41+
42+
IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () {
43+
@Override
44+
public void progress(final long current, final long max) {
45+
//Check progress
46+
}
47+
@Override
48+
public void success(final DriveItem result) {
49+
//Handle the successful response
50+
String finishedItemId = result.id;
51+
Assert.assertNotNull(finishedItemId);
52+
}
53+
54+
@Override
55+
public void failure(final ClientException ex) {
56+
//Handle the failed upload
57+
Assert.fail("Upload session failed");
58+
}
59+
};
60+
61+
UploadSession uploadSession = testBase
62+
.graphClient
63+
.me()
64+
.drive()
65+
.items(itemId)
66+
.itemWithPath("_hamilton.jpg")
67+
.createUploadSession(new DriveItemUploadableProperties())
68+
.buildRequest()
69+
.post();
70+
71+
ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>(
72+
uploadSession,
73+
testBase.graphClient,
74+
uploadFile,
75+
fileSize,
76+
DriveItem.class);
77+
78+
chunkedUploadProvider.upload(callback);
79+
}
80+
81+
}

0 commit comments

Comments
 (0)