tsaebeht
tsaebeht

Reputation: 1680

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

Upvotes: 20

Views: 12298

Answers (2)

Phani Rithvij
Phani Rithvij

Reputation: 4487

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 and if the RAM isn't enough, it fails with 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

Note: An alternate way is to use gh release upload <tag> <files>... [flags] instead of curl. (docs)

gh is GitHub's official command line tool.

Upvotes: 8

galeksandrp
galeksandrp

Reputation: 776

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)"

Upvotes: 26

Related Questions