Skip to content

Commit 6ca8c1f

Browse files
authored
Adding support for uploading empty 0 byte files through DataLakeFileClient.uploadFromFile() (#37863)
1 parent 51f8804 commit 6ca8c1f

5 files changed

Lines changed: 38 additions & 3 deletions

File tree

sdk/storage/azure-storage-file-datalake/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking Changes
88

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

1112
### Other Changes
1213

sdk/storage/azure-storage-file-datalake/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "java",
44
"TagPrefix": "java/storage/azure-storage-file-datalake",
5-
"Tag": "java/storage/azure-storage-file-datalake_ab160ac82f"
5+
"Tag": "java/storage/azure-storage-file-datalake_0fffe56fda"
66
}

sdk/storage/azure-storage-file-datalake/src/main/java/com/azure/storage/file/datalake/DataLakeFileAsyncClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,8 +870,8 @@ public Mono<Response<PathInfo>> uploadFromFileWithResponse(String filePath, Para
870870
long fileSize = channel.size();
871871

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

877877
// By default, if the file is larger than 100 MB chunk it and append it in stages.

sdk/storage/azure-storage-file-datalake/src/test/java/com/azure/storage/file/datalake/FileApiTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2377,6 +2377,22 @@ public void uploadFromFileWithResponse(int dataSize, long singleUploadSize, Long
23772377
assertEquals(dataSize, fc.getProperties().getFileSize());
23782378
}
23792379

2380+
@Test
2381+
public void uploadFromFileEmptyFile() {
2382+
File file = getRandomFile(0);
2383+
file.deleteOnExit();
2384+
createdFiles.add(file);
2385+
2386+
Response<PathInfo> response = fc.uploadFromFileWithResponse(file.toPath().toString(), null, null, null, null,
2387+
null, null);
2388+
// uploadFromFileWithResponse will return 200 for a non-empty file, but since we are uploading an empty file,
2389+
// it will return 201 since only createWithResponse gets called
2390+
assertEquals(201, response.getStatusCode());
2391+
assertNotNull(response.getValue().getETag());
2392+
2393+
assertEquals(0, fc.getProperties().getFileSize());
2394+
}
2395+
23802396
private static void compareListToBuffer(List<ByteBuffer> buffers, ByteBuffer result) {
23812397
result.position(0);
23822398
for (ByteBuffer buffer : buffers) {

sdk/storage/azure-storage-file-datalake/src/test/java/com/azure/storage/file/datalake/FileAsyncApiTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,6 +2844,24 @@ public void uploadFromFileWithResponse(int dataSize, long singleUploadSize, Long
28442844
.verifyComplete();
28452845
}
28462846

2847+
@Test
2848+
public void uploadFromFileEmptyFile() {
2849+
File file = getRandomFile(0);
2850+
file.deleteOnExit();
2851+
createdFiles.add(file);
2852+
2853+
StepVerifier.create(fc.uploadFromFileWithResponse(file.toPath().toString(), null, null, null, null))
2854+
.assertNext(r -> {
2855+
// uploadFromFileWithResponse will return 200 for a non-empty file, but since we are uploading an empty
2856+
// file, it will return 201 since only createWithResponse gets called
2857+
assertEquals(201, r.getStatusCode());
2858+
assertNotNull(r.getValue().getETag());
2859+
}).then(() -> StepVerifier.create(fc.getProperties())
2860+
.assertNext(r -> assertEquals(0, r.getFileSize()))
2861+
.verifyComplete())
2862+
.verifyComplete();
2863+
}
2864+
28472865
@EnabledIf("com.azure.storage.file.datalake.DataLakeTestBase#isLiveMode")
28482866
@Test
28492867
public void asyncBufferedUploadEmpty() {

0 commit comments

Comments
 (0)