From 43d5f13bb6f695c69accf1558a619f09950636c7 Mon Sep 17 00:00:00 2001 From: Nalin Gaddis <33502801+nalingaddis@users.noreply.github.com> Date: Fri, 11 Oct 2024 09:11:19 -0700 Subject: [PATCH] Model Artifact Download Example (#3417) * DownloadModel example * spacing * whitespace --------- Co-authored-by: Nalin Gaddis --- .../Assets/Model/ModelOperations.cs | 46 +++++++++++++++++++ .../AzureML-Samples-CSharp.csproj | 1 + 2 files changed, 47 insertions(+) diff --git a/sdk/dotnet/AzureML-Samples-CSharp/Assets/Model/ModelOperations.cs b/sdk/dotnet/AzureML-Samples-CSharp/Assets/Model/ModelOperations.cs index ac5eafc3fd..6d7f0f1807 100644 --- a/sdk/dotnet/AzureML-Samples-CSharp/Assets/Model/ModelOperations.cs +++ b/sdk/dotnet/AzureML-Samples-CSharp/Assets/Model/ModelOperations.cs @@ -1,8 +1,10 @@ using Azure.Core; +using Azure.Identity; using Azure.ResourceManager; using Azure.ResourceManager.MachineLearning; using Azure.ResourceManager.MachineLearning.Models; using Azure.ResourceManager.Resources; +using Azure.Storage.Blobs; namespace Azure.MachineLearning.Samples.Assets.Model; @@ -57,4 +59,48 @@ public static async Task GetOrCreateModelVe } // + /// + /// Download the model artifact from the workspace datastore. + /// + // + public static async Task DownloadModelVersion( + string subscriptionId, + string resourceGroupName, + string workspaceName, + string modelName, + string version, + string downloadToPath) + { + var cred = new DefaultAzureCredential(); + var armClient = new ArmClient(cred); + + Console.WriteLine("Getting model version data ..."); + var modelId = MachineLearningModelVersionResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, workspaceName, modelName, version); + var modelResult = await armClient.GetMachineLearningModelVersionResource(modelId).GetAsync(); + var modelData = modelResult.Value.Data; + Console.WriteLine($"Succeeded on id: {modelData.Id}"); + + Console.WriteLine("Getting workspace datastore ..."); + var datastoreName = "workspaceblobstore"; + var datastoreId = MachineLearningDatastoreResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, workspaceName, datastoreName); + var datastoreResult = await armClient.GetMachineLearningDatastoreResource(datastoreId).GetAsync(); + var datastoreData = datastoreResult.Value.Data; + Console.WriteLine($"Succeeded on id: {datastoreData.Id}"); + Console.WriteLine(datastoreData); + + var blobName = modelData.Properties.ModelUri.AbsolutePath.Split("/paths/").Last(); + Console.WriteLine($"Model blob name: {blobName}"); + + var datastoreProperties = (MachineLearningAzureBlobDatastore)datastoreData.Properties; + var storageEndpoint = $"https://{datastoreProperties.AccountName}.blob.core.windows.net/{datastoreProperties.ContainerName}"; + Console.WriteLine($"Storage endpoint: {storageEndpoint}"); + + var modelUri = new Uri($"{storageEndpoint}/{blobName}"); + Console.WriteLine($"Downloading model from {modelUri} ..."); + + var blobClient = new BlobClient(modelUri, cred); + blobClient.DownloadTo(downloadToPath); + Console.WriteLine($"Succeded on downloading model to {downloadToPath}"); + } + // } \ No newline at end of file diff --git a/sdk/dotnet/AzureML-Samples-CSharp/AzureML-Samples-CSharp.csproj b/sdk/dotnet/AzureML-Samples-CSharp/AzureML-Samples-CSharp.csproj index 80d8b9c79d..bb2d0cd98b 100644 --- a/sdk/dotnet/AzureML-Samples-CSharp/AzureML-Samples-CSharp.csproj +++ b/sdk/dotnet/AzureML-Samples-CSharp/AzureML-Samples-CSharp.csproj @@ -15,6 +15,7 @@ +