对于大小大于1MB的文件,Github.js getShall 403错误 [英] 403 error from Github.js getSha for files above ~1MB in size

查看:220
本文介绍了对于大小大于1MB的文件,Github.js getShall 403错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 Github.js 获取sha时,出现403错误(以及随后的getBlob)大于〜1MB的文件。文件大小是否有限制?代码如下:

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

//从github获取回购
var repo = gh.getRepo('some-username','name-of-repo');
$ b $ //获得承诺
repo.getSha('some-branch','some-file.json')。then(function(result){

//将sha传递给getBlob
返回repo.getBlob(result.data.sha);

))。then(function(result){

do_something_with_blob (result.data);

});

GitHub API表示它支持大小达到100MB的blob,我找不到任何有关文件大小限制在 Github.js文档中。另外,这些文件来自一个私人的Github仓库。

解决方案

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

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



使用此方法使用GET树API,您可以在不下载整个文件的情况下获取文件sha,然后使用 repo.getBlob (它使用获取blob API ,文件不超过100Mo)。 下面的例子将会使用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);
//现在得到blob
repo.getBlob(file [0] .sha).then(function(response){
console.log(response size:+ response.data.length);
});
} else {
console.log(file+ fileName +not found);
}
});


I'm getting a 403 error when I try to use Github.js to getSha (and subsequently getBlob) from files larger than ~1MB. 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. Also, the files are from a private Github repo.

解决方案

It throws a 403 Forbidden error because it uses Github GET contents API which gives results for file not exceding 1Mo. For instance the following will throw 403 :

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

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 :

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

这篇关于对于大小大于1MB的文件,Github.js getShall 403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆