Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Update file uploads docs #1249

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions doc/files.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ BoxFile.Info newFileInfo = rootFolder.uploadFile(stream, "My File.txt");
stream.close();
```

Note that using `FileInputStream` allows you to read the content of the file only once.
If the first upload attempt fails, the stream will become exhausted,
and request retry with no content might be performed. To retry an upload, you would have to
create a new `FileInputStream` and call `uploadFile()` method again.

If you want the SDK to automatically retry the upload in case of any error, you need to provide an
`InputStream` class that supports the `reset()` operation. For example, you can read all bytes from your file into
a `ByteArrayInputStream` and then use it for the upload method. Be aware that this approach will load the whole file
into memory, so it is not recommended for large files, as it can exhaust easily your memory.

```java
BoxFolder rootFolder = BoxFolder.getRootFolder(api);
InputStream stream = new ByteArrayInputStream(Files.readAllBytes(new File(path).toPath()));
BoxFile.Info newFileInfo = rootFolder.uploadFile(stream, "My File.txt");
stream.close();
```

Upload progress can be tracked by providing the size of the file and a
[`ProgressListener`][progress] to
[`uploadFile(InputStream fileContents, String fileName, long fileSize, ProgressListener progress)`][upload2].
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/box/sdk/BoxFolderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static com.box.sdk.http.ContentType.APPLICATION_JSON_PATCH;
import static com.box.sdk.http.ContentType.APPLICATION_OCTET_STREAM;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.github.tomakehurst.wiremock.stubbing.Scenario.STARTED;
import static java.lang.String.format;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -1255,6 +1256,40 @@ public void testUploadFileWithDescriptionSucceeds() {
assertEquals(fileDescription, file.getDescription());
}

@Test
public void testUploadFileSucceedsAfter500InTheFirstAttempt() throws IOException {
final String folderID = "12345";
final String fileURL = "/2.0/files/content";
final String fileContent = "Test file";
final String fileName = "Test File.txt";
InputStream stream = new ByteArrayInputStream(fileContent.getBytes(StandardCharsets.UTF_8));

String result = TestUtils.getFixture("BoxFile/CreateFileWithDescription201");

wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(fileURL))
.inScenario("Retry Scenario")
.whenScenarioStateIs(STARTED)
.willReturn(WireMock.aResponse()
.withStatus(500))
.willSetStateTo("Retry"));

wireMockRule.stubFor(WireMock.post(WireMock.urlPathEqualTo(fileURL))
.inScenario("Retry Scenario")
.whenScenarioStateIs("Retry")
.willReturn(WireMock.aResponse()
.withHeader("Content-Type", APPLICATION_JSON_PATCH)
.withBody(result)
.withStatus(201))
.willSetStateTo("Success"));

BoxFolder folder = new BoxFolder(this.api, folderID);
BoxFile.Info file = folder.uploadFile(stream, fileName);

WireMock.verify(2, WireMock.postRequestedFor(WireMock.urlEqualTo("/2.0/files/content")));

assertEquals(fileName, file.getName());
}

@Test
public void testGetFolderItemsWithSortAndOffset() {
final String folderID = "12345";
Expand Down
Loading