当新文件上传到 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?在此先感谢,马可