How to use github repo as like CDN server for uploading assets file?

3k Views Asked by At

I am learning ruby on rails. I am developing a rails 5 application. I don't want to use amazon s3 service for containing my assets files. I want to use github for serving my assets files as like CDN.

But I am facing a problem, I have dynamic file and image upload system. So therefore, When I will upload my files and images, all files and images will be uploaded in a github repository (assume, I have a git repo named busket; so, all images and files will be uploaded in busket repo from my server and will serve all assets from rails application).

So, how can I make github as like CDN ? Please help me about this issue.

4

There are 4 best solutions below

0
On BEST ANSWER

1.) Encode image to base 64 ref: http://ruby-doc.org/stdlib-2.2.0/libdoc/base64/rdoc/Base64.html

2.) Make API call to github

ref: https://developer.github.com/v3/repos/contents/#create-a-file

This will upload and return the url of your stored image within github, store that in your database.

Although you stated you didn't want to use S3, the paperclip gem makes it very streamlined.

1
On

I feel that it would be a generally bad idea to upload images and general types of content files to GitHub for long terms storage. GitHub was designed to be a repository provider for Git, not as a NoSQL or other type of data store. Updating files in GitHub would require making a commit to a particular branch. Hence, every time you change an image file, it would require a new commit. This won't scale, because Git does not handle binary files well.

So if you need a long term data store for your image and content files, I would suggest looking into tools which were designed for that, such as Amazon's S3, Google Cloud Storage, and things similar to this.

1
On
https://cdn.jsdelivr.net/gh/username/repository@master/file

username = name of the user of github
repository = name of github repo
file = actual name of the file

0
On

As mentioned by @Tim its generally a bad idea. However if you still want to use Git as a File Server append

?raw=true

at the end of the URI. For example

https://github.com/git/git/blob/master/ewah/bitmap.c?raw=true

would give you the contents of the file.