s3-upload
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseS3 Upload
S3 Upload
通过 操作 S3 兼容存储(七牛 Kodo、MinIO、AWS S3 等)。配置来自环境变量,与 及 一致。
scripts/s3-cli.jssrc/app-agent/lib/server/s3-storage.ts.env-templateOperate S3-compatible storage (Qiniu Kodo, MinIO, AWS S3, etc.) via . Configurations come from environment variables, consistent with and .
scripts/s3-cli.jssrc/app-agent/lib/server/s3-storage.ts.env-templateAgent 工作流
Agent Workflow
- 先检查配置 — 未配置时不要猜测密钥,提示用户补全 :
.envbashnode --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js config - 选命令 — 上传用 ;浏览用
upload;按名称搜用list;取公网链用find。url - 优先 — 需要解析结果给后续步骤时用 JSON 输出。
--json - 从仓库根目录执行 — 依赖根目录 中的
node_modules。@aws-sdk/client-s3 - 删除需 — 避免误删;向用户确认后再删。
--yes - 失败时必须给出正确反馈 — 见下方「错误处理」;不要编造 URL 或假装上传成功。
- Check Configuration First — Do not guess credentials when not configured, prompt the user to complete :
.envbashnode --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js config - Select Command — Use for uploading;
uploadfor browsing;listfor searching by name;findfor getting public links.url - Prioritize — Use JSON output when results need to be parsed for subsequent steps.
--json - Execute from Repository Root — Depends on in the root directory's
@aws-sdk/client-s3.node_modules - Required for Deletion — Avoid accidental deletion; confirm with the user before deleting.
--yes - Must Provide Correct Feedback on Failure — See "Error Handling" below; do not fabricate URLs or pretend uploads were successful.
错误处理(Agent 必遵)
Error Handling (Mandatory for Agent)
CLI 失败时 exit code = 1。加 时 stdout 为结构化 JSON(含 、、),Agent 必须解析后再回复用户。
--jsoncodeerrorhintWhen the CLI fails, exit code = 1. When adding , stdout is structured JSON (containing , , ), and the Agent must parse it before replying to the user.
--jsoncodeerrorhint回复用户格式
User Reply Format
失败时按此结构告知用户(用中文,不要只贴原始 stack):
- 发生了什么 — 引用 字段
error - 可能原因 — 结合 与上下文
code - 下一步 — 引用 ;若可自动修复(如
hint),先尝试再汇报--no-acl
禁止: 忽略 exit code;把 stderr 当成功;未验证就回复公网 URL;向用户展示完整 Secret Key。
When failing, inform the user in this structure:
- What Happened — Reference the field
error - Possible Cause — Combine with context
code - Next Steps — Reference the ; if auto-fix is available (e.g.,
hint), attempt it first before reporting--no-acl
Prohibited: Ignore exit code; treat stderr as success; reply with public URL without verification; show complete Secret Key to users.
错误码对照
Error Code Reference
| 含义 | Agent 动作 |
|---|---|---|
| 缺环境变量 | 列出 |
| 密钥错误 | 请用户核对 AK/SK,不要重复用同一错误密钥盲试 |
| 无权限 | 说明权限不足;upload 时若 hint 提到 |
| 不支持 ACL | 用 |
| 桶不存在 | 检查 |
| 对象不存在 | 建议 |
| 本地文件不存在 | 检查路径、cwd 是否在仓库根目录 |
| 未加 | 向用户确认后再加 |
| 参数缺失 | 按 hint 修正命令,不要猜参数 |
| 网络/端点不可达 | 检查 endpoint、region、代理 |
| 缺 SDK | 在仓库根目录 |
| 其他 S3 错误 | 汇报 |
| Meaning | Agent Action |
|---|---|---|
| Missing environment variables | List the |
| Invalid credentials | Ask the user to check AK/SK, do not blindly retry with the same invalid credentials |
| Permission denied | Explain insufficient permissions; if the hint mentions |
| ACL not supported | Retry upload with |
| Bucket does not exist | Check if |
| Object does not exist | Suggest using |
| Local file does not exist | Check the path and whether cwd is in the repository root |
| | Confirm with the user before adding |
| Missing parameters | Correct the command according to the hint, do not guess parameters |
| Network/endpoint unreachable | Check endpoint, region, and proxy |
| Missing SDK | Retry after running |
| Other S3 errors | Report |
诊断流程
Diagnostic Flow
命令失败 (exit 1)
├─ 加 --json 重跑,读取 code
├─ CONFIG_* / INVALID_CREDENTIALS → config
├─ OBJECT_NOT_FOUND → list/find 定位 key
├─ ACCESS_DENIED + upload → 试 --no-acl
└─ 仍失败 → 原样汇报 error/hint/details,不要擅自改密钥Command failed (exit 1)
├─ Re-run with --json, read code
├─ CONFIG_* / INVALID_CREDENTIALS → config
├─ OBJECT_NOT_FOUND → list/find to locate key
├─ ACCESS_DENIED + upload → try --no-acl
└─ Still failed → Report error/hint/details as is, do not modify credentials without permission示例:向用户反馈
Example: User Feedback
配置缺失:
S3 未配置完整,缺少。请在项目根目录S3_SECRET_ACCESS_KEY中补全(参考.env),保存后我再验证。.env-template
上传 ACL 失败(Agent 应先重试):
存储不支持 ACL,已改用重新上传并成功。公网 URL: …--no-acl
对象不存在:
桶内没有。当前assets/demo/missing.png下共有 3 个文件:…assets/demo/
Missing Configuration:
S3 is not fully configured, missing. Please complete it in theS3_SECRET_ACCESS_KEYfile at the project root (refer to.env), and I will verify it after saving..env-template
Upload ACL Failure (Agent should retry first):
The storage does not support ACL. Has re-uploaded successfully using. Public URL: …--no-acl
Object Not Found:
The objectdoes not exist in the bucket. There are 3 files under currentassets/demo/missing.png: …assets/demo/
快速开始
Quick Start
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js --help
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js config上传并拿到公网 URL:
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js upload ./local.png --prefix assets/demo/列出 / 查找:
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js list --prefix uploads/ --max 50
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js find "photo" --prefix assets/
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js find "*.png" --prefix assets/下载 / 删除 / URL:
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js download assets/demo/photo.png --output ./photo.png
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js url assets/demo/photo.png
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js delete assets/demo/old.png --yesbash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js --help
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js configUpload and get public URL:
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js upload ./local.png --prefix assets/demo/List / Find:
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js list --prefix uploads/ --max 50
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js find "photo" --prefix assets/
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js find "*.png" --prefix assets/Download / Delete / URL:
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js download assets/demo/photo.png --output ./photo.png
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js url assets/demo/photo.png
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js delete assets/demo/old.png --yes环境变量
Environment Variables
| 变量 | 必填 | 说明 |
|---|---|---|
| 是 | 存储桶 |
| 是 | Access Key |
| 是 | Secret Key |
| 是 | 服务端点(如 |
| 否 | 默认 |
| 否 | CDN 域名; |
| 否 | 提供商标识( |
CLI 会自动读取仓库根目录 (不覆盖已有 )。也可显式传 。
.envprocess.envnode --env-file-if-exists=.env禁止 把密钥写入 skill、脚本或提交到 git。只读 或用户提供的 env。
.env| Variable | Required | Description |
|---|---|---|
| Yes | Storage bucket |
| Yes | Access Key |
| Yes | Secret Key |
| Yes | Service endpoint (e.g., |
| No | Defaults to |
| No | CDN domain name; |
| No | Provider identifier ( |
The CLI automatically reads the file in the repository root (does not overwrite existing ). You can also explicitly pass .
.envprocess.envnode --env-file-if-exists=.envDo NOT write credentials into skills, scripts, or commit to git. Only read from or user-provided env.
.env阿里云 OSS(S3_PROVIDER=aliyun
)
S3_PROVIDER=aliyunAlibaba Cloud OSS (S3_PROVIDER=aliyun
)
S3_PROVIDER=aliyunenv
S3_BUCKET_NAME=<bucket>
S3_ACCESS_KEY_ID=<RAM AccessKey ID>
S3_SECRET_ACCESS_KEY=<RAM AccessKey Secret>
S3_ENDPOINT=https://<bucket>.s3.oss-<region>.aliyuncs.com
S3_REGION=cn-hangzhou
S3_CDN=https://<your-cdn-domain>
S3_PROVIDER=aliyun要点:
- Endpoint — 推荐 S3 兼容格式:(与
https://<bucket>.s3.oss-cn-hangzhou.aliyuncs.com一致);亦可用S3_BUCKET_NAME(path-style)。CLI 会自动识别 endpoint 是否已含 bucket 子域。https://s3.oss-cn-hangzhou.aliyuncs.com - Region — 与 endpoint 中的地域一致(如 ),不要用占位符
cn-hangzhou。<your-region> - ACL — 新版 OSS 桶通常禁用 Object ACL;CLI 在 下 默认不上传 ACL。若仍报 ACL 错,显式加
aliyun。--no-acl - 公网 URL — 配置了 时:
S3_CDN;需确保 CDN 已绑定该桶并开启回源。https://cdn.example.com/<objectKey> - RAM 权限 — 密钥需有目标桶的 、
oss:PutObject、oss:GetObject、oss:ListObjects等。oss:DeleteObject
env
S3_BUCKET_NAME=<bucket>
S3_ACCESS_KEY_ID=<RAM AccessKey ID>
S3_SECRET_ACCESS_KEY=<RAM AccessKey Secret>
S3_ENDPOINT=https://<bucket>.s3.oss-<region>.aliyuncs.com
S3_REGION=cn-hangzhou
S3_CDN=https://<your-cdn-domain>
S3_PROVIDER=aliyunKey Points:
- Endpoint — Recommended S3-compatible format: (consistent with
https://<bucket>.s3.oss-cn-hangzhou.aliyuncs.com); you can also useS3_BUCKET_NAME(path-style). The CLI will automatically detect whether the endpoint contains a bucket subdomain.https://s3.oss-cn-hangzhou.aliyuncs.com - Region — Consistent with the region in the endpoint (e.g., ), do not use placeholder
cn-hangzhou.<your-region> - ACL — New OSS buckets usually disable Object ACL; the CLI does not upload ACL by default when is set. If you still get an ACL error, explicitly add
aliyun.--no-acl - Public URL — When is configured:
S3_CDN; ensure the CDN is bound to the bucket and origin-pull is enabled.https://cdn.example.com/<objectKey> - RAM Permissions — The credentials need permissions like ,
oss:PutObject,oss:GetObject,oss:ListObjectsfor the target bucket.oss:DeleteObject
命令参考
Command Reference
| 命令 | 用途 |
|---|---|
| 验证配置,密钥脱敏输出 |
| 上传; |
| 按 |
| key 子串或 |
| 是否存在及元数据 |
| 下载; |
| 删除对象 |
| 输出公网 URL |
全局:、 / 。
--json-h--help| Command | Purpose |
|---|---|
| Verify configuration, output credentials in desensitized format |
| Upload; options: |
| List by |
| Substring or wildcard ( |
| Check existence and metadata |
| Download; |
| Delete object |
| Output public URL |
Global options: , / .
--json-h--help与应用代码的关系
Relationship with Application Code
- 服务端上传逻辑见 (Presigned POST、CDN URL 解析等)。
src/app-agent/lib/server/s3-storage.ts - Agent 运维/一次性上传 用本 skill 的 CLI,不要在对话里重写 SDK 逻辑。
- 应用内功能 继续用 或现有 OSS 管道,不要混用 CLI。
s3-storage.ts
- Server-side upload logic can be found in (Presigned POST, CDN URL parsing, etc.).
src/app-agent/lib/server/s3-storage.ts - Agent operation/one-time upload uses this skill's CLI; do not rewrite SDK logic in conversations.
- In-app functions continue to use or existing OSS pipelines; do not mix with the CLI.
s3-storage.ts
常见场景
Common Scenarios
用户要上传截图/构建产物并分享链接
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js upload ./dist/bundle.zip --key releases/v1.2.0/bundle.zip --json从 JSON 取 回复用户。
publicUrl用户问某前缀下有哪些文件
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js list --prefix static/_next/ --max 100 --json用户给了 CDN URL,要确认 key 是否存在
用 的逻辑:去掉 CDN 前缀得 key,再 。
objectKeyFromPublicUrlhead配置报错
缺少必填 env → CLI 返回 及 数组 → 对照 请用户补全 → 重跑 直到 。
CONFIG_MISSINGmissing.env-templateconfigok: trueCLI 返回 JSON 错误示例
json
{
"ok": false,
"code": "ACL_NOT_SUPPORTED",
"error": "当前存储不支持 ACL 字段",
"hint": "重试: upload <file> ... --no-acl",
"details": "…"
}Agent 应执行 hint 中的重试,成功后再回复用户。
.envS3_*同一文件内出现多组 等变量时,后出现的会覆盖先前的(Node 行为)。只保留一组配置,旧配置注释掉。
S3_BUCKET_NAME--env-fileUser wants to upload screenshots/build artifacts and share links
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js upload ./dist/bundle.zip --key releases/v1.2.0/bundle.zip --jsonExtract from JSON to reply to the user.
publicUrlUser asks which files are under a certain prefix
bash
node --env-file-if-exists=.env .cursor/skills/s3-upload/scripts/s3-cli.js list --prefix static/_next/ --max 100 --jsonUser provides a CDN URL and wants to confirm if the key exists
Use the logic of : remove the CDN prefix to get the key, then run .
objectKeyFromPublicUrlheadConfiguration Error
Missing required env → CLI returns and array → Ask the user to complete it by referring to → Re-run until .
CONFIG_MISSINGmissing.env-templateconfigok: trueCLI JSON Error Example
json
{
"ok": false,
"code": "ACL_NOT_SUPPORTED",
"error": "Current storage does not support ACL field",
"hint": "Retry: upload <file> ... --no-acl",
"details": "…"
}The Agent should execute the retry in the hint, then reply to the user after success.
Do NOT Define Repeatedly in
S3_*.envWhen multiple sets of variables like appear in the same file, the later ones will overwrite the earlier ones (Node behavior). Keep only one set of configurations, comment out old ones.
S3_BUCKET_NAME--env-file