This project is a proof of concept for using a GitHub repository as an image hosting service. The API allows users to upload, list, and delete image files from the repository, providing an easy interface to manage files hosted on GitHub.
To set up and run this project, you need Node.js and npm installed on your system.
-
Clone the repository:
git clone https://github.com/MasFana/Github-Image-Bucket-API cd Github-Image-Bucket-API
-
Install dependencies:
npm install
-
Set up environment variables by creating a
.env
file in the root directory with the following values:GITHUB_TOKEN=<your_github_token> GITHUB_REPO=<your_github_repo> GITHUB_BRANCH=<your_github_branch>
-
Start the server:
npm start
The server will run on http://localhost:3000
.
The API requires the following environment variables to be set:
GITHUB_TOKEN
: GitHub Personal Access Token for authentication.GITHUB_REPO
: GitHub repository name in the formatowner/repository
.GITHUB_BRANCH
: GitHub branch name where files will be uploaded.
Uploads a file to the configured GitHub repository.
- Content-Type:
multipart/form-data
- Body: A form-data containing a file under the key
file
.
-
200 OK: If the file is successfully uploaded, returns the public URL of the uploaded file.
{ "publicUrl": "https://raw.githubusercontent.com/<repo>/<branch>/uploads/<file_name>" }
-
500 Internal Server Error: If the upload fails, an error message is returned.
{ "error": "Failed to upload the file" }
Retrieves a list of all files uploaded to the GitHub repository.
- No parameters are required.
-
200 OK: Returns a list of file names with their corresponding public URLs.
[ { "name": "<file_name>", "url": "https://raw.githubusercontent.com/<repo>/<branch>/uploads/<file_name>" }, ... ]
-
500 Internal Server Error: If there is an issue retrieving the file list, an error message is returned.
{ "error": "Failed to retrieve files" }
Deletes a file from the GitHub repository.
-
Content-Type:
application/json
-
Body: JSON object with the file name to be deleted.
{ "fileName": "<file_name>" }
-
200 OK: If the file is successfully deleted, returns a success message.
{ "message": "File \"<file_name>\" deleted successfully" }
-
400 Bad Request: If the file name is not provided in the request body.
{ "error": "File name is required" }
-
500 Internal Server Error: If there is an error deleting the file, an error message is returned.
{ "error": "Failed to delete the file" }
- readFileAsBase64(filePath): Reads a file from the specified
filePath
and returns its content encoded as Base64. - deleteLocalFile(filePath): Deletes a local file from the given
filePath
. - getGithubFileSha(fileName): Retrieves the SHA hash of a file stored in the GitHub repository for deletion.
- uploadFileToGithub(file, content): Uploads a file to the GitHub repository with its Base64 content.
- deleteFileFromGithub(fileName, sha): Deletes a file from the GitHub repository using its SHA hash.
-
Install all required dependencies:
npm install
-
Start the Express server:
npm start
The API will be available at
http://localhost:3000
.
- Ensure that your GitHub repository is set up to accept file uploads and has the correct permissions for your GitHub token.
- This API assumes that the uploaded files will be stored in the
uploads/
directory in the GitHub repository.