@@ -9,7 +9,7 @@ ms.topic: how-to
99ms.author : franksolomon
1010author : fbsolo-ms1
1111ms.reviewer : ambadal
12- ms.date : 07/24 /2024
12+ ms.date : 11/19 /2024
1313ms.custom : data4ml, devx-track-azurecli
1414# Customer intent: As an experienced data scientist with Python skills, I have data located in external sources outside of Azure. I need to make that data available to the Azure Machine Learning platform, to train my machine learning models.
1515---
@@ -124,13 +124,18 @@ from azure.ai.ml import MLClient
124124from azure.ai.ml.entities import WorkspaceConnection
125125from azure.ai.ml.entities import UsernamePasswordConfiguration
126126
127+ # If using username/password, the name/password values should be url-encoded
128+ import urllib.parse
129+ username = urllib.parse.quote(os.environ[" SNOWFLAKEDB_USERNAME" ], safe = " " )
130+ password = urllib.parse.quote(os.environ[" SNOWFLAKEDB_PASSWORD" ], safe = " " )
131+
127132target= " jdbc:snowflake://<myaccount>.snowflakecomputing.com/?db=<mydb>&warehouse=<mywarehouse>&role=<myrole>"
128133# add the Snowflake account, database, warehouse name and role name here. If no role name provided it will default to PUBLIC
129134name= < my_snowflake_connection> # name of the connection
130135wps_connection = WorkspaceConnection(name = name,
131136type = " snowflake" ,
132137target = target,
133- credentials = UsernamePasswordConfiguration(username = " < username> " , password = " < password> " )
138+ credentials = UsernamePasswordConfiguration(username = username, password = password)
134139)
135140
136141ml_client.connections.create_or_update(workspace_connection = wps_connection)
@@ -301,14 +306,19 @@ from azure.ai.ml import MLClient
301306from azure.ai.ml.entities import WorkspaceConnection
302307from azure.ai.ml.entities import UsernamePasswordConfiguration
303308
309+ # If using username/password, the name/password values should be url-encoded
310+ import urllib.parse
311+ username = urllib.parse.quote(os.environ[" MYSQL_USERNAME" ], safe = " " )
312+ password = urllib.parse.quote(os.environ[" MYSQL_PASSWORD" ], safe = " " )
313+
304314target= " Server=tcp:<myservername>,<port>;Database=<mydatabase>;Trusted_Connection=False;Encrypt=True;Connection Timeout=30"
305315# add the sql servername, port address and database
306316
307317name= < my_sql_connection> # name of the connection
308318wps_connection = WorkspaceConnection(name = name,
309319type = " azure_sql_db" ,
310320target = target,
311- credentials = UsernamePasswordConfiguration(username = " < username> " , password = " < password> " )
321+ credentials = UsernamePasswordConfiguration(username = username, password = password)
312322)
313323
314324ml_client.connections.create_or_update(workspace_connection = wps_connection)
@@ -532,6 +542,11 @@ The following example creates a Python feed connection. A Personal Access Token
532542from azure.ai.ml.entities import WorkspaceConnection
533543from azure.ai.ml.entities import UsernamePasswordConfiguration, PatTokenConfiguration
534544
545+ # If using username/password, the name/password values should be url-encoded
546+ # import urllib.parse
547+ # username = urllib.parse.quote(os.environ["FEED_USERNAME"], safe="")
548+ # password = urllib.parse.quote(os.environ["FEED_PASSWORD"], safe="")
549+
535550
536551name = " my_pfeed_conn"
537552
@@ -541,7 +556,7 @@ wps_connection = WorkspaceConnection(
541556 name = name,
542557 type = " python_feed" ,
543558 target = target,
544- # credentials=UsernamePasswordConfiguration(username="< username>" , password="< password>" ),
559+ # credentials=UsernamePasswordConfiguration(username=username, password=password),
545560 credentials = PatTokenConfiguration(pat = " <PatTokenConfiguration>" ),
546561
547562 # credentials=None
@@ -587,6 +602,10 @@ The following example creates an Azure Container Registry connection. A managed
587602from azure.ai.ml.entities import WorkspaceConnection
588603from azure.ai.ml.entities import UsernamePasswordConfiguration
589604
605+ # If using username/password, the name/password values should be url-encoded
606+ import urllib.parse
607+ username = urllib.parse.quote(os.environ[" REGISTRY_USERNAME" ], safe = " " )
608+ password = urllib.parse.quote(os.environ[" REGISTRY_PASSWORD" ], safe = " " )
590609
591610name = " my_acr_conn"
592611
@@ -596,7 +615,7 @@ wps_connection = WorkspaceConnection(
596615 name = name,
597616 type = " container_registry" ,
598617 target = target,
599- credentials = UsernamePasswordConfiguration(username = " < username> " , password = " < password> " ),
618+ credentials = UsernamePasswordConfiguration(username = username, password = password),
600619)
601620ml_client.connections.create_or_update(workspace_connection = wps_connection)
602621```
@@ -710,16 +729,18 @@ from azure.ai.ml.entities import UsernamePasswordConfiguration
710729from azureml.core.conda_dependencies import CondaDependencies
711730from azure.ai.ml import command
712731
732+ # If using username/password, the name/password values should be url-encoded
733+ import urllib.parse
734+ username = urllib.parse.quote(os.environ[" REGISTRY_USERNAME" ], safe = " " )
735+ password = urllib.parse.quote(os.environ[" REGISTRY_PASSWORD" ], safe = " " )
736+
713737# Enter details of AML workspace
714738subscription_id = " <SUBSCRIPTION_ID>"
715739resource_group = " <RESOURCE_GROUP>"
716740workspace = " <AML_WORKSPACE_NAME>"
717741
718742ml_client = MLClient( DefaultAzureCredential(), subscription_id, resource_group, workspace)
719- # Fetching secrets from env var to secure access, these secrets can be set outside or source code
720- registry_username = os.environ[" REGISTRY_USERNAME" ]
721- registry_password = os.environ[" REGISTRY_PASSWORD" ]
722- credentials = UsernamePasswordConfiguration(username = registry_username, password = registry_password)
743+ credentials = UsernamePasswordConfiguration(username = username, password = password)
723744
724745# Create GenericContainerRegistry workspace connection for a generic registry
725746ws_connection = WorkspaceConnection(name = " <name>" , target = " <target>" , type = " GenericContainerRegistry" , credentials = credentials)
0 commit comments