github-image-upload
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseUpload images and files to GitHub (gh-image)
上传图片和文件到GitHub (gh-image)
GitHub has no public API for attachment uploads — the web UI uses an internal
endpoint that mints URLs scoped to the repo's visibility.
(MIT, © drogers0) replicates
that flow as a CLI extension, so you can upload images or other files (PDF,
zip, log, …) from the terminal and get a ready-to-paste reference back — an
embed for images, a bare URL for videos (GitHub renders it as an
inline player), or a download link for other files.
user-attachmentsgh-imagegh[name](url)This skill drives and then embeds the result into a PR/issue/comment.
gh-imageGitHub 没有用于附件上传的公开API——网页端UI使用一个内部端点生成与仓库可见性绑定的链接。(MIT协议,© drogers0)将该流程复刻为一款 CLI扩展,让你可以从终端上传图片或其他文件(PDF、zip、日志等),并直接获得可粘贴的引用——图片对应嵌入格式,视频对应纯链接(GitHub会将其渲染为内嵌播放器),其他文件则对应下载链接。
user-attachmentsgh-imagegh[name](url)本技能调用并将结果嵌入到PR/Issue/评论中。
gh-imagePrerequisites — verify these before uploading
前置条件——上传前请确认以下事项
Run these checks; only act on the ones that fail.
-
CLI installed & authenticated
ghbashgh auth statusIf it fails, tell the user to run(do not attempt it unattended).gh auth login -
Theextension installed (idempotent — skip if already present)
gh-imagebashgh extension list | grep -q 'drogers0/gh-image' || gh extension install drogers0/gh-image -
A GitHub session for the upload.does NOT use the
gh-imagetoken for the upload (that endpoint rejects tokens); it needs the browserghcookie. Resolution order (first match wins):user_session- flag, or
--token <value> - env var (use this in CI / headless), or
GH_SESSION_TOKEN - the cookie store of a logged-in browser (Chrome/Brave/Chromium/Edge/Firefox/ Opera/Safari) — the default for local use. On macOS the first read may show a Keychain prompt; the user should click Always Allow.
⚠️ Acookie grants full account access (it is not scoped like a PAT). Treat it like a password; in CI use a dedicated bot account.user_session
执行以下检查,仅处理未通过的项。
-
CLI已安装并完成认证
ghbashgh auth status如果检查失败,请告知用户运行(请勿自动执行该操作)。gh auth login -
已安装扩展(幂等操作——若已安装可跳过)
gh-imagebashgh extension list | grep -q 'drogers0/gh-image' || gh extension install drogers0/gh-image -
用于上传的GitHub会话。不会使用
gh-image令牌进行上传(该端点拒绝令牌);它需要浏览器的ghCookie。优先级顺序(匹配到第一个即生效):user_session- 参数,或
--token <value> - 环境变量(适用于CI/无头环境),或
GH_SESSION_TOKEN - 已登录浏览器的Cookie存储(支持Chrome/Brave/Chromium/Edge/Firefox/Opera/Safari)——本地使用的默认方式。在macOS上首次读取时可能会弹出钥匙链提示,用户应点击始终允许。
⚠️Cookie授予完整账户访问权限(不像PAT那样有范围限制)。请像对待密码一样妥善保管;在CI环境中请使用专用机器人账户。user_session
Step 1 — Normalize the file path
步骤1——标准化文件路径
Use an absolute path. If a glob is given, resolve it first. Paths with spaces
or Unicode (e.g. CleanShot's narrow spaces) work, but quote them.
使用绝对路径。如果给定通配符,先解析路径。包含空格或Unicode字符的路径(例如CleanShot的窄空格)可以正常使用,但需要加引号。
Step 2 — Upload
步骤2——上传
bash
undefinedbash
undefinedOne or more files (images or PDF/zip/log/…); --repo is optional inside a repo
单个或多个文件(图片或PDF/zip/日志等);在仓库工作目录中时--repo参数可选(会自动从远程仓库推断)。
working dir (inferred from the remote).
—
gh image "/abs/path/screenshot.png" --repo <owner>/<repo>
`gh image` prints the reference to **stdout** — an image embed for images, a bare
URL for videos (GitHub renders it as an inline player), and a download link for
other files, e.g.:
Capture that output — it is the embeddable reference. For multiple files it prints
one line per file.gh image "/abs/path/screenshot.png" --repo <owner>/<repo>
`gh image` 将引用输出到**标准输出**——图片对应图片嵌入格式,视频对应纯链接(GitHub会渲染为内嵌播放器),其他文件对应下载链接,例如:
捕获该输出——这就是可嵌入的引用。如果上传多个文件,每个文件对应一行输出。Step 3 — Embed into the PR / issue / comment
步骤3——嵌入到PR / Issue / 评论中
gh-imageAppend to a PR description (preserves the existing body):
bash
MD="$(gh image "/abs/path/shot.png" --repo owner/repo)"
BODY="$(gh pr view <pr> --repo owner/repo --json body -q .body)"
printf '%s\n\n## Screenshots\n\n%s\n' "$BODY" "$MD" \
| gh pr edit <pr> --repo owner/repo --body-file -Post as a new PR comment:
bash
MD="$(gh image "/abs/path/shot.png" --repo owner/repo)"
printf '## Screenshots\n\n%s\n' "$MD" | gh pr comment <pr> --repo owner/repo --body-file -Add to an issue body / comment: same pattern with
or .
gh issue edit <n> --body-file -gh issue comment <n> --body-file -Always use (not inline ) so multi-line bodies and special
characters can't break shell quoting.
--body-file ---bodygh-image追加到PR描述中(保留原有内容):
bash
MD="$(gh image "/abs/path/shot.png" --repo owner/repo)"
BODY="$(gh pr view <pr> --repo owner/repo --json body -q .body)"
printf '%s\n\n## 截图\n\n%s\n' "$BODY" "$MD" \
| gh pr edit <pr> --repo owner/repo --body-file -发布为新的PR评论:
bash
MD="$(gh image "/abs/path/shot.png" --repo owner/repo)"
printf '## 截图\n\n%s\n' "$MD" | gh pr comment <pr> --repo owner/repo --body-file -添加到Issue内容/评论: 使用相同模式,替换为或。
gh issue edit <n> --body-file -gh issue comment <n> --body-file -请始终使用(而非内联的),这样多行内容和特殊字符不会破坏shell引号规则。
--body-file ---bodyStep 4 — Verify
步骤4——验证
bash
gh pr view <pr> --repo owner/repo --json body -q .body # confirm the URL is presentThe URL inherits the repo's visibility, so on a private repo
it renders only for authorized viewers (an anonymous fetch returns 404/403 — that is
expected, not a failure).
user-attachmentsbash
gh pr view <pr> --repo owner/repo --json body -q .body # 确认链接已存在user-attachmentsSizing (optional)
尺寸调整(可选)
To control display size, embed an HTML tag instead of the bare markdown:
html
<img width="800" alt="screenshot" src="https://github.com/user-attachments/assets/<uuid>" />如需控制显示尺寸,使用HTML标签替代纯Markdown格式:
html
<img width="800" alt="screenshot" src="https://github.com/user-attachments/assets/<uuid>" />Troubleshooting
故障排除
| Symptom | Cause / fix |
|---|---|
| The org requires SSO and your session isn't authorized. Open the |
| The generic no-token case. Confirm you have write access; if the repo's org uses SSO, authorize at |
No | Log into GitHub in a supported browser, or set |
| Windows + Chrome 127+ can't read cookies | Known cookie-library limitation — use another browser or |
| CI / headless run | Set |
| Install the GitHub CLI ( |
| 症状 | 原因/解决方法 |
|---|---|
| 该组织要求SSO且你的会话未授权。在浏览器中打开消息中的 |
| 通用的无令牌情况。确认你拥有写入权限;如果仓库所属组织使用SSO,请访问 |
No | 在支持的浏览器中登录GitHub,或设置 |
| Windows + Chrome 127+无法读取Cookie | 已知的Cookie库限制——使用其他浏览器或设置 |
| CI / 无头环境运行 | 设置 |
| 安装GitHub CLI(例如 |