簡體   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