Integrate Qiniu upload service and update configuration
- 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.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
- `docs/common/README.md`
|
||||
- `docs/common/auth.md`
|
||||
- `docs/common/response.md`
|
||||
- `docs/common/upload_qiniu.md`
|
||||
|
||||
## 去水印小程序
|
||||
|
||||
|
||||
@@ -23,3 +23,6 @@
|
||||
|
||||
除登录接口外,其他接口都需要携带登录后返回的 `session_key`(见:`docs/common/auth.md`)。
|
||||
|
||||
## 上传(七牛直传)
|
||||
|
||||
- `docs/common/upload_qiniu.md`
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
# 七牛(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
|
||||
```
|
||||
|
||||
请求体(可选):
|
||||
|
||||
```json
|
||||
{
|
||||
"filename": "avatar.png"
|
||||
}
|
||||
```
|
||||
|
||||
说明:
|
||||
- `filename` 仅用于提取文件后缀(例如 `.png`),以便后端生成带后缀的 `key`;不传也可以。
|
||||
|
||||
成功响应示例:
|
||||
|
||||
```json
|
||||
{
|
||||
"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,上传时必须使用该 key
|
||||
- `upload_url`:上传入口(表单上传 URL)
|
||||
- `expire`:token 过期时间(Unix 秒)
|
||||
- `cdn_domain`:可选;如果配置了,可用 `cdn_domain + "/" + key` 拼出访问 URL
|
||||
|
||||
## 使用示例(curl 直传)
|
||||
|
||||
1) 先请求 token:
|
||||
|
||||
```bash
|
||||
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"}'
|
||||
```
|
||||
|
||||
2) 再把文件直传七牛(multipart/form-data):
|
||||
|
||||
```bash
|
||||
curl -X POST 'https://upload.qiniup.com' \
|
||||
-F "token=上一步返回的 token" \
|
||||
-F "key=上一步返回的 key" \
|
||||
-F "file=@./avatar.png"
|
||||
```
|
||||
|
||||
七牛成功时会返回 JSON(字段可能因配置不同略有差异),其中一般会包含 `key/hash`。
|
||||
|
||||
Reference in New Issue
Block a user