Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a direct link to a file in github LFS (large file storage)?

I uploaded a large file to gitub using Git LFS (Large file storage).

At first I could download the file from a direct link.

raw.githubusercontent.com/userName/reposiotry/master/file.mp4

But on the next day the file began to contain a text value

oid sha256:59f24bc922e1a48bb3feeba18b23f0e9622a7ee07166d925650d7a933283f8b1
size 123882252

How do I download this file ? How to get a direct link to it ?

like image 807
I.U. Avatar asked Oct 03 '19 13:10

I.U.


3 Answers

Assuming that ServerAddress is for example https://api.github.com:

  1. if you know your RepoName ('UserName/RepositoryName' pair) and FileHash, then you fetch: ServerAddress + '/repos/RepoName/git/blobs/FileHash'
    and if in response you have:
version https://git-lfs.github.com/spec/v1  
oid sha256:59f24bc922e1a48bb3feeba18b23f0e9622a7ee07166d925650d7a933283f8b1  
size 123882252
  1. then you must search in contents of your repository for the matching FileHash,
    fetching: ServerAddress + '/repos/RepoName/contents'
    and in response You have:
{
  "name": "filename.ext",
  "path": "path/filename.ext",
  "sha": "FileHash",
  "size": 42,
  "url": "https://api.github.com/repos/RepoName/contents/path/filename.ext?ref=master",
  "html_url": "...",
  "git_url": "...",
  "download_url": "...",
  "type": "file",
  "_links": { ... }
}
  1. now you take url value, and fetch it,
    in response you have:
{
  "name": "filename.ext",
  "path": "path/filename.ext",
  "sha": "FileHash",
  "size": 720896,
  "url": "...",
  "html_url": "...",
  "git_url": "...",
  "download_url": "https://media.githubusercontent.com/media/RepoName/RepoHash/path/filename.ext?token=...",
  "type": "file",
  "content": "...", // same as response from pt. 1
  "encoding": "base64",
  "_links": { ... }
}
  1. now you take download_url value, and fetch it,
    in response you should have BINARY file content.
like image 85
NevTon Avatar answered Nov 15 '22 03:11

NevTon


Quote from this article,

Storage quota

If you use more than 1 GB of storage without purchasing a data pack, you can still clone repositories with large assets, but you will only retrieve the pointer files, and you will not be able to push new files back up.

I am assuming you have not purchased any additional storage for git lfs on GitHub. As you said that initially you were able to access it via this link : http://raw.githubusercontent.com/userName/reposiotry/master/file.mp4

I am guessing that you uploaded more such files which caused you to exceed your storage quota i.e 1 GB. As pointed by the article above, you will only retrieve the pointer files once you exceed the quota.

You can check if you have exceeded the limit of your storage quota here, https://github.com/settings/billing

However, if my assumptions are wrong, then you could try to get the file using this link

https://media.githubusercontent.com/media/user_name/repository_name/branch_name/file_name

like image 35
Saurabh P Bhandari Avatar answered Nov 15 '22 03:11

Saurabh P Bhandari


You need to go into the web interface and find the raw link for your file and use that instead.

When you use Git LFS, the file that's actually stored in your repository is a pointer file like the one you see above. GitHub has different URLs to serve raw content for repository contents and Git LFS files, so if you want to get the Git LFS contents, you'll need to use the appropriate link. If you previously had the file as a non-LFS file, then the URL will have changed when you uploaded it again as an LFS file.

Just to note, using those URLs in web pages or otherwise using those direct links as a form of CDN is discouraged.

like image 1
bk2204 Avatar answered Nov 15 '22 02:11

bk2204