|
| 1 | +--- |
| 2 | +title: Query for a Blob Storage endpoint using the Azure Storage management library |
| 3 | +titleSuffix: Azure Storage |
| 4 | +description: Learn how to query for a Blob Storage endpoint using the Azure Storage management library. Then use the endpoint to create a BlobServiceClient object to connect to Blob Storage data resources. |
| 5 | +services: storage |
| 6 | +author: pauljewellmsft |
| 7 | +ms.author: pauljewell |
| 8 | + |
| 9 | +ms.service: storage |
| 10 | +ms.topic: how-to |
| 11 | +ms.date: 03/24/2023 |
| 12 | +ms.subservice: blobs |
| 13 | +ms.custom: devguide-csharp, devguide-java, devguide-javascript, devguide-python |
| 14 | +--- |
| 15 | + |
| 16 | +# Query for a Blob Storage endpoint using the Azure Storage management library |
| 17 | + |
| 18 | +A Blob Storage endpoint forms the base address for all objects within a storage account. When you create a storage account, you specify which type of endpoint you want to use. Blob Storage supports two types of endpoints: |
| 19 | + |
| 20 | +- A [standard endpoint](../common/storage-account-overview.md#standard-endpoints) includes the unique storage account name along with a fixed domain name. The format of a standard endpoint is `https://<storage-account>.blob.core.windows.net`. |
| 21 | +- An [Azure DNS zone endpoint (preview)](../common/storage-account-overview.md#azure-dns-zone-endpoints-preview) dynamically selects an Azure DNS zone and assigns it to the storage account when it's created. The format of an Azure DNS Zone endpoint is `https://<storage-account>.z[00-99].blob.storage.azure.net`. |
| 22 | + |
| 23 | +When your application creates a service client object that connects to Blob Storage data resources, you pass a URI referencing the endpoint to the service client constructor. You can construct the URI string manually, or you can query for the service endpoint at runtime using the Azure Storage management library. |
| 24 | + |
| 25 | +The Azure Storage management library provides programmatic access to the [Azure Storage resource provider](/rest/api/storagerp). The resource provider is the Azure Storage implementation of the Azure Resource Manager. The management library enables developers to manage storage accounts and account configuration, as well as configure lifecycle management policies, object replication policies, and immutability policies. |
| 26 | + |
| 27 | +In this article, you learn how to query a Blob Storage endpoint using the Azure Storage management library. Then you use that endpoint to create a `BlobServiceClient` object to connect with Blob Storage data resources. |
| 28 | + |
| 29 | +## Set up your project |
| 30 | + |
| 31 | +To work with the code examples in this article, follow these steps to set up your project. |
| 32 | + |
| 33 | +### Install packages |
| 34 | + |
| 35 | +Install packages to work with the libraries used in this example. |
| 36 | + |
| 37 | +## [.NET](#tab/dotnet) |
| 38 | + |
| 39 | +Install the following packages using `dotnet add package`: |
| 40 | + |
| 41 | +```dotnetcli |
| 42 | +dotnet add package Azure.Identity |
| 43 | +dotnet add package Azure.ResourceManager.Storage |
| 44 | +dotnet add package Azure.Storage.Blobs |
| 45 | +``` |
| 46 | + |
| 47 | +## [Java](#tab/java) |
| 48 | + |
| 49 | +Open the `pom.xml` file in your text editor. |
| 50 | + |
| 51 | +Add **azure-sdk-bom** to take a dependency on the latest version of the library. In the following snippet, replace the `{bom_version_to_target}` placeholder with the version number. Using **azure-sdk-bom** keeps you from having to specify the version of each individual dependency. To learn more about the BOM, see the [Azure SDK BOM README](https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/boms/azure-sdk-bom/README.md). |
| 52 | + |
| 53 | +```xml |
| 54 | +<dependencyManagement> |
| 55 | + <dependencies> |
| 56 | + <dependency> |
| 57 | + <groupId>com.azure</groupId> |
| 58 | + <artifactId>azure-sdk-bom</artifactId> |
| 59 | + <version>{bom_version_to_target}</version> |
| 60 | + <type>pom</type> |
| 61 | + <scope>import</scope> |
| 62 | + </dependency> |
| 63 | + </dependencies> |
| 64 | +</dependencyManagement> |
| 65 | +``` |
| 66 | + |
| 67 | +Then add the following dependency elements to the group of dependencies. The **azure-identity** dependency is needed for passwordless connections to Azure services. |
| 68 | + |
| 69 | +```xml |
| 70 | +<dependency> |
| 71 | + <groupId>com.azure</groupId> |
| 72 | + <artifactId>azure-storage-blob</artifactId> |
| 73 | +</dependency> |
| 74 | +<dependency> |
| 75 | + <groupId>com.azure</groupId> |
| 76 | + <artifactId>azure-identity</artifactId> |
| 77 | +</dependency> |
| 78 | +<dependency> |
| 79 | + <groupId>com.azure.resourcemanager</groupId> |
| 80 | + <artifactId>azure-resourcemanager</artifactId> |
| 81 | + <version>2.24.0</version> |
| 82 | +</dependency> |
| 83 | +<dependency> |
| 84 | + <groupId>com.azure.resourcemanager</groupId> |
| 85 | + <artifactId>azure-resourcemanager-storage</artifactId> |
| 86 | + <version>2.24.0</version> |
| 87 | +</dependency> |
| 88 | +<dependency> |
| 89 | + <groupId>com.azure</groupId> |
| 90 | + <artifactId>azure-core-management</artifactId> |
| 91 | + <version>1.10.2</version> |
| 92 | +</dependency> |
| 93 | +``` |
| 94 | + |
| 95 | +## [JavaScript](#tab/javascript) |
| 96 | + |
| 97 | +Install the following packages using `npm install`: |
| 98 | + |
| 99 | +```console |
| 100 | +npm install @azure/identity |
| 101 | +npm install @azure/storage-blob |
| 102 | +npm install @azure/arm-resources |
| 103 | +npm install @azure/arm-storage |
| 104 | +``` |
| 105 | + |
| 106 | +## [Python](#tab/python) |
| 107 | + |
| 108 | +Install the following packages using `pip install`: |
| 109 | + |
| 110 | +```console |
| 111 | +pip install azure-identity |
| 112 | +pip install azure-storage-blob |
| 113 | +pip install azure-mgmt-resource |
| 114 | +pip install azure-mgmt-storage |
| 115 | +``` |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +### Set up the app code |
| 120 | + |
| 121 | +Add the necessary `using` or `import` directives to the code. Note that the code examples may split out functionality between files, but in this section all directives are listed together. |
| 122 | + |
| 123 | +## [.NET](#tab/dotnet) |
| 124 | + |
| 125 | +Add the following `using` directives: |
| 126 | + |
| 127 | +```csharp |
| 128 | +using Azure.Core; |
| 129 | +using Azure.Identity; |
| 130 | +using Azure.Storage.Blobs; |
| 131 | +using Azure.ResourceManager; |
| 132 | +using Azure.ResourceManager.Resources; |
| 133 | +using Azure.ResourceManager.Storage; |
| 134 | +``` |
| 135 | + |
| 136 | +Client library information: |
| 137 | + |
| 138 | +- [Azure.Identity](/dotnet/api/overview/azure/identity-readme): Provides Azure Active Directory (Azure AD) token authentication support across the Azure SDK, and is needed for passwordless connections to Azure services. |
| 139 | +- [Azure.ResourceManager.Storage](/dotnet/api/overview/azure/resourcemanager.storage-readme): Supports management of Azure Storage resources, including resource groups and storage accounts. |
| 140 | +- [Azure.Storage.Blobs](/dotnet/api/overview/azure/storage.blobs-readme): Contains the primary classes that you can use to work with Blob Storage data resources. |
| 141 | + |
| 142 | +## [Java](#tab/java) |
| 143 | + |
| 144 | +Add the following `import` directives: |
| 145 | + |
| 146 | +```java |
| 147 | +import com.azure.identity.*; |
| 148 | +import com.azure.storage.blob.*; |
| 149 | +import com.azure.resourcemanager.*; |
| 150 | +import com.azure.resourcemanager.storage.models.*; |
| 151 | +import com.azure.core.management.*; |
| 152 | +import com.azure.core.management.profile.*; |
| 153 | +``` |
| 154 | + |
| 155 | +Client library information: |
| 156 | + |
| 157 | +- [com.azure.identity](/java/api/overview/azure/identity-readme): Provides Azure Active Directory (Azure AD) token authentication support across the Azure SDK, and is needed for passwordless connections to Azure services. |
| 158 | +- [com.azure.storage.blob](/java/api/com.azure.storage.blob): Contains the primary classes that you can use to work with Blob Storage data resources. |
| 159 | +- [com.azure.resourcemanager](/java/api/overview/azure/resourcemanager-readme): Supports management of Azure resources and resource groups. |
| 160 | +- [com.azure.resourcemanager.storage](/java/api/overview/azure/resourcemanager-storage-readme): Supports management of Azure Storage resources, including resource groups and storage accounts. |
| 161 | + |
| 162 | +## [JavaScript](#tab/javascript) |
| 163 | + |
| 164 | +Add the following `require` statements to load the modules: |
| 165 | + |
| 166 | +```javascript |
| 167 | +const { DefaultAzureCredential } = require("@azure/identity"); |
| 168 | +const { BlobServiceClient } = require("@azure/storage-blob"); |
| 169 | +const { ResourceManagementClient } = require("@azure/arm-resources"); |
| 170 | +const { StorageManagementClient } = require("@azure/arm-storage"); |
| 171 | +``` |
| 172 | + |
| 173 | +Client library information: |
| 174 | + |
| 175 | +- [@azure/identity](/javascript/api/overview/azure/identity-readme): Provides Azure Active Directory (Azure AD) token authentication support across the Azure SDK, and is needed for passwordless connections to Azure services. |
| 176 | +- [@azure/storage-blob](/javascript/api/overview/azure/storage-blob-readme): Contains the primary classes that you can use to work with Blob Storage data resources. |
| 177 | +- [@azure/arm-resources](/javascript/api/overview/azure/arm-resources-readme): Supports management of Azure resources and resource groups. |
| 178 | +- [@azure/arm-storage](/javascript/api/overview/azure/arm-storage-readme): Supports management of Azure Storage resources, including resource groups and storage accounts. |
| 179 | + |
| 180 | +## [Python](#tab/python) |
| 181 | + |
| 182 | +Add the following `import` statements: |
| 183 | + |
| 184 | +```python |
| 185 | +from azure.identity import DefaultAzureCredential |
| 186 | +from azure.storage.blob import BlobServiceClient |
| 187 | +from azure.mgmt.resource import ResourceManagementClient |
| 188 | +from azure.mgmt.storage import StorageManagementClient |
| 189 | +``` |
| 190 | + |
| 191 | +Client library information: |
| 192 | + |
| 193 | +- [azure-identity](/python/api/overview/azure/identity-readme): Provides Azure Active Directory (Azure AD) token authentication support across the Azure SDK, and is needed for passwordless connections to Azure services. |
| 194 | +- [azure-storage-blob](/python/api/overview/azure/storage-blob-readme): Contains the primary classes that you can use to work with Blob Storage data resources. |
| 195 | +- [azure-mgmt-resource](/python/api/azure-mgmt-resource/azure.mgmt.resource.resourcemanagementclient): Supports management of Azure resources and resource groups. |
| 196 | +- [azure-mgmt-storage](/python/api/azure-mgmt-storage/azure.mgmt.storage.storagemanagementclient): Supports management of Azure Storage resources, including resource groups and storage accounts. |
| 197 | + |
| 198 | +--- |
| 199 | + |
| 200 | +### Register the Storage resource provider with a subscription |
| 201 | + |
| 202 | +A resource provider must be registered with your Azure subscription before you can work with it. This step only needs to be done once per subscription, and only applies if the resource provider **Microsoft.Storage** is not currently registered with your subscription. |
| 203 | + |
| 204 | +You can register the Storage resource provider, or check the registration status, using [Azure portal](/azure/azure-resource-manager/management/resource-providers-and-types#azure-portal), [Azure CLI](/azure/azure-resource-manager/management/resource-providers-and-types#azure-cli), or [Azure PowerShell](/azure/azure-resource-manager/management/resource-providers-and-types#azure-powershell). |
| 205 | + |
| 206 | +You can also use the Azure management libraries to check the registration status and register the Storage resource provider, as shown in the following examples: |
| 207 | + |
| 208 | +## [.NET](#tab/dotnet) |
| 209 | + |
| 210 | +:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobQueryEndpoint/QueryEndpoint.cs" id="Snippet_RegisterSRP"::: |
| 211 | + |
| 212 | +## [Java](#tab/java) |
| 213 | + |
| 214 | +:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-query-endpoint/src/main/java/com/blobs/queryendpoint/AccountProperties.java" id="Snippet_RegisterSRP"::: |
| 215 | + |
| 216 | +## [JavaScript](#tab/javascript) |
| 217 | + |
| 218 | +:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/blob-query-endpoint/index.js" id="Snippet_register_srp"::: |
| 219 | + |
| 220 | +## [Python](#tab/python) |
| 221 | + |
| 222 | +:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-query-endpoint/blob-query-endpoint.py" id="Snippet_register_srp"::: |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +> [!NOTE] |
| 227 | +> To perform the register operation, you'll need permissions for the following Azure RBAC action: **Microsoft.Storage/register/action**. This permission is included in the **Contributor** and **Owner** roles. |
| 228 | +
|
| 229 | +## Query for the Blob Storage endpoint |
| 230 | + |
| 231 | +To retrieve the Blob Storage endpoint for a given storage account, we need to get the storage account properties by calling the [Get Properties](/rest/api/storagerp/storage-accounts/get-properties) operation. The following code samples use both the data access and management libraries to get a Blob Storage endpoint for a specified storage account: |
| 232 | + |
| 233 | +## [.NET](#tab/dotnet) |
| 234 | + |
| 235 | +To get the properties for a specified storage account, use the following method from a [StorageAccountCollection](/dotnet/api/azure.resourcemanager.storage.storageaccountcollection) object: |
| 236 | + |
| 237 | +- [GetAsync](/dotnet/api/azure.resourcemanager.storage.storageaccountcollection.getasync) |
| 238 | + |
| 239 | +This method returns a [StorageAccountResource](/dotnet/api/azure.resourcemanager.storage.storageaccountresource) object, which represents the storage account. |
| 240 | + |
| 241 | +:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobQueryEndpoint/QueryEndpoint.cs" id="Snippet_QueryEndpoint"::: |
| 242 | + |
| 243 | +## [Java](#tab/java) |
| 244 | + |
| 245 | +To get the properties for a specified storage account, use the following method from an [AzureResourceManager](/java/api/com.azure.resourcemanager.azureresourcemanager) object: |
| 246 | + |
| 247 | +- [storageAccounts().getByResourceGroup](/java/api/com.azure.resourcemanager.resources.fluentcore.arm.collection.supportsgettingbyresourcegroup#com-azure-resourcemanager-resources-fluentcore-arm-collection-supportsgettingbyresourcegroup-getbyresourcegroup(java-lang-string-java-lang-string)) |
| 248 | + |
| 249 | +This method returns a [StorageAccount](/java/api/com.azure.resourcemanager.storage.models.storageaccount) interface, which is an immutable client-side representation of the storage account. |
| 250 | + |
| 251 | +:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-query-endpoint/src/main/java/com/blobs/queryendpoint/AccountProperties.java" id="Snippet_QueryEndpoint"::: |
| 252 | + |
| 253 | +## [JavaScript](#tab/javascript) |
| 254 | + |
| 255 | +To get the properties for a specified storage account, use the following method from a [StorageManagementClient](/javascript/api/@azure/arm-storage/storagemanagementclient) object: |
| 256 | + |
| 257 | +- [storageAccounts.getProperties](/javascript/api/@azure/arm-storage/storageaccounts#@azure-arm-storage-storageaccounts-getproperties) |
| 258 | + |
| 259 | +This method returns a [`Promise<StorageAccountsGetPropertiesResponse>`](/javascript/api/@azure/arm-storage/storageaccountsgetpropertiesresponse), which represents the storage account. |
| 260 | + |
| 261 | +:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/blob-query-endpoint/index.js" id="Snippet_query_blob_endpoint"::: |
| 262 | + |
| 263 | +## [Python](#tab/python) |
| 264 | + |
| 265 | +To get the properties for a specified storage account, use the following method from a [StorageManagementClient](/python/api/azure-mgmt-storage/azure.mgmt.storage.storagemanagementclient) object: |
| 266 | + |
| 267 | +- [storageAccounts.getProperties](/python/api/azure-mgmt-storage/azure.mgmt.storage.storagemanagementclient#azure-mgmt-storage-storagemanagementclient-storage-accounts) |
| 268 | + |
| 269 | +This method returns a `StorageAccount` object, which represents the storage account. |
| 270 | + |
| 271 | +:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-query-endpoint/blob-query-endpoint.py" id="Snippet_query_blob_endpoint"::: |
| 272 | + |
| 273 | +--- |
| 274 | + |
| 275 | +## Create a client object using the endpoint |
| 276 | + |
| 277 | +Once you have the Blob Storage endpoint for a storage account, you can instantiate a client object to work with data resources. The following code sample creates a `BlobServiceClient` object using the endpoint we retrieved in the earlier example: |
| 278 | + |
| 279 | +## [.NET](#tab/dotnet) |
| 280 | + |
| 281 | +:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobQueryEndpoint/Program.cs" id="Snippet_CreateClientWithEndpoint"::: |
| 282 | + |
| 283 | +## [Java](#tab/java) |
| 284 | + |
| 285 | +:::code language="java" source="~/azure-storage-snippets/blobs/howto/Java/blob-query-endpoint/src/main/java/com/blobs/queryendpoint/App.java" id="Snippet_CreateClientWithEndpoint"::: |
| 286 | + |
| 287 | +## [JavaScript](#tab/javascript) |
| 288 | + |
| 289 | +:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/blob-query-endpoint/index.js" id="Snippet_create_client_with_endpoint"::: |
| 290 | + |
| 291 | +## [Python](#tab/python) |
| 292 | + |
| 293 | +:::code language="python" source="~/azure-storage-snippets/blobs/howto/python/blob-query-endpoint/blob-query-endpoint.py" id="Snippet_create_client_with_endpoint"::: |
| 294 | + |
| 295 | +--- |
| 296 | + |
| 297 | +## Next steps |
| 298 | + |
| 299 | +View the full code samples (GitHub): |
| 300 | +- [.NET](https://github.com/Azure-Samples/AzureStorageSnippets/tree/master/blobs/howto/dotnet/BlobQueryEndpoint) |
| 301 | +- [Java](https://github.com/Azure-Samples/AzureStorageSnippets/tree/master/blobs/howto/Java/blob-query-endpoint/src/main/java/com/blobs/queryendpoint) |
| 302 | +- [JavaScript](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/JavaScript/blob-query-endpoint/index.js) |
| 303 | +- [Python](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/python/blob-query-endpoint/blob-query-endpoint.py) |
| 304 | + |
| 305 | +To learn more about creating client objects, see [Create and manage client objects that interact with data resources](storage-blob-client-management.md). |
| 306 | + |
| 307 | + |
| 308 | + |
0 commit comments