diff --git a/sdk/storage/azure-storage-blob/README.md b/sdk/storage/azure-storage-blob/README.md index b93c22288a14..a97a86b01ad3 100644 --- a/sdk/storage/azure-storage-blob/README.md +++ b/sdk/storage/azure-storage-blob/README.md @@ -149,13 +149,14 @@ The following sections provide several code snippets covering some of the most c - [Download a blob to a stream](#download-a-blob-to-a-stream) - [Download a blob to local path](#download-a-blob-to-local-path) - [Enumerate blobs](#enumerate-blobs) +- [Copy a blob](#copy-a-blob) - [Authenticate with Azure Identity](#authenticate-with-azure-identity) ### Create a `BlobServiceClient` Create a `BlobServiceClient` using the [`sasToken`](#get-credentials) generated above. - + ```java BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() .endpoint("") @@ -165,7 +166,7 @@ BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() or - + ```java // Only one "?" is needed here. If the sastoken starts with "?", please removing one "?". BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() @@ -177,14 +178,14 @@ BlobServiceClient blobServiceClient = new BlobServiceClientBuilder() Create a `BlobContainerClient` using a `BlobServiceClient`. - + ```java BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mycontainer"); ``` Create a `BlobContainerClient` from the builder [`sasToken`](#get-credentials) generated above. - + ```java BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() .endpoint("") @@ -195,7 +196,7 @@ BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() or - + ```java // Only one "?" is needed here. If the sastoken starts with "?", please removing one "?". BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() @@ -207,7 +208,7 @@ BlobContainerClient blobContainerClient = new BlobContainerClientBuilder() Create a `BlobClient` using a `BlobContainerClient`. - + ```java BlobClient blobClient = blobContainerClient.getBlobClient("myblob"); ``` @@ -216,7 +217,7 @@ or Create a `BlobClient` from the builder [`sasToken`](#get-credentials) generated above. - + ```java BlobClient blobClient = new BlobClientBuilder() .endpoint("") @@ -228,7 +229,7 @@ BlobClient blobClient = new BlobClientBuilder() or - + ```java // Only one "?" is needed here. If the sastoken starts with "?", please removing one "?". BlobClient blobClient = new BlobClientBuilder() @@ -240,7 +241,7 @@ BlobClient blobClient = new BlobClientBuilder() Create a container using a `BlobServiceClient`. - + ```java blobServiceClient.createBlobContainer("mycontainer"); ``` @@ -249,7 +250,7 @@ or Create a container using a `BlobContainerClient`. - + ```java blobContainerClient.create(); ``` @@ -258,7 +259,7 @@ blobContainerClient.create(); Upload from an `InputStream` to a blob using a `BlockBlobClient` generated from a `BlobContainerClient`. - + ```java BlockBlobClient blockBlobClient = blobContainerClient.getBlobClient("myblockblob").getBlockBlobClient(); String dataSample = "samples"; @@ -273,7 +274,7 @@ try (ByteArrayInputStream dataStream = new ByteArrayInputStream(dataSample.getBy Upload a file to a blob using a `BlobClient` generated from a `BlobContainerClient`. - + ```java BlobClient blobClient = blobContainerClient.getBlobClient("myblockblob"); blobClient.uploadFromFile("local-file.jpg"); @@ -283,7 +284,7 @@ blobClient.uploadFromFile("local-file.jpg"); Download a blob to an `OutputStream` using a `BlobClient`. - + ```java try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { blobClient.download(outputStream); @@ -296,7 +297,7 @@ try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { Download blob to a local file using a `BlobClient`. - + ```java blobClient.downloadToFile("downloaded-file.jpg"); ``` @@ -305,18 +306,36 @@ blobClient.downloadToFile("downloaded-file.jpg"); Enumerating all blobs using a `BlobContainerClient`. - + ```java for (BlobItem blobItem : blobContainerClient.listBlobs()) { System.out.println("This is the blob name: " + blobItem.getName()); } ``` +### Copy a blob + +Copying a blob. Please refer to the javadocs on each of these methods for more information around requirements on the +copy source and its authentication. + + +```java +SyncPoller poller = blobClient.beginCopy("", Duration.ofSeconds(1)); +poller.waitForCompletion(); +``` + +or + + +```java +blobClient.copyFromUrl("url-to-blob"); +``` + ### Authenticate with Azure Identity The [Azure Identity library][identity] provides Azure Active Directory support for authenticating with Azure Storage. - + ```java BlobServiceClient blobStorageClient = new BlobServiceClientBuilder() .endpoint("") diff --git a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobAsyncClientBase.java b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobAsyncClientBase.java index 4457c7b8c47a..d6b339ff1872 100644 --- a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobAsyncClientBase.java +++ b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobAsyncClientBase.java @@ -397,6 +397,11 @@ Mono> existsWithResponse(Context context) { /** * Copies the data at the source URL to a blob. + *

+ * This method triggers a long-running, asynchronous operations. The source may be another blob or an Azure File. If + * the source is in another account, the source must either be public or authenticated with a SAS token. If the + * source is in the same account, the Shared Key authorization on the destination will also be applied to the + * source. The source URL must be URL encoded. * *

Code Samples

* @@ -417,6 +422,11 @@ public PollerFlux beginCopy(String sourceUrl, Duration pollI /** * Copies the data at the source URL to a blob. + *

+ * This method triggers a long-running, asynchronous operations. The source may be another blob or an Azure File. If + * the source is in another account, the source must either be public or authenticated with a SAS token. If the + * source is in the same account, the Shared Key authorization on the destination will also be applied to the + * source. The source URL must be URL encoded. * *

Starting a copy operation

* Starting a copy operation and polling on the responses. @@ -451,6 +461,11 @@ public PollerFlux beginCopy(String sourceUrl, Map + * This method triggers a long-running, asynchronous operations. The source may be another blob or an Azure File. If + * the source is in another account, the source must either be public or authenticated with a SAS token. If the + * source is in the same account, the Shared Key authorization on the destination will also be applied to the + * source. The source URL must be URL encoded. * *

Starting a copy operation

* Starting a copy operation and polling on the responses. @@ -664,6 +679,9 @@ Mono> abortCopyFromUrlWithResponse(String copyId, String leaseId, /** * Copies the data at the source URL to a blob and waits for the copy to complete before returning a response. + *

+ * The source must be a block blob no larger than 256MB. The source must also be either public or have a sas token + * attached. The URL must be URL encoded. * *

Code Samples

* @@ -685,6 +703,9 @@ public Mono copyFromUrl(String copySource) { /** * Copies the data at the source URL to a blob and waits for the copy to complete before returning a response. + *

+ * The source must be a block blob no larger than 256MB. The source must also be either public or have a sas token + * attached. The URL must be URL encoded. * *

Code Samples

* @@ -713,7 +734,10 @@ public Mono> copyFromUrlWithResponse(String copySource, Map + * The source must be a block blob no larger than 256MB. The source must also be either public or have a sas token + * attached. The URL must be URL encoded. + * *

Code Samples

* * {@codesnippet com.azure.storage.blob.specialized.BlobAsyncClientBase.copyFromUrlWithResponse#BlobCopyFromUrlOptions} diff --git a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobClientBase.java b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobClientBase.java index 7be55e86ef6c..7802ddbe83f1 100644 --- a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobClientBase.java +++ b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/specialized/BlobClientBase.java @@ -268,6 +268,11 @@ public Response existsWithResponse(Duration timeout, Context context) { /** * Copies the data at the source URL to a blob. + *

+ * This method triggers a long-running, asynchronous operations. The source may be another blob or an Azure File. If + * the source is in another account, the source must either be public or authenticated with a SAS token. If the + * source is in the same account, the Shared Key authorization on the destination will also be applied to the + * source. The source URL must be URL encoded. * *

Code Samples

* @@ -292,6 +297,11 @@ public SyncPoller beginCopy(String sourceUrl, Duration pollI /** * Copies the data at the source URL to a blob. + *

+ * This method triggers a long-running, asynchronous operations. The source may be another blob or an Azure File. If + * the source is in another account, the source must either be public or authenticated with a SAS token. If the + * source is in the same account, the Shared Key authorization on the destination will also be applied to the + * source. The source URL must be URL encoded. * *

Code Samples

* @@ -324,6 +334,11 @@ public SyncPoller beginCopy(String sourceUrl, Map + * This method triggers a long-running, asynchronous operations. The source may be another blob or an Azure File. If + * the source is in another account, the source must either be public or authenticated with a SAS token. If the + * source is in the same account, the Shared Key authorization on the destination will also be applied to the + * source. The source URL must be URL encoded. * *

Code Samples

* @@ -380,6 +395,9 @@ public Response abortCopyFromUrlWithResponse(String copyId, String leaseId /** * Copies the data at the source URL to a blob and waits for the copy to complete before returning a response. + *

+ * The source must be a block blob no larger than 256MB. The source must also be either public or have a sas token + * attached. The URL must be URL encoded. * *

Code Samples

* @@ -398,6 +416,9 @@ public String copyFromUrl(String copySource) { /** * Copies the data at the source URL to a blob and waits for the copy to complete before returning a response. + *

+ * The source must be a block blob no larger than 256MB. The source must also be either public or have a sas token + * attached. The URL must be URL encoded. * *

Code Samples

* @@ -429,6 +450,9 @@ public Response copyFromUrlWithResponse(String copySource, Map + * The source must be a block blob no larger than 256MB. The source must also be either public or have a sas token + * attached. The URL must be URL encoded. * *

Code Samples

* diff --git a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/ReadmeSamples.java b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/ReadmeSamples.java index d64cefc60475..fa21efe7eb26 100644 --- a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/ReadmeSamples.java +++ b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/ReadmeSamples.java @@ -2,13 +2,16 @@ // Licensed under the MIT License. package com.azure.storage.blob; +import com.azure.core.util.polling.SyncPoller; import com.azure.identity.DefaultAzureCredentialBuilder; +import com.azure.storage.blob.models.BlobCopyInfo; import com.azure.storage.blob.models.BlobItem; import com.azure.storage.blob.specialized.BlockBlobClient; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.time.Duration; /** * WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS @@ -124,5 +127,13 @@ public void authWithIdentity() { .buildClient(); } + public void copyBlob() { + SyncPoller poller = blobClient.beginCopy("", Duration.ofSeconds(1)); + poller.waitForCompletion(); + } + + public void copyBlob2() { + blobClient.copyFromUrl("url-to-blob"); + } }