You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#Customer intent: As a Python developer, I want to use the Azure SDK for Python to access my data in Azure App Configuration.
13
13
---
@@ -70,76 +70,6 @@ Add the following key-value to the App Configuration store and leave **Label** a
70
70
> [!NOTE]
71
71
> The code snippets in this example will help you get started with the App Configuration client library for Python. For your application, you should also consider handling exceptions according to your needs. To learn more about exception handling, please refer to our [Python SDK documentation](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/appconfiguration/azure-appconfiguration).
72
72
73
-
## Configure your App Configuration connection string
74
-
75
-
1. Set an environment variable named **AZURE_APPCONFIG_CONNECTION_STRING**, and set it to the connection string of your App Configuration store. At the command line, run the following command:
To run the app locally using the Windows command prompt, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
If you use Windows PowerShell, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
If you use macOS, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
If you use Linux, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
Using the Windows command prompt, restart the command prompt to allow the change to take effect and run the following command:
114
-
115
-
```cmd
116
-
echo %AZURE_APPCONFIG_CONNECTION_STRING%
117
-
```
118
-
119
-
### [PowerShell](#tab/powershell)
120
-
121
-
If you use Windows PowerShell, run the following command:
122
-
123
-
```azurepowershell
124
-
$Env:AZURE_APPCONFIG_CONNECTION_STRING
125
-
```
126
-
127
-
### [macOS](#tab/unix)
128
-
129
-
If you use macOS, run the following command:
130
-
131
-
```console
132
-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
133
-
```
134
-
135
-
### [Linux](#tab/linux)
136
-
137
-
If you use Linux, run the following command:
138
-
139
-
```console
140
-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
141
-
```
142
-
143
73
## Code samples
144
74
145
75
The sample code snippets in this section show you how to perform common operations with the App Configuration client library for Python. Add these code snippets to the `try` block in *app-configuration-example.py* file you created earlier.
@@ -160,21 +90,37 @@ Learn below how to:
160
90
161
91
### Connect to an App Configuration store
162
92
163
-
The following code snippet creates an instance of **AzureAppConfigurationClient** using the connection string stored in your environment variables.
93
+
The following code snippet creates an instance of **AzureAppConfigurationClient**. You can connect to your App Configuration store using Microsoft Entra ID (recommended), or a connection string.
94
+
95
+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
96
+
97
+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
@@ -183,64 +129,63 @@ The following code snippet creates a `ConfigurationSetting` object with `key` an
183
129
This method will throw an exception if you try to add a configuration setting that already exists in your store. If you want to avoid this exception, the [set_configuration_setting](#update-a-configuration-setting) method can be used instead.
The following code snippet retrieves a list of configuration settings. The `key_filter` and `label_filter` arguments can be provided to filter key-values based on `key` and `label` respectively. For more information on filtering, see how to [query configuration settings](./concept-key-value.md#query-key-values).
print("\nRetrieved list of configuration settings:")
148
+
for item in filtered_settings_list:
149
+
print("Key: "+ item.key +", Value: "+ item.value)
204
150
```
205
151
206
152
### Lock a configuration setting
207
153
208
154
The lock status of a key-value in App Configuration is denoted by the `read_only` attribute of the `ConfigurationSetting` object. If `read_only` is `True`, the setting is locked. The `set_read_only` method can be invoked with `read_only=True` argument to lock the configuration setting.
print("\nRead-only status for "+ locked_config_setting.key +": "+str(locked_config_setting.read_only))
213
159
```
214
160
215
161
### Unlock a configuration setting
216
162
217
163
If the `read_only` attribute of a `ConfigurationSetting` is `False`, the setting is unlocked. The `set_read_only` method can be invoked with `read_only=False` argument to unlock the configuration setting.
print("\nRead-only status for "+ unlocked_config_setting.key +": "+str(unlocked_config_setting.read_only))
222
168
```
223
169
224
170
### Update a configuration setting
225
171
226
172
The `set_configuration_setting` method can be used to update an existing setting or create a new setting. The following code snippet changes the value of an existing configuration setting.
227
173
228
174
```python
229
-
added_config_setting.value ="Value has been updated!"
In your console window, navigate to the directory containing the *app-configuration-example.py* file and execute the following Python command to run the app:
249
+
### [Connection string](#tab/connection-string)
301
250
302
-
```console
303
-
python app-configuration-example.py
304
-
```
251
+
```python
252
+
import os
253
+
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
294
+
exceptExceptionas ex:
295
+
print('Exception:')
296
+
print(ex)
330
297
```
331
298
299
+
---
300
+
301
+
1. Configure an environment variable
302
+
303
+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
304
+
305
+
Set the environment variable named **AZURE_APPCONFIG_ENDPOINT** to the endpoint of your App Configuration store found under the *Overview* of your store in the Azure portal.
306
+
307
+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
Set the environment variable named **AZURE_APPCONFIG_CONNECTION_STRING** to the read-only connection string of your App Configuration store found under *Access keys* of your store in the Azure portal.
328
+
329
+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
1. After the environment variable is properly set, in your console window, navigate to the directory containing the *app-configuration-example.py* file and execute the following Python command to run the app:
349
+
350
+
```console
351
+
python app-configuration-example.py
352
+
```
353
+
354
+
You should see the following output:
355
+
356
+
```output
357
+
Azure App Configuration - Python example
358
+
359
+
Retrieved configuration setting:
360
+
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
361
+
362
+
Added configuration setting:
363
+
Key: TestApp:Settings:NewSetting, Value: New setting value
364
+
365
+
Retrieved list of configuration settings:
366
+
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
367
+
Key: TestApp:Settings:NewSetting, Value: New setting value
368
+
369
+
Read-only status for TestApp:Settings:NewSetting: True
370
+
371
+
Read-only status for TestApp:Settings:NewSetting: False
372
+
373
+
Updated configuration setting:
374
+
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
375
+
376
+
Deleted configuration setting:
377
+
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
0 commit comments