簡體   English   中英

從 azure devops 存儲庫讀取文件的 Azure 函數

[英]Azure function to read file from azure devops repository

我正在尋找一種方法,我的 azure 函數如何從 azure(devops)存儲庫中讀取文件。 文件內容將在之后的 RESTfull Post 或 Put 請求中使用。 存儲庫中的文件會不時更新。

該過程將從觸發 azure 功能的網頁手動觸發。

最好的方法是如何做到這一點?

如果我理解正確,我認為您希望更新 Azure 存儲庫中的文件。 請閱讀以下來自 MSDN 的文檔,以獲取有關 Azure 存儲庫 API 的更多見解。

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/?view=azure-devops-rest-6.0

下面的代碼示例來自此處的Microsoft Github 存儲庫。

namespace Microsoft.Azure.DevOps.ClientSamples.Git
{
    [ClientSample(GitWebApiConstants.AreaName, "items")]
    public class ItemsSample : ClientSample
    {
        [ClientSampleMethod]
        public IEnumerable<GitItem> ListItems()
        {
        VssConnection connection = this.Context.Connection;
        GitHttpClient gitClient = connection.GetClient<GitHttpClient>();

        TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
        GitRepository repo = GitSampleHelpers.FindAnyRepository(this.Context, project.Id);

        List<GitItem> items = gitClient.GetItemsAsync(repo.Id, scopePath: "/", recursionLevel: VersionControlRecursionType.OneLevel).Result;

        Console.WriteLine("project {0}, repo {1}", project.Name, repo.Name);
        foreach(GitItem item in items)
        {
            Console.WriteLine("{0} {1} {2}", item.GitObjectType, item.ObjectId, item.Path);
        }

        return items;            
    }

    [ClientSampleMethod]
    public GitItem GetItem()
    {
        VssConnection connection = this.Context.Connection;
        GitHttpClient gitClient = connection.GetClient<GitHttpClient>();

        TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
        GitRepository repo = GitSampleHelpers.FindAnyRepository(this.Context, project.Id);

        // get a filename we know exists
        string filename = gitClient.GetItemsAsync(repo.Id, scopePath: "/", recursionLevel: VersionControlRecursionType.OneLevel).Result
            .Where(o => o.GitObjectType == GitObjectType.Blob).FirstOrDefault().Path;

        // retrieve the contents of the file
        GitItem item = gitClient.GetItemAsync(repo.Id, filename, includeContent: true).Result;

        Console.WriteLine("File {0} at commit {1} is of length {2}", filename, item.CommitId, item.Content.Length);

        return item;
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM