从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?
当前回答
在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
可以在此处找到上述命令的文档。
其他回答
我使用了以下格式,我觉得告知路径很重要。
https://github.com/user/repository/raw/branch/filename
^^^以上内容在我看来并不完整
https://github.com/<user>/<repoROOTname>/blob/master/<path>/<filename>?raw=true
有些人说raw.github.com或raw而不是blob,但第二行对我有用,我希望能帮助其他人。。。
2¢
或者试试这个
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');
要从Github repo下载文件,请使用带有原始文件链接的“curl”命令。
curl https://raw.githubusercontent.com/user/repo/filename --output filename
添加--output选项,后跟新文件名,将原始文件下载到新创建的文件中。
这肯定会奏效的。至少在Chrome中。右键单击“原始”图标->将链接另存为。
有一个名为Enhanced Github的chrome扩展
它将在每个文件的右侧直接添加一个下载按钮。