【问题标题】:Azure function to read file from azure devops repositoryAzure 函数从 azure devops 存储库中读取文件
【发布时间】:2021-12-26 07:25:28
【问题描述】:

我正在寻找我的 azure 函数如何从 azure (devops) 存储库中读取文件的方法。文件内容将在之后的 RESTfull Post 或 Put 请求中使用。存储库中的文件会不时更新。

该过程将从触发 azure 功能的网页手动触发。

最好的方法是什么?

【问题讨论】:

    标签: c# .net azure file


    【解决方案1】:

    如果我理解正确,我认为您正在寻找更新 Azure 存储库中的文件。 请阅读 MSDN 的以下文档以获取有关 Azure repo API 的更多见解。

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

    以下代码示例来自 Microsoft Github repo here

    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;
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-16
      • 2023-03-18
      • 2021-02-21
      • 2019-12-12
      • 2020-11-06
      • 2021-04-30
      • 1970-01-01
      • 2019-12-20
      相关资源
      最近更新 更多