2017-12-16 32 views
3

Github.jsを使用して〜1MBを超えるファイルからgetSha(以降はgetBlob)を使用しようとすると403エラーが発生します。ファイルサイズには制限がありますか?コードは以下の通りです:Github.jsからの403エラー〜1MB以上のサイズのファイルの場合

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すると、私はGithub.js docsでファイルサイズの制限については何も見つけることができませんでした塊をサポートしていることを述べています。また、ファイルはプライベートGithubリポジトリからのものです。

答えて

3

Github GET contents APIを使用しています。これは、ファイルが1Moを超えていないために結果が得られますので、403 Forbiddenというエラーがスローされます。例えば以下は403がスローされます:GETツリーAPIを使用してthis methodを使用して

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

、あなたは(ファイルが100MoをexcedingないためGet blob APIを使用する)、ファイル全体をダウンロードせずに、ファイルのSHAを取得し、repo.getBlobを使用することができます。

次の例では、GETの木APIで(1Mo鋼をexcedingファイル用)指定されたファイルの親フォルダのツリーを取得し、名前で特定のファイルをフィルタリングして、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"); 
    } 
}); 
+1

ありがとう、それは公共reposのために動作しますが、私は404エラーを取得しているプラ​​イベートレポのための木(フェッチを使用して)を取得することはできません。フェッチは認証トークンを一切通過していないため、私はそれを推測しています。フェッチを使ってトークンを渡すか、すでにトークンを持っているghオブジェクトのメソッドを使ってツリーを取得する方法はありますか? –

+1

@NickFernandez取得オプションでAuthorizationヘッダーを追加して回答を更新しました –

関連する問題