Skip to content

Commit 745273f

Browse files
authored
Update logic for extractByteBuffer to handle proper byte extraction for upload operations (Azure#40451)
1 parent 376d523 commit 745273f

File tree

4 files changed

+125
-1
lines changed
  • sdk/storage
    • azure-storage-blob/src/test/java/com/azure/storage/blob
    • azure-storage-common/src/main/java/com/azure/storage/common/implementation
    • azure-storage-file-datalake/src/test/java/com/azure/storage/file/datalake
    • azure-storage-file-share/src/test/java/com/azure/storage/file/share

4 files changed

+125
-1
lines changed

sdk/storage/azure-storage-blob/src/test/java/com/azure/storage/blob/BlobApiTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import java.io.ByteArrayOutputStream;
9797
import java.io.File;
9898
import java.io.IOException;
99+
import java.io.InputStream;
99100
import java.io.UncheckedIOException;
100101
import java.net.MalformedURLException;
101102
import java.net.URL;
@@ -361,6 +362,66 @@ public void uploadStreamAccessTierCold() {
361362
assertEquals(AccessTier.COLD, properties.getAccessTier());
362363
}
363364

365+
@LiveOnly
366+
@Test
367+
public void uploadAndDownloadAndUploadAgain() {
368+
byte[] randomData = getRandomByteArray(20 * Constants.MB);
369+
ByteArrayInputStream input = new ByteArrayInputStream(randomData);
370+
371+
String blobName = generateBlobName();
372+
BlobClient blobClient = cc.getBlobClient(blobName);
373+
374+
ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
375+
.setBlockSizeLong((long) Constants.MB)
376+
.setMaxSingleUploadSizeLong(2L * Constants.MB)
377+
.setMaxConcurrency(5);
378+
BlobParallelUploadOptions parallelUploadOptions = new BlobParallelUploadOptions(input)
379+
.setParallelTransferOptions(parallelTransferOptions);
380+
381+
blobClient.uploadWithResponse(parallelUploadOptions, null, null);
382+
383+
InputStream inputStream = blobClient.openInputStream();
384+
385+
// Upload the downloaded content to a different location
386+
String blobName2 = generateBlobName();
387+
388+
parallelUploadOptions = new BlobParallelUploadOptions(inputStream)
389+
.setParallelTransferOptions(parallelTransferOptions);
390+
391+
BlobClient blobClient2 = cc.getBlobClient(blobName2);
392+
blobClient2.uploadWithResponse(parallelUploadOptions, null, null);
393+
}
394+
395+
@LiveOnly
396+
@Test
397+
public void uploadAndDownloadAndUploadAgainWithSize() {
398+
byte[] randomData = getRandomByteArray(20 * Constants.MB);
399+
ByteArrayInputStream input = new ByteArrayInputStream(randomData);
400+
401+
String blobName = generateBlobName();
402+
BlobClient blobClient = cc.getBlobClient(blobName);
403+
404+
ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
405+
.setBlockSizeLong((long) Constants.MB)
406+
.setMaxSingleUploadSizeLong(2L * Constants.MB)
407+
.setMaxConcurrency(5);
408+
BlobParallelUploadOptions parallelUploadOptions = new BlobParallelUploadOptions(input)
409+
.setParallelTransferOptions(parallelTransferOptions);
410+
411+
blobClient.uploadWithResponse(parallelUploadOptions, null, null);
412+
413+
InputStream inputStream = blobClient.openInputStream();
414+
415+
// Upload the downloaded content to a different location
416+
String blobName2 = generateBlobName();
417+
418+
parallelUploadOptions = new BlobParallelUploadOptions(inputStream, 20 * Constants.MB)
419+
.setParallelTransferOptions(parallelTransferOptions);
420+
421+
BlobClient blobClient2 = cc.getBlobClient(blobName2);
422+
blobClient2.uploadWithResponse(parallelUploadOptions, null, null);
423+
}
424+
364425
@RequiredServiceVersion(clazz = BlobServiceVersion.class, min = "2019-12-12")
365426
@Test
366427
public void downloadAllNull() {

sdk/storage/azure-storage-common/src/main/java/com/azure/storage/common/implementation/UploadUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.azure.storage.common.Utility;
1212
import reactor.core.publisher.Flux;
1313
import reactor.core.publisher.Mono;
14+
import reactor.core.scheduler.Schedulers;
1415

1516
import java.io.IOException;
1617
import java.io.InputStream;
@@ -210,7 +211,7 @@ public static Flux<ByteBuffer> extractByteBuffer(Flux<ByteBuffer> data, Long opt
210211
if (data == null && optionalLength == null) {
211212
// We can only buffer up to max int due to restrictions in ByteBuffer.
212213
int chunkSize = (int) Math.min(Constants.MAX_INPUT_STREAM_CONVERTER_BUFFER_LENGTH, blockSize);
213-
data = FluxUtil.toFluxByteBuffer(dataStream, chunkSize);
214+
data = FluxUtil.toFluxByteBuffer(dataStream, chunkSize).subscribeOn(Schedulers.boundedElastic());
214215
// specified length (legacy requirement): use custom converter. no marking because we buffer anyway.
215216
} else if (data == null) {
216217
// We can only buffer up to max int due to restrictions in ByteBuffer.

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2584,6 +2584,36 @@ private void uploadSmallJson(int numCopies) {
25842584
fc.flush(b.length(), true);
25852585
}
25862586

2587+
@LiveOnly
2588+
@Test
2589+
public void uploadAndDownloadAndUploadAgain() {
2590+
byte[] randomData = getRandomByteArray(20 * Constants.MB);
2591+
ByteArrayInputStream input = new ByteArrayInputStream(randomData);
2592+
2593+
String pathName = generatePathName();
2594+
DataLakeFileClient fileClient = dataLakeFileSystemClient.getFileClient(pathName);
2595+
fileClient.createIfNotExists();
2596+
2597+
ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
2598+
.setBlockSizeLong((long) Constants.MB)
2599+
.setMaxSingleUploadSizeLong(2L * Constants.MB)
2600+
.setMaxConcurrency(5);
2601+
FileParallelUploadOptions parallelUploadOptions = new FileParallelUploadOptions(input)
2602+
.setParallelTransferOptions(parallelTransferOptions);
2603+
2604+
fileClient.uploadWithResponse(parallelUploadOptions, null, null);
2605+
2606+
DataLakeFileOpenInputStreamResult inputStreamResult = fileClient.openInputStream();
2607+
2608+
// Upload the downloaded content to a different location
2609+
String pathName2 = generatePathName();
2610+
2611+
parallelUploadOptions = new FileParallelUploadOptions(inputStreamResult.getInputStream())
2612+
.setParallelTransferOptions(parallelTransferOptions);
2613+
2614+
DataLakeFileClient fileClient2 = dataLakeFileSystemClient.getFileClient(pathName2);
2615+
fileClient2.uploadWithResponse(parallelUploadOptions, null, null);
2616+
}
25872617

25882618
private static byte[] readFromInputStream(InputStream stream, int numBytesToRead) {
25892619
byte[] queryData = new byte[numBytesToRead];

sdk/storage/azure-storage-file-share/src/test/java/com/azure/storage/file/share/FileApiTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,38 @@ public void uploadRangeAndDownloadDataWithArgs() {
509509
assertArrayEquals(DATA.getDefaultBytes(), stream.toByteArray());
510510
}
511511

512+
@LiveOnly
513+
@Test
514+
public void uploadAndDownloadAndUploadAgain() {
515+
byte[] randomData = getRandomByteArray(20 * Constants.MB);
516+
ByteArrayInputStream input = new ByteArrayInputStream(randomData);
517+
518+
String pathName = generatePathName();
519+
ShareFileClient fileClient = shareClient.getFileClient(pathName);
520+
fileClient.create(20 * Constants.MB);
521+
522+
ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions()
523+
.setBlockSizeLong((long) Constants.MB)
524+
.setMaxSingleUploadSizeLong(2L * Constants.MB)
525+
.setMaxConcurrency(5);
526+
ShareFileUploadOptions parallelUploadOptions = new ShareFileUploadOptions(input)
527+
.setParallelTransferOptions(parallelTransferOptions);
528+
529+
fileClient.uploadWithResponse(parallelUploadOptions, null, null);
530+
531+
StorageFileInputStream inputStreamResult = fileClient.openInputStream();
532+
533+
// Upload the downloaded content to a different location
534+
String pathName2 = generatePathName();
535+
536+
parallelUploadOptions = new ShareFileUploadOptions(inputStreamResult)
537+
.setParallelTransferOptions(parallelTransferOptions);
538+
539+
ShareFileClient fileClient2 = shareClient.getFileClient(pathName2);
540+
fileClient2.create(20 * Constants.MB);
541+
fileClient2.uploadWithResponse(parallelUploadOptions, null, null);
542+
}
543+
512544
@Test
513545
public void downloadAllNull() {
514546
primaryFileClient.create(DATA.getDefaultDataSizeLong());

0 commit comments

Comments
 (0)