Skip to content

Commit

Permalink
Adding support for uploading empty 0 byte files through DataLakeFileC…
Browse files Browse the repository at this point in the history
…lient.uploadFromFile() (#37863)
  • Loading branch information
ibrahimrabab committed Dec 4, 2023
1 parent 51f8804 commit 6ca8c1f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions sdk/storage/azure-storage-file-datalake/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Breaking Changes

### Bugs Fixed
- Fixed a bug that did not allow uploading an empty 0 byte file with `DataLakeFileClient.uploadFromFile()`.

### Other Changes

Expand Down
2 changes: 1 addition & 1 deletion sdk/storage/azure-storage-file-datalake/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/storage/azure-storage-file-datalake",
"Tag": "java/storage/azure-storage-file-datalake_ab160ac82f"
"Tag": "java/storage/azure-storage-file-datalake_0fffe56fda"
}
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,8 @@ public Mono<Response<PathInfo>> uploadFromFileWithResponse(String filePath, Para
long fileSize = channel.size();

if (fileSize == 0) {
throw LOGGER.logExceptionAsError(new IllegalArgumentException("Size of the file must be "
+ "greater than 0."));
// if file size is 0, create the file but do not upload data.
return createWithResponse(null, null, headers, metadata, validatedRequestConditions);
}

// By default, if the file is larger than 100 MB chunk it and append it in stages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2377,6 +2377,22 @@ public void uploadFromFileWithResponse(int dataSize, long singleUploadSize, Long
assertEquals(dataSize, fc.getProperties().getFileSize());
}

@Test
public void uploadFromFileEmptyFile() {
File file = getRandomFile(0);
file.deleteOnExit();
createdFiles.add(file);

Response<PathInfo> response = fc.uploadFromFileWithResponse(file.toPath().toString(), null, null, null, null,
null, null);
// uploadFromFileWithResponse will return 200 for a non-empty file, but since we are uploading an empty file,
// it will return 201 since only createWithResponse gets called
assertEquals(201, response.getStatusCode());
assertNotNull(response.getValue().getETag());

assertEquals(0, fc.getProperties().getFileSize());
}

private static void compareListToBuffer(List<ByteBuffer> buffers, ByteBuffer result) {
result.position(0);
for (ByteBuffer buffer : buffers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2844,6 +2844,24 @@ public void uploadFromFileWithResponse(int dataSize, long singleUploadSize, Long
.verifyComplete();
}

@Test
public void uploadFromFileEmptyFile() {
File file = getRandomFile(0);
file.deleteOnExit();
createdFiles.add(file);

StepVerifier.create(fc.uploadFromFileWithResponse(file.toPath().toString(), null, null, null, null))
.assertNext(r -> {
// uploadFromFileWithResponse will return 200 for a non-empty file, but since we are uploading an empty
// file, it will return 201 since only createWithResponse gets called
assertEquals(201, r.getStatusCode());
assertNotNull(r.getValue().getETag());
}).then(() -> StepVerifier.create(fc.getProperties())
.assertNext(r -> assertEquals(0, r.getFileSize()))
.verifyComplete())
.verifyComplete();
}

@EnabledIf("com.azure.storage.file.datalake.DataLakeTestBase#isLiveMode")
@Test
public void asyncBufferedUploadEmpty() {
Expand Down

0 comments on commit 6ca8c1f

Please sign in to comment.