基本配置
const owner = "ConsoleLZ";
const repo = "githubAPI";
const accessToken = "???";
const branch = 'main'
获取文件sha值的函数
async function getFileSha() {
if (!fileName.value) {
return
}
const existingFileResponse = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${fileName.value}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
if (existingFileResponse.ok) {
const existingFileData = await existingFileResponse.json();
return existingFileData.sha;
} else {
console.log('仓库文件不存在')
return
}
}
查
async function findFileList() {
const fileList = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
).then(res=>res.json());
console.log(fileList)
}
增或改
async function handleUploadFile() {
const file = fileInput.files[0];
if (!file || !fileName.value) {
alert("文件为空或者输入文件路径为空");
return;
}
const commitMessage = "githubAPI";
let sha = await getFileSha();
const reader = new FileReader();
reader.onload = async function () {
const base64Content = btoa(
new Uint8Array(reader.result).reduce(
(data, byte) => data + String.fromCharCode(byte),
""
)
);
const fileData = {
message: commitMessage,
content: base64Content,
sha,
branch
};
const uploadResponse = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${fileName.value}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(fileData),
}
);
if (uploadResponse.ok) {
alert('文件上传成功')
} else {
alert('文件上传失败')
}
};
reader.readAsArrayBuffer(file);
}
删
async function handleDeleteFile() {
const sha = await getFileSha()
const message = '删除文件'
if (sha) {
const deleteResponse = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${fileName.value}`,
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
message,
sha,
branch
})
}
);
if (!deleteResponse.ok) {
throw new Error(`Failed to delete file: ${response.statusText}`);
}
alert('文件删除成功')
} else {
alert('要删除的文件不存在')
}
}
全部代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Upload File to GitHub</title>
</head>
<body>
<input type="text" id="fileName" placeholder="请选择文件路径">
<input type="file" id="fileInput" />
<button onclick="handleUploadFile()">Upload File</button>
<button style="background-color: brown;" onclick="handleDeleteFile()">删除</button>
<script>
const fileInput = document.getElementById("fileInput");
const fileName = document.getElementById("fileName");
const owner = "ConsoleLZ";
const repo = "githubAPI";
const accessToken = "???";
const branch = 'main'
async function handleUploadFile() {
const file = fileInput.files[0];
if (!file || !fileName.value) {
alert("文件为空或者输入文件路径为空");
return;
}
const commitMessage = "githubAPI";
let sha = await getFileSha();
const reader = new FileReader();
reader.onload = async function () {
const base64Content = btoa(
new Uint8Array(reader.result).reduce(
(data, byte) => data + String.fromCharCode(byte),
""
)
);
const fileData = {
message: commitMessage,
content: base64Content,
sha,
branch
};
const uploadResponse = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${fileName.value}`,
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(fileData),
}
);
if (uploadResponse.ok) {
alert('文件上传成功')
} else {
alert('文件上传失败')
}
};
reader.readAsArrayBuffer(file);
}
async function handleDeleteFile() {
const sha = await getFileSha()
const message = '删除文件'
if (sha) {
const deleteResponse = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${fileName.value}`,
{
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({
message,
sha,
branch
})
}
);
if (!deleteResponse.ok) {
throw new Error(`Failed to delete file: ${response.statusText}`);
}
alert('文件删除成功')
} else {
alert('要删除的文件不存在')
}
}
async function findFileList() {
const fileList = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
).then(res=>res.json());
console.log(fileList)
}
findFileList()
async function getFileSha() {
if (!fileName.value) {
return
}
const existingFileResponse = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${fileName.value}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);
if (existingFileResponse.ok) {
const existingFileData = await existingFileResponse.json();
return existingFileData.sha;
} else {
console.log('仓库文件不存在')
return
}
}
</script>
</body>
</html>