-1

当新文件上传到 blobstorage 时,我在 Azure 函数上使用以下代码将文件推送到 github 存储库,这会触发该函数。但是如果在短时间内将多个文件上传到blobstorage,则不起作用:仅将一个随机文件推送到github,然后函数抛出异常;在日志中: 描述:由于未处理的异常,进程被终止。异常信息:Octokit.ApiValidationException:无法更新参考 {"message":"Reference cannot be updated","documentation_url":"https://docs.github.com/rest/reference/git..."} 这是编码:

  public static async void PushToGithub(string fileName, Stream myBlob)
    {
        // github variables
        var owner = GITHUB_USER;
        var repo = GITHUB_REPO;
        var token = GITHUB_TOKEN;

        //Create our Client
        var github = new GitHubClient(new ProductHeaderValue("GithubCommit"));
        var tokenAuth = new Credentials(token);

        github.Credentials = tokenAuth;

        var headMasterRef = "heads/master";

        // Get reference of master branch
        var masterReference = await github.Git.Reference.Get(owner, repo, headMasterRef);
        // Get the laster commit of this branch
        var latestCommit = await github.Git.Commit.Get(owner, repo, masterReference.Object.Sha);

        // For image, get image content and convert it to base64
        byte[] bytes;
        using (var memoryStream = new MemoryStream())
        {
            myBlob.Position = 0;
            myBlob.CopyTo(memoryStream);
            bytes = memoryStream.ToArray();
        }

        var pdfBase64 = Convert.ToBase64String(bytes);
        // Create blob
        var pdfBlob = new NewBlob { Encoding = EncodingType.Base64, Content = (pdfBase64) };
        var pdfBlobRef = await github.Git.Blob.Create(owner, repo, pdfBlob);

        // Create new Tree
        var nt = new NewTree { BaseTree = latestCommit.Tree.Sha };
        // Add items based on blobs
        nt.Tree.Add(new NewTreeItem { Path = fileName, Mode = "100644", Type = TreeType.Blob, Sha = pdfBlobRef.Sha });

        var newTree = await github.Git.Tree.Create(owner, repo, nt);

        // Create Commit
        var newCommit = new NewCommit("File update " + DateTime.UtcNow, newTree.Sha, masterReference.Object.Sha);
        var commit = await github.Git.Commit.Create(owner, repo, newCommit);

        // Update HEAD with the commit
        await github.Git.Reference.Update(owner, repo, headMasterRef, new ReferenceUpdate(commit.Sha, true));
    }

我该如何解决,以便将所有上传到 blobstorage 的文件正确推送到 github?在此先感谢,马可

4

1 回答 1

1

看看这个官方文档:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=csharp

此外,存储日志是在“尽力而为”的基础上创建的。不能保证所有事件都被捕获。在某些情况下,可能会丢失日志。

如果您需要更快或更可靠的 Blob 处理,请考虑在创建 Blob 时创建队列消息。然后使用队列触发器而不是 blob 触发器来处理 blob。另一种选择是使用事件网格;请参阅教程使用事件网格自动调整上传图像的大小。

如果你专注于处理blob而不关心loose事件,那么你可以使用队列触发器来确保blob被处理,如果你关心loose事件,请使用事件网格。

于 2020-12-07T01:23:41.537 回答