From 5ad65b490d1d6feaeb8a8b621f3156af99534d07 Mon Sep 17 00:00:00 2001 From: Victor Vazquez Date: Fri, 6 Sep 2019 18:11:46 +0000 Subject: [PATCH] adding code snippets for BlobService Sync and Async clients --- .../storage/blob/BlobServiceAsyncClient.java | 73 +++++- .../azure/storage/blob/BlobServiceClient.java | 62 ++++- ...ServiceAsyncClientJavaDocCodeSnippets.java | 224 +++++++++++++++++ .../BlobServiceClientJavaDocCodeSnippets.java | 237 +++++++++++++++++- 4 files changed, 589 insertions(+), 7 deletions(-) diff --git a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobServiceAsyncClient.java b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobServiceAsyncClient.java index 035c68b97b73..05c56985cb92 100644 --- a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobServiceAsyncClient.java +++ b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobServiceAsyncClient.java @@ -83,6 +83,10 @@ public final class BlobServiceAsyncClient { * create a container. It simply constructs the URL to the container and offers access to methods relevant to * containers. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getContainerAsyncClient#String} + * * @param containerName The name of the container to point to. * @return A {@link ContainerAsyncClient} object pointing to the specified container */ @@ -98,6 +102,10 @@ public ContainerAsyncClient getContainerAsyncClient(String containerName) { * fails. For more information, see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.createContainer#String} + * * @param containerName Name of the container to create * @return A {@link Mono} containing a {@link ContainerAsyncClient} used to interact with the container created. */ @@ -110,6 +118,10 @@ public Mono createContainer(String containerName) { * fails. For more information, see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.createContainerWithResponse#String-Metadata-PublicAccessType} + * * @param containerName Name of the container to create * @param metadata {@link Metadata} * @param accessType Specifies how the data in this container is available to the public. See the @@ -132,6 +144,9 @@ Mono> createContainerWithResponse(String containe * Deletes the specified container in the storage account. If the container doesn't exist the operation fails. For * more information see the Azure * Docs. + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.deleteContainer#String} * * @param containerName Name of the container to delete * @return A {@link Mono} containing containing status code and HTTP headers @@ -145,6 +160,10 @@ public Mono deleteContainer(String containerName) { * more information see the Azure * Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.deleteContainerWithResponse#String-Context} + * * @param containerName Name of the container to delete * @return A {@link Mono} containing containing status code and HTTP headers */ @@ -174,6 +193,10 @@ public URL getAccountUrl() { * Returns a reactive Publisher emitting all the containers in this account lazily as needed. For more information, * see the Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.listContainers} + * * @return A reactive response emitting the list of containers. */ public PagedFlux listContainers() { @@ -184,6 +207,10 @@ public PagedFlux listContainers() { * Returns a reactive Publisher emitting all the containers in this account lazily as needed. For more information, * see the Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.listContainers#ListContainersOptions} + * * @param options A {@link ListContainersOptions} which specifies what data should be returned by the service. * @return A reactive response emitting the list of containers. */ @@ -251,6 +278,10 @@ private Mono listContainersSegment(String * Gets the properties of a storage account’s Blob service. For more information, see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getProperties} + * * @return A reactive response containing the storage account properties. */ public Mono getProperties() { @@ -261,6 +292,10 @@ public Mono getProperties() { * Gets the properties of a storage account’s Blob service. For more information, see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getPropertiesWithResponse} + * * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} contains the storage * account properties. */ @@ -280,11 +315,15 @@ Mono> getPropertiesWithResponse(Context conte * Note that setting the default service version has no effect when using this client because this client explicitly * sets the version header on each request, overriding the default. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.setProperties#StorageServiceProperties} + * * @param properties Configures the service. * @return A {@link Mono} containing the storage account properties. */ public Mono setProperties(StorageServiceProperties properties) { - return setPropertiesWithReponse(properties).flatMap(FluxUtil::toMono); + return setPropertiesWithResponse(properties).flatMap(FluxUtil::toMono); } /** @@ -292,15 +331,18 @@ public Mono setProperties(StorageServiceProperties properties) { * Azure Docs. * Note that setting the default service version has no effect when using this client because this client explicitly * sets the version header on each request, overriding the default. + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.setPropertiesWithResponse#StorageServiceProperties} * * @param properties Configures the service. * @return A {@link Mono} containing the storage account properties. */ - public Mono setPropertiesWithReponse(StorageServiceProperties properties) { - return withContext(context -> setPropertiesWithReponse(properties, context)); + public Mono setPropertiesWithResponse(StorageServiceProperties properties) { + return withContext(context -> setPropertiesWithResponse(properties, context)); } - Mono setPropertiesWithReponse(StorageServiceProperties properties, Context context) { + Mono setPropertiesWithResponse(StorageServiceProperties properties, Context context) { return postProcessResponse( this.azureBlobStorage.services().setPropertiesWithRestResponseAsync(properties, null, null, context)) .map(VoidResponse::new); @@ -310,6 +352,10 @@ Mono setPropertiesWithReponse(StorageServiceProperties properties, * Gets a user delegation key for use with this account's blob storage. Note: This method call is only valid when * using {@link TokenCredential} in this object's {@link HttpPipeline}. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getUserDelegationKey#OffsetDateTime-OffsetDateTime} + * * @param start Start time for the key's validity. Null indicates immediate start. * @param expiry Expiration of the key's validity. * @return A {@link Mono} containing the user delegation key. @@ -323,6 +369,10 @@ public Mono getUserDelegationKey(OffsetDateTime start, Offset * Gets a user delegation key for use with this account's blob storage. Note: This method call is only valid when * using {@link TokenCredential} in this object's {@link HttpPipeline}. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getUserDelegationKeyWithResponse#OffsetDateTime-OffsetDateTime} + * * @param start Start time for the key's validity. Null indicates immediate start. * @param expiry Expiration of the key's validity. * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} containing the user @@ -354,6 +404,10 @@ Mono> getUserDelegationKeyWithResponse(OffsetDateTim * the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getStatistics} + * * @return A {@link Mono} containing the storage account statistics. */ public Mono getStatistics() { @@ -366,6 +420,10 @@ public Mono getStatistics() { * the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getStatisticsWithResponse} + * * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} containing the storage * account statistics. */ @@ -382,6 +440,9 @@ Mono> getStatisticsWithResponse(Context context) { /** * Returns the sku name and account kind for the account. For more information, please see the * Azure Docs. + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getAccountInfo} * * @return A {@link Mono} containing containing the storage account info. */ @@ -393,6 +454,10 @@ public Mono getAccountInfo() { * Returns the sku name and account kind for the account. For more information, please see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceAsyncClient.getAccountInfoWithResponse} + * * @return A {@link Mono} containing a {@link Response} whose {@link Response#value() value} the storage account * info. */ diff --git a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobServiceClient.java b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobServiceClient.java index a829f552342b..017bbd250488 100644 --- a/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobServiceClient.java +++ b/sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/BlobServiceClient.java @@ -59,6 +59,10 @@ public final class BlobServiceClient { * Initializes a {@link ContainerClient} object pointing to the specified container. This method does not create a * container. It simply constructs the URL to the container and offers access to methods relevant to containers. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.getContainerClient#String} + * * @param containerName The name of the container to point to. * @return A {@link ContainerClient} object pointing to the specified container */ @@ -71,6 +75,10 @@ public ContainerClient getContainerClient(String containerName) { * fails. For more information, see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.createContainer#String} + * * @param containerName Name of the container to create * @return The {@link ContainerClient} used to interact with the container created. */ @@ -83,6 +91,10 @@ public ContainerClient createContainer(String containerName) { * fails. For more information, see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.createContainerWithResponse#String-Metadata-PublicAccessType-Context} + * * @param containerName Name of the container to create * @param metadata {@link Metadata} * @param accessType Specifies how the data in this container is available to the public. See the @@ -100,6 +112,10 @@ public Response createContainerWithResponse(String containerNam * Deletes the specified container in the storage account. If the container doesn't exist the operation fails. For * more information see the Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.deleteContainer#String} + * * @param containerName Name of the container to delete */ public void deleteContainer(String containerName) { @@ -132,6 +148,10 @@ public URL getAccountUrl() { * while new items are automatically retrieved as needed. For more information, see the Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.listContainers} + * * @return The list of containers. */ public PagedIterable listContainers() { @@ -143,6 +163,10 @@ public PagedIterable listContainers() { * while new items are automatically retrieved as needed. For more information, see the Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.listContainers#ListContainersOptions-Duration} + * * @param options A {@link ListContainersOptions} which specifies what data should be returned by the service. * @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised. * @return The list of containers. @@ -155,6 +179,10 @@ public PagedIterable listContainers(ListContainersOptions options * Gets the properties of a storage account’s Blob service. For more information, see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.getProperties} + * * @return The storage account properties. */ public StorageServiceProperties getProperties() { @@ -165,6 +193,10 @@ public StorageServiceProperties getProperties() { * Gets the properties of a storage account’s Blob service. For more information, see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.getPropertiesWithResponse#Duration-Context} + * * @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised. * @param context Additional context that is passed through the Http pipeline during the service call. * @return A {@link Response} whose {@link Response#value() value} contains the storage account properties. @@ -182,6 +214,10 @@ public Response getPropertiesWithResponse(Duration tim * Note that setting the default service version has no effect when using this client because this client explicitly * sets the version header on each request, overriding the default. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.setProperties#StorageServiceProperties} + * * @param properties Configures the service. */ public void setProperties(StorageServiceProperties properties) { @@ -194,13 +230,17 @@ public void setProperties(StorageServiceProperties properties) { * Note that setting the default service version has no effect when using this client because this client explicitly * sets the version header on each request, overriding the default. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.setPropertiesWithResponse#StorageServiceProperties-Duration-Context} + * * @param properties Configures the service. * @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised. * @param context Additional context that is passed through the Http pipeline during the service call. * @return The storage account properties. */ public VoidResponse setPropertiesWithResponse(StorageServiceProperties properties, Duration timeout, Context context) { - Mono response = blobServiceAsyncClient.setPropertiesWithReponse(properties, context); + Mono response = blobServiceAsyncClient.setPropertiesWithResponse(properties, context); return Utility.blockWithOptionalTimeout(response, timeout); } @@ -209,6 +249,10 @@ public VoidResponse setPropertiesWithResponse(StorageServiceProperties propertie * Gets a user delegation key for use with this account's blob storage. Note: This method call is only valid when * using {@link TokenCredential} in this object's {@link HttpPipeline}. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.getUserDelegationKey#OffsetDateTime-OffsetDateTime} + * * @param start Start time for the key's validity. Null indicates immediate start. * @param expiry Expiration of the key's validity. * @return The user delegation key. @@ -221,6 +265,10 @@ public UserDelegationKey getUserDelegationKey(OffsetDateTime start, OffsetDateTi * Gets a user delegation key for use with this account's blob storage. Note: This method call is only valid when * using {@link TokenCredential} in this object's {@link HttpPipeline}. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.getUserDelegationKeyWithResponse#OffsetDateTime-OffsetDateTime-Duration-Context} + * * @param start Start time for the key's validity. Null indicates immediate start. * @param expiry Expiration of the key's validity. * @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised. @@ -240,6 +288,10 @@ public Response getUserDelegationKeyWithResponse(OffsetDateTi * the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.getStatistics} + * * @return The storage account statistics. */ public StorageServiceStats getStatistics() { @@ -252,6 +304,10 @@ public StorageServiceStats getStatistics() { * the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.getStatisticsWithResponse#Duration-Context} + * * @param timeout An optional timeout value beyond which a {@link RuntimeException} will be raised. * @param context Additional context that is passed through the Http pipeline during the service call. * @return A {@link Response} whose {@link Response#value() value} the storage account statistics. @@ -266,6 +322,10 @@ public Response getStatisticsWithResponse(Duration timeout, * Returns the sku name and account kind for the account. For more information, please see the * Azure Docs. * + *

Code Samples

+ * + * {@codesnippet com.azure.storage.blob.BlobServiceClient.getAccountInfo} + * * @return The storage account info. */ public StorageAccountInfo getAccountInfo() { diff --git a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobServiceAsyncClientJavaDocCodeSnippets.java b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobServiceAsyncClientJavaDocCodeSnippets.java index 13ddb70ba97a..027aaac12d8b 100644 --- a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobServiceAsyncClientJavaDocCodeSnippets.java +++ b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobServiceAsyncClientJavaDocCodeSnippets.java @@ -3,15 +3,30 @@ package com.azure.storage.blob; +import com.azure.core.util.Context; + import com.azure.storage.common.AccountSASPermission; import com.azure.storage.common.AccountSASResourceType; import com.azure.storage.common.AccountSASService; import com.azure.storage.common.Constants; import com.azure.storage.common.IPRange; import com.azure.storage.common.SASProtocol; +import com.azure.storage.blob.models.ContainerListDetails; +import com.azure.storage.blob.models.ListContainersOptions; +import com.azure.storage.blob.models.Logging; +import com.azure.storage.blob.models.Metadata; +import com.azure.storage.blob.models.Metrics; +import com.azure.storage.blob.models.PublicAccessType; +import com.azure.storage.blob.models.RetentionPolicy; +import com.azure.storage.blob.models.StorageServiceProperties; +import java.util.Collections; import java.time.OffsetDateTime; +/** + * Code snippets for {@link BlobServiceAsyncClient} + */ +@SuppressWarnings({"unused"}) public class BlobServiceAsyncClientJavaDocCodeSnippets { private BlobServiceAsyncClient client = JavaDocCodeSnippetsHelpers.getBlobServiceAsyncClient(); @@ -53,4 +68,213 @@ public void generateAccountSAS() { // END: com.azure.storage.blob.blobServiceAsyncClient.generateAccountSAS#AccountSASService-AccountSASResourceType-AccountSASPermission-OffsetDateTime-OffsetDateTime-String-IPRange-SASProtocol } + /** + * Code snippet for {@link BlobServiceAsyncClient#getContainerAsyncClient(String)} + */ + public void getContainerClient() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getContainerAsyncClient#String + ContainerAsyncClient containerAsyncClient = client.getContainerAsyncClient("containerName"); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getContainerAsyncClient#String + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#createContainer(String)} + */ + public void createContainer() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.createContainer#String + ContainerAsyncClient containerAsyncClient = + client.createContainer("containerName").block(); + // END: com.azure.storage.blob.BlobServiceAsyncClient.createContainer#String + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#createContainerWithResponse(String, Metadata, PublicAccessType)} + */ + public void createContainerWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.createContainerWithResponse#String-Metadata-PublicAccessType + Metadata metadata = new Metadata(Collections.singletonMap("metadata", "value")); + + ContainerAsyncClient containerClient = + client.createContainerWithResponse("containerName", metadata, PublicAccessType.CONTAINER).block().value(); + // END: com.azure.storage.blob.BlobServiceAsyncClient.createContainerWithResponse#String-Metadata-PublicAccessType + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#deleteContainer(String)} + */ + public void deleteContainer() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.deleteContainer#String + client.deleteContainer("containerName").subscribe( + response -> System.out.printf("Delete container completed%n"), + error -> System.out.printf("Delete container failed: %s%n", error)); + // END: com.azure.storage.blob.BlobServiceAsyncClient.deleteContainer#String + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#deleteContainerWithResponse(String, Context)} + */ + public void deleteContainerWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.deleteContainerWithResponse#String-Context + Context context = new Context("Key", "Value"); + client.deleteContainerWithResponse("containerName").subscribe(response -> + System.out.printf("Delete container completed with status %d%n", response.statusCode())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.deleteContainerWithResponse#String-Context + } + + /** + * Code snippets for {@link BlobServiceAsyncClient#listContainers()} and + * {@link BlobServiceAsyncClient#listContainers(ListContainersOptions)} + */ + public void listContainers() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.listContainers + client.listContainers().subscribe(container -> System.out.printf("Name: %s%n", container.name())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.listContainers + + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.listContainers#ListContainersOptions + ListContainersOptions options = new ListContainersOptions() + .prefix("containerNamePrefixToMatch") + .details(new ContainerListDetails().metadata(true)); + + client.listContainers(options).subscribe(container -> System.out.printf("Name: %s%n", container.name())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.listContainers#ListContainersOptions + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#getProperties()} + */ + public void getProperties() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getProperties + client.getProperties().subscribe(response -> + System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n", + response.hourMetrics().enabled(), + response.minuteMetrics().enabled())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getProperties + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#getPropertiesWithResponse()} + */ + public void getPropertiesWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getPropertiesWithResponse + client.getPropertiesWithResponse().subscribe(response -> + System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n", + response.value().hourMetrics().enabled(), + response.value().minuteMetrics().enabled())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getPropertiesWithResponse + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#setProperties(StorageServiceProperties)} + */ + public void setProperties() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.setProperties#StorageServiceProperties + RetentionPolicy loggingRetentionPolicy = new RetentionPolicy().enabled(true).days(3); + RetentionPolicy metricsRetentionPolicy = new RetentionPolicy().enabled(true).days(1); + + StorageServiceProperties properties = new StorageServiceProperties() + .logging(new Logging() + .write(true) + .delete(true) + .retentionPolicy(loggingRetentionPolicy)) + .hourMetrics(new Metrics() + .enabled(true) + .retentionPolicy(metricsRetentionPolicy)) + .minuteMetrics(new Metrics() + .enabled(true) + .retentionPolicy(metricsRetentionPolicy)); + + client.setProperties(properties).subscribe( + response -> System.out.printf("Setting properties completed%n"), + error -> System.out.printf("Setting properties failed: %s%n", error)); + // END: com.azure.storage.blob.BlobServiceAsyncClient.setProperties#StorageServiceProperties + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#setPropertiesWithResponse(StorageServiceProperties)} + */ + public void setPropertiesWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.setPropertiesWithResponse#StorageServiceProperties + RetentionPolicy loggingRetentionPolicy = new RetentionPolicy().enabled(true).days(3); + RetentionPolicy metricsRetentionPolicy = new RetentionPolicy().enabled(true).days(1); + + StorageServiceProperties properties = new StorageServiceProperties() + .logging(new Logging() + .write(true) + .delete(true) + .retentionPolicy(loggingRetentionPolicy)) + .hourMetrics(new Metrics() + .enabled(true) + .retentionPolicy(metricsRetentionPolicy)) + .minuteMetrics(new Metrics() + .enabled(true) + .retentionPolicy(metricsRetentionPolicy)); + + client.setPropertiesWithResponse(properties).subscribe(response -> + System.out.printf("Setting properties completed with status %d%n", response.statusCode())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.setPropertiesWithResponse#StorageServiceProperties + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#getUserDelegationKey(OffsetDateTime, OffsetDateTime)} + */ + public void getUserDelegationKey() { + OffsetDateTime delegationKeyStartTime = OffsetDateTime.now(); + OffsetDateTime delegationKeyExpiryTime = OffsetDateTime.now().plusDays(7); + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getUserDelegationKey#OffsetDateTime-OffsetDateTime + client.getUserDelegationKey(delegationKeyStartTime, delegationKeyExpiryTime).subscribe(response -> + System.out.printf("User delegation key: %s%n", response.value())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getUserDelegationKey#OffsetDateTime-OffsetDateTime + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#getUserDelegationKeyWithResponse(OffsetDateTime, OffsetDateTime)} + */ + public void getUserDelegationKeyWithResponse() { + OffsetDateTime delegationKeyStartTime = OffsetDateTime.now(); + OffsetDateTime delegationKeyExpiryTime = OffsetDateTime.now().plusDays(7); + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getUserDelegationKeyWithResponse#OffsetDateTime-OffsetDateTime + client.getUserDelegationKeyWithResponse(delegationKeyStartTime, delegationKeyExpiryTime).subscribe(response -> + System.out.printf("User delegation key: %s%n", response.value().value())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getUserDelegationKeyWithResponse#OffsetDateTime-OffsetDateTime + } + + /** + * Code snippets for {@link BlobServiceAsyncClient#getStatistics()} + */ + public void getStatistics() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getStatistics + client.getStatistics().subscribe(response -> + System.out.printf("Geo-replication status: %s%n", response.geoReplication().status())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getStatistics + } + + /** + * Code snippets for {@link BlobServiceAsyncClient#getStatisticsWithResponse()} + */ + public void getStatisticsWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getStatisticsWithResponse + client.getStatisticsWithResponse().subscribe(response -> + System.out.printf("Geo-replication status: %s%n", response.value().geoReplication().status())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getStatisticsWithResponse + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#getAccountInfo} + */ + public void getAccountInfo() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getAccountInfo + client.getAccountInfo().subscribe(response -> + System.out.printf("Account kind: %s, SKU: %s%n", response.accountKind(), response.skuName())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getAccountInfo + } + + /** + * Code snippet for {@link BlobServiceAsyncClient#getAccountInfoWithResponse} + */ + public void getAccountInfoWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceAsyncClient.getAccountInfoWithResponse + client.getAccountInfoWithResponse().subscribe(response -> + System.out.printf("Account kind: %s, SKU: %s%n", response.value().accountKind(), response.value().skuName())); + // END: com.azure.storage.blob.BlobServiceAsyncClient.getAccountInfoWithResponse + } } diff --git a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobServiceClientJavaDocCodeSnippets.java b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobServiceClientJavaDocCodeSnippets.java index f381c5fd5e98..f97ffa11563f 100644 --- a/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobServiceClientJavaDocCodeSnippets.java +++ b/sdk/storage/azure-storage-blob/src/samples/java/com/azure/storage/blob/BlobServiceClientJavaDocCodeSnippets.java @@ -3,6 +3,8 @@ package com.azure.storage.blob; +import com.azure.core.util.Context; + import com.azure.storage.common.AccountSASPermission; import com.azure.storage.common.AccountSASResourceType; import com.azure.storage.common.AccountSASService; @@ -10,11 +12,27 @@ import com.azure.storage.common.IPRange; import com.azure.storage.common.SASProtocol; +import com.azure.storage.blob.models.ContainerListDetails; +import com.azure.storage.blob.models.ListContainersOptions; +import com.azure.storage.blob.models.Logging; +import com.azure.storage.blob.models.Metadata; +import com.azure.storage.blob.models.Metrics; +import com.azure.storage.blob.models.PublicAccessType; +import com.azure.storage.blob.models.RetentionPolicy; +import com.azure.storage.blob.models.StorageAccountInfo; +import com.azure.storage.blob.models.StorageServiceProperties; + +import java.time.Duration; import java.time.OffsetDateTime; +import java.util.Collections; +/** + * Code snippets for {@link BlobServiceClient} + */ +@SuppressWarnings({"unused"}) public class BlobServiceClientJavaDocCodeSnippets { - - private BlobServiceClient client = JavaDocCodeSnippetsHelpers.getBlobServiceClient(); + private final BlobServiceClient client = JavaDocCodeSnippetsHelpers.getBlobServiceClient(); + private final Duration timeout = Duration.ofSeconds(30); /** * Generates a code sample for using {@link BlobServiceClient#generateAccountSAS(AccountSASService, @@ -53,4 +71,219 @@ public void generateAccountSAS() { // END: com.azure.storage.blob.blobServiceClient.generateAccountSAS#AccountSASService-AccountSASResourceType-AccountSASPermission-OffsetDateTime-OffsetDateTime-String-IPRange-SASProtocol } + /** + * Code snippet for {@link BlobServiceClient#getContainerClient(String)} + */ + public void getContainerClient() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.getContainerClient#String + ContainerClient containerClient = client.getContainerClient("containerName"); + // END: com.azure.storage.blob.BlobServiceClient.getContainerClient#String + } + + /** + * Code snippet for {@link BlobServiceClient#createContainer(String)} + */ + public void createContainer() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.createContainer#String + ContainerClient containerClient = client.createContainer("containerName"); + // END: com.azure.storage.blob.BlobServiceClient.createContainer#String + } + + /** + * Code snippet for {@link BlobServiceClient#createContainerWithResponse(String, Metadata, PublicAccessType, Context)} + */ + public void createContainerWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.createContainerWithResponse#String-Metadata-PublicAccessType-Context + Metadata metadata = new Metadata(Collections.singletonMap("metadata", "value")); + Context context = new Context("Key", "Value"); + + ContainerClient containerClient = client.createContainerWithResponse( + "containerName", + metadata, + PublicAccessType.CONTAINER, + context).value(); + // END: com.azure.storage.blob.BlobServiceClient.createContainerWithResponse#String-Metadata-PublicAccessType-Context + } + + /** + * Code snippet for {@link BlobServiceClient#deleteContainer(String)} + */ + public void deleteContainer() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.deleteContainer#String + try { + client.deleteContainer("container Name"); + System.out.printf("Delete container completed with status %n"); + } catch (UnsupportedOperationException error) { + System.out.printf("Delete container failed: %s%n", error); + } + // END: com.azure.storage.blob.BlobServiceClient.deleteContainer#String + } + + /** + * Code snippet for {@link BlobServiceClient#deleteContainerWithResponse(String, Context)} + */ + public void deleteContainerWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.deleteContainerWithResponse#String-Context + Context context = new Context("Key", "Value"); + System.out.printf("Delete container completed with status %d%n", + client.deleteContainerWithResponse("containerName", context).statusCode()); + // END: com.azure.storage.blob.BlobServiceClient.deleteContainerWithResponse#String-Context + } + + /** + * Code snippets for {@link BlobServiceClient#listContainers()} and + * {@link BlobServiceClient#listContainers(ListContainersOptions, Duration)} + */ + public void listContainers() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.listContainers + client.listContainers().forEach(container -> System.out.printf("Name: %s%n", container.name())); + // END: com.azure.storage.blob.BlobServiceClient.listContainers + + // BEGIN: com.azure.storage.blob.BlobServiceClient.listContainers#ListContainersOptions-Duration + ListContainersOptions options = new ListContainersOptions() + .prefix("containerNamePrefixToMatch") + .details(new ContainerListDetails().metadata(true)); + + client.listContainers(options, timeout).forEach(container -> System.out.printf("Name: %s%n", container.name())); + // END: com.azure.storage.blob.BlobServiceClient.listContainers#ListContainersOptions-Duration + } + + /** + * Code snippet for {@link BlobServiceClient#getProperties()} + */ + public void getProperties() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.getProperties + StorageServiceProperties properties = client.getProperties(); + + System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n", + properties.hourMetrics().enabled(), + properties.minuteMetrics().enabled()); + // END: com.azure.storage.blob.BlobServiceClient.getProperties + } + + /** + * Code snippet for {@link BlobServiceClient#getPropertiesWithResponse(Duration, Context)} + */ + public void getPropertiesWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.getPropertiesWithResponse#Duration-Context + Context context = new Context("Key", "Value"); + StorageServiceProperties properties = client.getPropertiesWithResponse(timeout, context).value(); + + System.out.printf("Hour metrics enabled: %b, Minute metrics enabled: %b%n", + properties.hourMetrics().enabled(), + properties.minuteMetrics().enabled()); + // END: com.azure.storage.blob.BlobServiceClient.getPropertiesWithResponse#Duration-Context + } + + /** + * Code snippet for {@link BlobServiceClient#setProperties(StorageServiceProperties)} + */ + public void setProperties() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.setProperties#StorageServiceProperties + RetentionPolicy loggingRetentionPolicy = new RetentionPolicy().enabled(true).days(3); + RetentionPolicy metricsRetentionPolicy = new RetentionPolicy().enabled(true).days(1); + + StorageServiceProperties properties = new StorageServiceProperties() + .logging(new Logging() + .write(true) + .delete(true) + .retentionPolicy(loggingRetentionPolicy)) + .hourMetrics(new Metrics() + .enabled(true) + .retentionPolicy(metricsRetentionPolicy)) + .minuteMetrics(new Metrics() + .enabled(true) + .retentionPolicy(metricsRetentionPolicy)); + + try { + client.setProperties(properties); + System.out.printf("Setting properties completed%n"); + } catch (UnsupportedOperationException error) { + System.out.printf("Setting properties failed: %s%n", error); + } + // END: com.azure.storage.blob.BlobServiceClient.setProperties#StorageServiceProperties + } + + /** + * Code snippet for {@link BlobServiceClient#setPropertiesWithResponse(StorageServiceProperties, Duration, Context)} + */ + public void setPropertiesWithResponse() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.setPropertiesWithResponse#StorageServiceProperties-Duration-Context + RetentionPolicy loggingRetentionPolicy = new RetentionPolicy().enabled(true).days(3); + RetentionPolicy metricsRetentionPolicy = new RetentionPolicy().enabled(true).days(1); + + StorageServiceProperties properties = new StorageServiceProperties() + .logging(new Logging() + .write(true) + .delete(true) + .retentionPolicy(loggingRetentionPolicy)) + .hourMetrics(new Metrics() + .enabled(true) + .retentionPolicy(metricsRetentionPolicy)) + .minuteMetrics(new Metrics() + .enabled(true) + .retentionPolicy(metricsRetentionPolicy)); + + Context context = new Context("Key", "Value"); + + System.out.printf("Setting properties completed with status %d%n", + client.setPropertiesWithResponse(properties, timeout, context).statusCode()); + // END: com.azure.storage.blob.BlobServiceClient.setPropertiesWithResponse#StorageServiceProperties-Duration-Context + } + + /** + * Code snippets for {@link BlobServiceClient#getUserDelegationKey(OffsetDateTime, OffsetDateTime)} + * and {@link BlobServiceClient#getUserDelegationKeyWithResponse(OffsetDateTime, OffsetDateTime, Duration, Context)} + */ + public void getUserDelegationKey() { + OffsetDateTime delegationKeyStartTime = OffsetDateTime.now(); + OffsetDateTime delegationKeyExpiryTime = OffsetDateTime.now().plusDays(7); + Context context = new Context("Key", "Value"); + // BEGIN: com.azure.storage.blob.BlobServiceClient.getUserDelegationKey#OffsetDateTime-OffsetDateTime + System.out.printf("User delegation key: %s%n", + client.getUserDelegationKey(delegationKeyStartTime, delegationKeyExpiryTime)); + // END: com.azure.storage.blob.BlobServiceClient.getUserDelegationKey#OffsetDateTime-OffsetDateTime + + // BEGIN: com.azure.storage.blob.BlobServiceClient.getUserDelegationKeyWithResponse#OffsetDateTime-OffsetDateTime-Duration-Context + System.out.printf("User delegation key: %s%n", + client.getUserDelegationKeyWithResponse(delegationKeyStartTime, delegationKeyExpiryTime, timeout, context)); + // END: com.azure.storage.blob.BlobServiceClient.getUserDelegationKeyWithResponse#OffsetDateTime-OffsetDateTime-Duration-Context + } + + /** + * Code snippets for {@link BlobServiceClient#getStatistics()} and {@link BlobServiceClient#getStatisticsWithResponse(Duration, Context)} + */ + public void getStatistics() { + Context context = new Context("Key", "Value"); + // BEGIN: com.azure.storage.blob.BlobServiceClient.getStatistics + System.out.printf("Geo-replication status: %s%n", + client.getStatistics().geoReplication().status()); + // END: com.azure.storage.blob.BlobServiceClient.getStatistics + + // BEGIN: com.azure.storage.blob.BlobServiceClient.getStatisticsWithResponse#Duration-Context + System.out.printf("Geo-replication status: %s%n", + client.getStatisticsWithResponse(timeout, context).value().geoReplication().status()); + // END: com.azure.storage.blob.BlobServiceClient.getStatisticsWithResponse#Duration-Context + } + + /** + * Code snippet for {@link BlobServiceClient#getAccountInfo} + */ + public void getAccountInfo() { + // BEGIN: com.azure.storage.blob.BlobServiceClient.getAccountInfo + StorageAccountInfo accountInfo = client.getAccountInfo(); + + System.out.printf("Account kind: %s, SKU: %s%n", accountInfo.accountKind(), accountInfo.skuName()); + // END: com.azure.storage.blob.BlobServiceClient.getAccountInfo + } + + /** + * Code snippet for {@link BlobServiceClient#getAccountInfoWithResponse(Duration, Context)} + */ + public void getAccountInfoWithResponse() { + Context context = new Context("Key", "Value"); + // BEGIN: com.azure.storage.blob.BlobServiceClient.getAccountInfoWithResponse#Duration-Context + StorageAccountInfo accountInfo = client.getAccountInfoWithResponse(timeout, context).value(); + // END: com.azure.storage.blob.BlobServiceClient.getAccountInfoWithResponse#Duration-Context + } }