从GitHub repo下载单个文件有哪些技巧?
我不想要显示原始文件的URL;对于二进制文件,什么都没有。
http://support.github.com/discussions/feature-requests/41-download-single-file
是否有可能将GitHub用作“下载服务器”?
如果我们决定切换到GoogleCode,是否提供了上述功能?
或者开源项目是否有免费托管和VCS?
从GitHub repo下载单个文件有哪些技巧?
我不想要显示原始文件的URL;对于二进制文件,什么都没有。
http://support.github.com/discussions/feature-requests/41-download-single-file
是否有可能将GitHub用作“下载服务器”?
如果我们决定切换到GoogleCode,是否提供了上述功能?
或者开源项目是否有免费托管和VCS?
当前回答
或者试试这个
const https = require('https');
const fs = require('fs');
const DOMAIN = 'raw.githubusercontent.com';
function writeFile(data, fileName) {
fs.appendFile(fileName, data.toString(), err => {
if (err) {
console.log('error in writing file', err);
}
});
}
function EOF(data) {
console.log('EOF');
}
function getFileName(pathToFile) {
var result = pathToFile.split('/');
var splitLength = result.length;
return result[splitLength - 1];
}
function getFile(branchName, username, repoName, ...pathToFile) {
pathToFile.forEach(item => {
const path = `/${username}/${repoName}/${branchName}/${item}`;
const URL = `${DOMAIN}${path}`;
const options = {
hostname: DOMAIN,
path: path
};
var fileName = getFileName(item);
https
.get(options, function(res) {
console.log(res.statusCode);
/* if file not found */
if (res.statusCode === 404) {
console.log('FILE NOT FOUND');
} else {
/* if file found */
res.on('data', data => writeFile(data, fileName));
res.on('end', data => EOF(data));
}
})
.on('error', function(res) {
console.log('error in reading URL');
});
});
}
getFile('master', 'bansalAyush', 'InstagramClone', '.babelrc', 'README.md');
其他回答
如果你碰巧用卷发和萤火虫。。。您可以使用cliget插件,它生成一个curl调用,包括所有身份验证机制(也称cookie)。
因此,右键单击原始按钮cliget->“复制链接的url”,然后将其粘贴到外壳中。即使你必须登录才能看到文件,你也会得到它。
在Mac或Linux上安装jq并使用它从Github中提取文件,如下所示:
curl -H 'Authorization: token <ACCESS_TOKEN>' \
-H "Accept: application/vnd.github.v3+raw" -L \
https://api.github.com/repos/MY-ORG/MY-REPO/contents/MY-FILE-PATH | \
jq -r '.content' | base64 --decode > MY_FILE_NAME.txt
可以在此处找到上述命令的文档。
您应该只使用文件的原始URL进行操作。
例如,下载AFNetworking的README:
curl https://raw.githubusercontent.com/AFNetworking/AFNetworking/master/README.md > ADREADME.md
由于它是公共回购,因此您不需要任何凭据。请注意url的类型:raw.githubusercontent.com/path/to/file
对于使用GitHub Enterprise的用户,您需要按照以下方案构造URL
调用WebRequesthttp://github.mycompany.com/api/v3/repos/my-org/my-repo/contents/myfiles/file.txt-标头@{“Authorization”=“token 8d795936d2c1b2806587719b9b6456bd16549ad8”}
详细信息可在此处找到
http://artisticcheese.blogspot.com/2017/04/how-to-download-individual-files-from.html
根据这一要点,您可以使用wget或cURL:
单击GitHub repo中的文件名。单击“原始”以显示文件目录在浏览器中复制URL。在命令行中,运行以下任一命令:wget—无检查证书—内容处置https://URL-from-step3/卷曲-LJOhttps://URL-from-step3/再一个:在RHEL7.6中,运行以下命令:wget公司https://URL-from-step3/--输出文档=FILE_NAME