Skip to content

Commit 43d5f13

Browse files
nalingaddisNalin Gaddis
and
Nalin Gaddis
authored
Model Artifact Download Example (Azure#3417)
* DownloadModel example * spacing * whitespace --------- Co-authored-by: Nalin Gaddis <[email protected]>
1 parent c7b8161 commit 43d5f13

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

sdk/dotnet/AzureML-Samples-CSharp/Assets/Model/ModelOperations.cs

+46
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Azure.Core;
2+
using Azure.Identity;
23
using Azure.ResourceManager;
34
using Azure.ResourceManager.MachineLearning;
45
using Azure.ResourceManager.MachineLearning.Models;
56
using Azure.ResourceManager.Resources;
7+
using Azure.Storage.Blobs;
68

79
namespace Azure.MachineLearning.Samples.Assets.Model;
810

@@ -57,4 +59,48 @@ public static async Task<MachineLearningModelVersionResource> GetOrCreateModelVe
5759
}
5860
// </GetOrCreateModelVersionAsync>
5961

62+
/// <summary>
63+
/// Download the model artifact from the workspace datastore.
64+
/// </summary>
65+
// <DownloadLatestModelVersion>
66+
public static async Task DownloadModelVersion(
67+
string subscriptionId,
68+
string resourceGroupName,
69+
string workspaceName,
70+
string modelName,
71+
string version,
72+
string downloadToPath)
73+
{
74+
var cred = new DefaultAzureCredential();
75+
var armClient = new ArmClient(cred);
76+
77+
Console.WriteLine("Getting model version data ...");
78+
var modelId = MachineLearningModelVersionResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, workspaceName, modelName, version);
79+
var modelResult = await armClient.GetMachineLearningModelVersionResource(modelId).GetAsync();
80+
var modelData = modelResult.Value.Data;
81+
Console.WriteLine($"Succeeded on id: {modelData.Id}");
82+
83+
Console.WriteLine("Getting workspace datastore ...");
84+
var datastoreName = "workspaceblobstore";
85+
var datastoreId = MachineLearningDatastoreResource.CreateResourceIdentifier(subscriptionId, resourceGroupName, workspaceName, datastoreName);
86+
var datastoreResult = await armClient.GetMachineLearningDatastoreResource(datastoreId).GetAsync();
87+
var datastoreData = datastoreResult.Value.Data;
88+
Console.WriteLine($"Succeeded on id: {datastoreData.Id}");
89+
Console.WriteLine(datastoreData);
90+
91+
var blobName = modelData.Properties.ModelUri.AbsolutePath.Split("/paths/").Last();
92+
Console.WriteLine($"Model blob name: {blobName}");
93+
94+
var datastoreProperties = (MachineLearningAzureBlobDatastore)datastoreData.Properties;
95+
var storageEndpoint = $"https://{datastoreProperties.AccountName}.blob.core.windows.net/{datastoreProperties.ContainerName}";
96+
Console.WriteLine($"Storage endpoint: {storageEndpoint}");
97+
98+
var modelUri = new Uri($"{storageEndpoint}/{blobName}");
99+
Console.WriteLine($"Downloading model from {modelUri} ...");
100+
101+
var blobClient = new BlobClient(modelUri, cred);
102+
blobClient.DownloadTo(downloadToPath);
103+
Console.WriteLine($"Succeded on downloading model to {downloadToPath}");
104+
}
105+
// </DownloadLatestModelVersion>
60106
}

sdk/dotnet/AzureML-Samples-CSharp/AzureML-Samples-CSharp.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<ItemGroup>
1616
<PackageReference Include="Azure.Identity" Version="1.12.1" />
1717
<PackageReference Include="Azure.ResourceManager.MachineLearning" Version="1.2.1" />
18+
<PackageReference Include="Azure.Storage.Blobs" Version="12.22.1" />
1819
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1920
</ItemGroup>
2021

0 commit comments

Comments
 (0)