|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.storage.blob; |
| 5 | + |
| 6 | +import com.azure.core.util.BinaryData; |
| 7 | +import com.azure.storage.blob.models.ParallelTransferOptions; |
| 8 | +import com.azure.storage.blob.options.BlobParallelUploadOptions; |
| 9 | +import com.azure.storage.blob.specialized.BlockBlobAsyncClient; |
| 10 | +import com.azure.storage.common.StorageSharedKeyCredential; |
| 11 | +import reactor.core.publisher.Flux; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.ByteBuffer; |
| 15 | +import java.nio.charset.Charset; |
| 16 | +import java.util.Locale; |
| 17 | + |
| 18 | +/** |
| 19 | + * This example shows how to use the buffered upload method on BlockBlobAsyncClient with a known length. |
| 20 | + * |
| 21 | + * Note that the use of .block() in the method is only used to enable the sample to run effectively in isolation. It is |
| 22 | + * not recommended for use in async environments. |
| 23 | + */ |
| 24 | +public class BufferedUploadWithKnownLengthExample { |
| 25 | + /** |
| 26 | + * Entry point into the basic examples for Storage blobs. |
| 27 | + * @param args Unused. Arguments to the program. |
| 28 | + * @throws IOException If an I/O error occurs |
| 29 | + * @throws RuntimeException If the downloaded data doesn't match the uploaded data |
| 30 | + */ |
| 31 | + public static void main(String[] args) throws IOException { |
| 32 | + |
| 33 | + /* |
| 34 | + * For more information on this setup, please refer to the BasicExample. |
| 35 | + */ |
| 36 | + String accountName = SampleHelper.getAccountName(); |
| 37 | + String accountKey = SampleHelper.getAccountKey(); |
| 38 | + StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey); |
| 39 | + String endpoint = String.format(Locale.ROOT, "https://%s.blob.core.windows.net", accountName); |
| 40 | + String containerName = "myjavacontainerbuffereduploadlength" + System.currentTimeMillis(); |
| 41 | + BlobServiceAsyncClient storageClient = new BlobServiceClientBuilder().endpoint(endpoint).credential(credential) |
| 42 | + .buildAsyncClient(); |
| 43 | + |
| 44 | + BlobContainerAsyncClient containerClient = storageClient.getBlobContainerAsyncClient(containerName); |
| 45 | + containerClient.create().block(); |
| 46 | + |
| 47 | + uploadSourceBlob(endpoint, credential, containerName); |
| 48 | + BlobAsyncClient blobClient = containerClient.getBlobAsyncClient("HelloWorld.txt"); |
| 49 | + |
| 50 | + |
| 51 | + /* |
| 52 | + sourceData has a network stream as its source and therefore likely does not support multiple subscribers. Even |
| 53 | + if it did support multiple subscribers, it would not produce the same data each time it was subscribed to. While |
| 54 | + we could inspect the http headers for the content-length, let us suppose that this information is unavailable |
| 55 | + at this time. All three of these factors would individually make the use of the standard upload method |
| 56 | + impossible--the first two because retries would not work and the third one because we could not satisfy the |
| 57 | + argument list. |
| 58 | + */ |
| 59 | + Flux<ByteBuffer> sourceData = getSourceBlobClient(endpoint, credential, containerName).downloadStream() |
| 60 | + // Perform transformation with length of 1 GB. |
| 61 | + .map(BufferedUploadWithKnownLengthExample::bufferTransformation); |
| 62 | + |
| 63 | + /* |
| 64 | + Although this upload overload permits the use of such unreliable data sources, with known length we can speed |
| 65 | + up the upload process. A buffer size and maximum concurrency can still be passed in to achieve optimized upload. |
| 66 | + */ |
| 67 | + long length = 10; |
| 68 | + long blockSize = 10 * 1024; |
| 69 | + int maxConcurrency = 5; |
| 70 | + ParallelTransferOptions parallelTransferOptions = new ParallelTransferOptions() |
| 71 | + .setBlockSizeLong(blockSize) |
| 72 | + .setMaxConcurrency(maxConcurrency); |
| 73 | + |
| 74 | + // Since we already know the size of our buffered bytes, we can pass the ByteBuffer and length to the BinaryData. |
| 75 | + // This will internally convert the BinaryData to a Flux<ByteBuffer>, but with known length we can optimize the |
| 76 | + // upload speed. |
| 77 | + // Need to use BinaryData.fromFlux(Flux<ByteBuffer>, Long, Boolean) with bufferContent set to false, this allows |
| 78 | + // us to configure the BinaryData to have a specified length set without the BinaryData being infinitely |
| 79 | + // subscribed to the Flux<ByteBuffer>. |
| 80 | + BinaryData.fromFlux(sourceData, length, false).flatMap(binaryData -> |
| 81 | + blobClient.uploadWithResponse(new BlobParallelUploadOptions(binaryData) |
| 82 | + .setParallelTransferOptions(parallelTransferOptions))); |
| 83 | + } |
| 84 | + |
| 85 | + @SuppressWarnings("cast") |
| 86 | + private static ByteBuffer bufferTransformation(ByteBuffer buffer) { |
| 87 | + // The JDK changed the return type of ByteBuffer#limit between 8 and 9. In 8 and below it returns Buffer, whereas |
| 88 | + // in JDK 9 and later, it returns ByteBuffer. To compile on both, we explicitly cast the returned value to |
| 89 | + // ByteBuffer. |
| 90 | + // See https://bugs-stage.openjdk.java.net/browse/JDK-8062376 |
| 91 | + int length = 10; |
| 92 | + return (ByteBuffer) buffer.limit(length); |
| 93 | + } |
| 94 | + |
| 95 | + private static void uploadSourceBlob(String endpoint, StorageSharedKeyCredential credential, String containerName) { |
| 96 | + getSourceBlobClient(endpoint, credential, containerName) |
| 97 | + .upload(Flux.just(ByteBuffer.wrap("Hello world".getBytes(Charset.defaultCharset()))), "Hello world".length()).block(); |
| 98 | + } |
| 99 | + |
| 100 | + private static BlockBlobAsyncClient getSourceBlobClient(String endpoint, StorageSharedKeyCredential credential, |
| 101 | + String containerName) { |
| 102 | + return new BlobServiceClientBuilder().endpoint(endpoint).credential(credential).buildAsyncClient() |
| 103 | + .getBlobContainerAsyncClient(containerName).getBlobAsyncClient("sourceBlob").getBlockBlobAsyncClient(); |
| 104 | + } |
| 105 | +} |
0 commit comments