简体   繁体   English

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

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

I'm getting a 403 error when I try to use Github.js to getSha (and subsequently getBlob) from files larger than ~1MB. 当我尝试从大于~1MB的文件中使用Github.js来获取getSha(以及随后的getBlob)时,我收到403错误。 Is there a limit to the file size? 文件大小是否有限制? The code is below: 代码如下:

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

});

The GitHub API says that it supports blobs up to 100MB in size and I could not find anything about filesize limits in the Github.js docs . GitHub API表示它支持最大100MB的blob,而我在Github.js文档中找不到任何有关filesize限制的内容 Also, the files are from a private Github repo. 此外,这些文件来自私人Github仓库。

It throws a 403 Forbidden error because it uses Github GET contents API which gives results for file not exceding 1Mo. 它抛出403 Forbidden错误,因为它使用Github GET内容API ,它给出的文件结果不超过1Mo。 For instance the following will throw 403 : 例如,以下将抛出403:

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

Using this method using GET tree API, you can get the file sha without downloading the whole file and then use repo.getBlob (which uses Get blob API for file not exceding 100Mo). 使用此方法使用GET树API,您可以在不下载整个文件的情况下获取文件sha,然后使用repo.getBlob (对于不超过100Mo的文件使用Get blob API )。

The following example will get the tree for the parent folder of the specified file (for a file exceding 1Mo) with the GET trees api, filter the specific file by name and then request blob data : 以下示例将使用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