Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload github asset file using CURL

I want to upload a file on my desktop called 'hello.txt' to my git repository which has a release. How do I do this? I read the git documentation but it says something like :

POST https://<upload_url>/repos/:owner/:repo/releases/:id/assets?name. How to do this in CURL. I did not understand this.

How to post this file as a release asset to my github release? Thanks

like image 542
tsaebeht Avatar asked Jun 13 '16 09:06

tsaebeht


People also ask

How do I upload an asset to GitHub?

On GitHub.com, navigate to the main page of the repository. Above the list of files, using the Add file drop-down, click Upload files. Drag and drop the file or folder you'd like to upload to your repository onto the file tree.

What is curl GitHub?

Curl is a command-line tool for transferring data specified with URL syntax.


2 Answers

curl \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "Content-Type: $(file -b --mime-type $FILE)" \
    --data-binary @$FILE \
    "https://uploads.github.com/repos/hubot/singularity/releases/123/assets?name=$(basename $FILE)"
  • https://developer.github.com/changes/2013-09-25-releases-api/
  • Cannot upload github release asset through API
like image 74
galeksandrp Avatar answered Oct 06 '22 00:10

galeksandrp


Extending upon @galeksandrp's answer, here are some issues I encountered

Note that the --data-binary option copies the file contents first to RAM, so if you have large files say close to 2048MB i.e. the absolute limit for github releases, for some reason, and if the RAM isn't enough, it fails curl: option -d: out of memory.

The fix for that is to use -T file path (without the @) and also on a side note if you want to see the upload progress you need to pipe the output to cat such as curl <...the whole command> | cat

So the complete command would look like this

curl -X POST \
    -H "Content-Length: <file size in bytes>" \
    -H "Content-Type: $(file -b --mime-type $FILE)" \ #from @galeksandrp's answer
    -T "path/to/large/file.ext" \
    -H "Authorization: token $GITHUB_TOKEN" \
    -H "Accept: application/vnd.github.v3+json" \ 
    https://uploads.github.com/repos/<username>/<repo>/releases/<id>/assets?name=<name> | cat
like image 33
Phani Rithvij Avatar answered Oct 05 '22 23:10

Phani Rithvij