# Public Storage 模块设计
Public Storage 作为当前应用所有公共存储的数据库,并在 CDN 的协助下,实现文件的分发。需求参见:[Public Storage PRD](https://bp-doc.atcloudbox.com/x8yw8N8jQhyp5PjWWe-clw#)
## 1. 数据库设计
```
CREATE TABLE public_storage (
storage_id VARCHAR PRIMARY KEY,
app_id VARCHAR,
content_key VARCHAR,
content_value VARCHAR,
sys_created_time TIMESTAMP WITH TIME ZONE,
sys_updated_time TIMESTAMP WITH TIME ZONE,
sys_deleted_time TIMESTAMP WITH TIME ZONE
);
CREATE INDEX public_storage_app_id_content_key_index ON public_storage USING BTREE(app_id, content_key);
```
content_value 存储文本内容,或文件的相对路径(`public_storage/{file_name}`)
文件在 S3 存放路径:`s3://bytepower-server-public/app_data/{app_id}/public_storage/{file_name}`
在 redis 缓存查询过的数据,过期时间 N 分钟
**key**:{app_id}:public_storage:#content_key#
**value**: hash,字段包括:
- content_value
- sys_created_time
- sys_updated_time
## 2. 供 client 调用的接口
### 2.1 查询某个 key
for client: GET - /bp/public_storage/{content_key}
**重定向到文件的 CDN 地址**
## 3. Server to Server 接口
### 3.1 新增
POST - /bp/server/public_storage/{content_key}
内容为文件,使用`multipart/form-data`方式上传,包含字段:`content_value`
response:
```
{
"public_storage": {
"content_key": "key1",
"content_value": "public_storage/XXX",
"created_time": unixTime,
"updated_time": unixTime
}
}
```
key 已经存在:
{
"error": {
"message": "invalid_parameter(content_key is already exists)",
"error_type": "invalid_parameter"
}
}
### 3.2 修改
PUT - /bp/server/public_storage/{content_key}
response:
```
{
"public_storage": {
"content_key": "key1",
"content_value": "public_storage/XXX",
"updated_time": unixTime
}
}
```
### 3.3 删除
DELETE - /bp/server/public_storage/{content_key}
## 4. Console 内部的接口(仅供前端网站使用)
### 4.1 新增
for console: POST - /api/workspace/{spaceID}/application/{appID}/public_storage
request:
内容为文件,使用`multipart/form-data`方式上传,包含字段:`content_key`, `content_value`
response:
```
{
"public_storage": {
"content_key": "key1",
"content_value": "public_storage/XXX",
"created_time": unixTime,
"updated_time": unixTime
}
}
```
### 4.2 修改
for console: PUT - /api/workspace/{spaceID}/application/{appID}/public_storage/{content_key}
request:
内容为文件,使用`multipart/form-data`方式上传,包含字段:`content_value`
request:
内容为文件,使用`multipart/form-data`方式上传,包含字段:`content_value`
### 4.3 删除某个 key
DELETE - /api/workspace/{spaceID}/application/{appID}/public_storage/{content_key}
### 4.4 批量查询
**根据多个 keys 查询** (暂不需要)
for console: POST - /api/workspace/{spaceID}/application/{appID}/public_storage_bulk/keys
request:
```
{
"keys": [
"key1",
"key2"
]
}
```
response:
```
{
"result": [
{
"content_key": "key1",
"public_storage": {
"content_value": "{CDN_Prefix}/public_storage/XXX",
"created_time": unixTime,
"updated_time": unixTime
}
},
{
"content_key": "key2",
"error": "content_key is not exist"
}
]
}
```
**模糊查询** (不再使用)
for console: POST - /api/workspace/{spaceID}/application/{appID}/public_storage_bulk/condition
只支持包含关系
request:
```
{
"contains": "key"
}
```
response:
```
{
"public_storages": [{
"content_key": "key1",
"content_value": "{CDN_Prefix}/public_storage/value1",
"created_time": unixTime,
"updated_time": unixTime
}
],
"page": 1,
"per_page": 10,
"total_count": 100,
"total_pages": 10
}
```
**查询**
for console: GET - /api/workspace/{spaceID}/application/{appID}/public_storage?contains=key
?query=all_keys
response:
```
{
"keys": [
"key1",
"key2"
]
}
```
### 4.5 批量删除
for console: DELETE - /api/workspace/{spaceID}/application/{appID}/public_storage_bulk/keys
request:
```
{
"keys": [
"key1",
"key2"
]
}
```
### 4.6 查询某个 content_key
for console: GET - /api/workspace/{spaceID}/application/{appID}/public_storage/{content_key}
response:
```
{
"public_storage": {
"content_key": "key1",
"content_value": "{CDN_Prefix}/public_storage/XXX",
"created_time": unixTime,
"updated_time": unixTime
}
}
```
其中,`{CDN_Prefix}`来自于 App 配置`config.json`的 `download_url_prefix`:
```
"storage": {
"download_url_prefix": "http://cdn-storage.atcloudbox.com"
},
```