[Azure] C# 如何控制 Azure DevOps - 將取得檔案公開分享的資訊

2020-12-11

上一篇文章 ( [Azure] C# 如何控制 Azure DevOps - 上面的專案 取得檔案內容跟所有 Repository資訊 ) 簡單的讓你可以取得  Azure 上面 的Respository  以及他的檔案,今天我們要來聊一下,傳上去的 image or .js 檔案,我們在外部如何讀取他呢? 至於能做啥我就不好說了..







Step1. 申請 https://dev.azure.com/ 帳號

Step2. 開一個專案

Step3. 申請自己的的 Personal Access Tokens,請注意最多只有 90 天,所以如果你有 production 要記得要做更換機制。


Step4. 安裝  Microsoft.TeamFoundationServer.Client

網址:  https://www.nuget.org/packages/Microsoft.TeamFoundationServer.Client/16.153.0   


 

接下來就是 C# code 的部分,基本上 簡單的說就是要再去 獨立取一次 item 然後 把他的 url 拿出來在網址上加上 &$format=octetStream 應該就可以了

//Step2 就會拿到網址 var orgUrl = new Uri("https://dev.azure.com/yourporjectname"); //Step3 就會拿到 Personal Access Token var personalAccessToken = "your_personal_access_token"; // Create a connection var connection = new VssConnection(orgUrl, new VssBasicCredential(string.Empty, personalAccessToken)); var githttpClient = connection.GetClient<GitHttpClient>(); var repos = githttpClient.GetRepositoriesAsync().Result; if (repos != null) { foreach (var repo in repos) { // Repo Id 很重要&#65292;之後文章會用到 Content += "==>" + repo.Name + "," + repo.Id + "<br>"; var contents = githttpClient.GetItemsAsync(repo.Id, "/", VersionControlRecursionType.OneLevel, true, true, true, true).Result; //如果不是 folder foreach (var con in contents) { if (!con.IsFolder) { //如果判斷是圖片的話 if (System.IO.Path.GetExtension(con.Path.ToLower()) == ".jpg") { Content += "[IMG] " + " OBJECTID : " + con.ObjectId + " , PATH:" + con.Path + "<br>"; var contentInfo = githttpClient.GetItemAsync(repo.Id, con.Path, "", null, true, true, true).Result; //插入圖片看看效果 //記得結尾要加上 $format=octetStream Content += "<img src="&quot; + contentInfo.Url + &quot;&amp;$format=octetStream" style="width:200px" />"; } } } } }

reference:
https://github.com/microsoft/azure-devops-dotnet-samples


當麻許的碎念筆記 2014 | Donma Hsu Design.