繁体   English   中英

来自Github.js getSha的403错误,大小超过1MB的文件

[英]403 error from Github.js getSha for files above ~1MB in size

当我尝试从大于~1MB的文件中使用Github.js来获取getSha(以及随后的getBlob)时,我收到403错误。 文件大小是否有限制? 代码如下:

var gh = new GitHub({
    username: username,
    password: password
});

// get the repo from github
var repo = gh.getRepo('some-username','name-of-repo');

// get promise
repo.getSha('some-branch', 'some-file.json').then(function(result){

  // pass the sha onto getBlob
  return repo.getBlob(result.data.sha);

}).then(function(result){

  do_something_with_blob(result.data);

});

GitHub API表示它支持最大100MB的blob,而我在Github.js文档中找不到任何有关filesize限制的内容 此外,这些文件来自私人Github仓库。

它抛出403 Forbidden错误,因为它使用Github GET内容API ,它给出的文件结果不超过1Mo。 例如,以下将抛出403:

https://api.github.com/repos/bertrandmartel/w230st-osx/contents/CLOVER/tools/Shell64.efi?ref=master

使用此方法使用GET树API,您可以在不下载整个文件的情况下获取文件sha,然后使用repo.getBlob (对于不超过100Mo的文件使用Get blob API )。

以下示例将使用GET树api获取指定文件(对于1Mo的文件除外)的父文件夹的树,按名称过滤特定文件,然后请求blob数据:

const accessToken = 'YOUR_ACCESS_TOKEN';

const gh = new GitHub({
  token: accessToken
});

const username = 'bertrandmartel';
const repoName = 'w230st-osx';
const branchName = 'master';
const filePath = 'CLOVER/tools/Shell64.efi'

var fileName = filePath.split(/(\\|\/)/g).pop();
var fileParent = filePath.substr(0, filePath.lastIndexOf("/"));

var repo = gh.getRepo(username, repoName);

fetch('https://api.github.com/repos/' +
  username + '/' +
  repoName + '/git/trees/' +
  encodeURI(branchName + ':' + fileParent), {
    headers: {
      "Authorization": "token " + accessToken
    }
  }).then(function(response) {
  return response.json();
}).then(function(content) {
  var file = content.tree.filter(entry => entry.path === fileName);

  if (file.length > 0) {
    console.log("get blob for sha " + file[0].sha);
    //now get the blob
    repo.getBlob(file[0].sha).then(function(response) {
      console.log("response size : " + response.data.length);
    });
  } else {
    console.log("file " + fileName + " not found");
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM