1
1
using Azure . Core ;
2
+ using Azure . Identity ;
2
3
using Azure . ResourceManager ;
3
4
using Azure . ResourceManager . MachineLearning ;
4
5
using Azure . ResourceManager . MachineLearning . Models ;
5
6
using Azure . ResourceManager . Resources ;
7
+ using Azure . Storage . Blobs ;
6
8
7
9
namespace Azure . MachineLearning . Samples . Assets . Model ;
8
10
@@ -57,4 +59,48 @@ public static async Task<MachineLearningModelVersionResource> GetOrCreateModelVe
57
59
}
58
60
// </GetOrCreateModelVersionAsync>
59
61
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>
60
106
}
0 commit comments