2017-12-16 25 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을 사용하기 때문에 403 Forbidden 오류가 발생합니다.이 파일은 1Mo를 초과하지 않는 파일에 대한 결과를 제공합니다. 예를 들어 다음과 같은 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를 사용하는) 전체 파일을 다운로드하지 않고 파일 샤 얻을 다음 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

을 고맙습니다 그것은 공공 repo에 대한 작동하지만 내가 (404) 오류가 발생하는 개인 repos에 대한 가져 오기를 사용하여 나무를 얻을 수 없습니다. 가져 오기가 인증 토큰을 함께 전달하지 않기 때문입니다. 가져 오기를 사용하여 토큰을 전달하거나 토큰이 이미있는 gh 객체의 메서드를 사용하여 트리를 가져 오는 방법이 있습니까? –

+1

@NickFernandez 가져 오기 옵션에서 Authorization 헤더를 추가하여 답변을 업데이트했습니다. –