cd7ae5ac56
- Added Qiniu configuration options to .env.example and config.go for file uploads. - Refactored main.go to include new Qiniu service and upload handler. - Updated route registration to accommodate the new upload handler. - Enhanced documentation to include references for Qiniu upload functionality. - Removed legacy authentication handler and services to streamline the codebase.
1.8 KiB
1.8 KiB
七牛(Kodo)直传:获取上传凭证
用途:小程序/前端把文件直接上传到七牛(CDN 源站),后端只负责签发上传 token,减少带宽与压力。
前置条件
- 已完成登录并拿到
session_key(见:docs/common/auth.md) - 已配置
.env中的七牛参数(见:.env.example)
接口
POST /api/v1/common/upload/qiniu/token
Header:
Authorization: Bearer <session_key>
Content-Type: application/json
请求体(可选):
{
"filename": "avatar.png"
}
说明:
filename仅用于提取文件后缀(例如.png),以便后端生成带后缀的key;不传也可以。
成功响应示例:
{
"code": 200,
"message": "success",
"data": {
"token": "xxx",
"key": "uploads/mp_1/user_123/20251231/ab12cd34ef....png",
"upload_url": "https://upload.qiniup.com",
"expire": 1767150000,
"cdn_domain": "https://cdn.example.com"
}
}
字段说明:
token:七牛上传凭证(uptoken)key:后端生成的对象 key,上传时必须使用该 keyupload_url:上传入口(表单上传 URL)expire:token 过期时间(Unix 秒)cdn_domain:可选;如果配置了,可用cdn_domain + "/" + key拼出访问 URL
使用示例(curl 直传)
- 先请求 token:
curl -X POST 'http://127.0.0.1:8080/api/v1/common/upload/qiniu/token' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer wx-session-key' \
-d '{"filename":"avatar.png"}'
- 再把文件直传七牛(multipart/form-data):
curl -X POST 'https://upload.qiniup.com' \
-F "token=上一步返回的 token" \
-F "key=上一步返回的 key" \
-F "file=@./avatar.png"
七牛成功时会返回 JSON(字段可能因配置不同略有差异),其中一般会包含 key/hash。