我正在嘗試使用其 API 將檔案上傳到 Github。
以下代碼有效,但僅適用于約小于 1MB 的較小尺寸內容。
tar -czvf logs.tar.gz a.log b.log
base64_logs=$(base64 logs.tar.gz | tr -d \\n)
content_response=$(curl \
-X PUT \
-u :"$GIT_TOKEN" \
-H "Accept: application/vnd.github.v3 json" \
"$content_url" \
-d '{"message": "Log files", "content": "'"$base64_logs"'"}')
對于有點大的內容,我收到以下錯誤:
/usr/bin/curl: Argument list too long
現在,關于此錯誤訊息的 SO 上已經有一個問題,它說直接上傳檔案。請參閱此處:curl:引數串列太長
當我嘗試這個時,我收到一條problem parsing JSON錯誤訊息。
tar -czvf logs.tar.gz a.log b.log
base64_logs=$( base64 logs.tar.gz | tr -d \\ ) > base64_logs.txt
content_response=$(curl \
-X PUT \
-u :"$GIT_TOKEN" \
-H "Accept: application/vnd.github.v3 json" \
"$content_url" \
-d '{"message": "Log files", "content": @base64_logs.txt}')
誰能指出我在這里犯錯的地方?謝謝!
uj5u.com熱心網友回復:
使用base64命令而不是@base64過濾器 from jq,因為后者只能編碼文本資料而不是來自.gz存檔的二進制資料。
通過管道將 base64 流jq格式化為 JSON 資料流。
Curl 將讀取 JSON 資料流并發送它。
# use base64 to encode binary data and -w0 all in one-line stream
base64 --wrap=0 logs.tar.gz |
# JSON format from raw input
jq \
--raw-input \
--compact-output \
'{"message": "Log files", "content": . }' |
# Pipe JSON to curl
curl \
--request PUT \
--user ":$GIT_TOKEN" \
--header "Accept: application/vnd.github.v3 json" \
--header 'Content-Type: application/json' \
--data-binary @- \
--url "$content_url"
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/321180.html
標籤:json 猛击 github 卷曲 github-api
上一篇:PythonCURL洗掉請求函式
