fsd-oss 0.14.1 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +53 -26
- package/index.d.ts +461 -14
- package/package.json +9 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Liang Xingchen https://github.com/liangxingchen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, destribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,39 +1,66 @@
|
|
|
1
1
|
# fsd-oss
|
|
2
2
|
|
|
3
|
-
FSD OSS
|
|
3
|
+
FSD 阿里云 OSS 文件存储适配器 - 提供对阿里云 OSS 对象存储的读写访问。
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
const FSD = require('fsd');
|
|
7
|
-
const OSSAdapter = require('fsd-oss');
|
|
5
|
+
[](https://www.npmjs.com/package/fsd-oss)
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
const fsd = FSD({ adapter: adapter });
|
|
7
|
+
## 概述
|
|
11
8
|
|
|
12
|
-
|
|
9
|
+
`fsd-oss` 是 `fsd` 核心库的阿里云 OSS 适配器,提供完整的对象存储操作能力。
|
|
13
10
|
|
|
14
|
-
|
|
11
|
+
### 核心特性
|
|
15
12
|
|
|
13
|
+
- ✅ 完整的文件 CRUD 操作
|
|
14
|
+
- ✅ 分段上传(大文件优化)
|
|
15
|
+
- ✅ STS 角色扮演(边缘上传)
|
|
16
|
+
- ✅ 上传回调通知
|
|
17
|
+
- ✅ 自定义 OSS 客户端
|
|
18
|
+
- ✅ 缩略图支持
|
|
19
|
+
|
|
20
|
+
## 安装
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install fsd-oss
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## 配置
|
|
27
|
+
|
|
28
|
+
```typescript
|
|
29
|
+
import FSD from 'fsd';
|
|
30
|
+
import OSSAdapter from 'fsd-oss';
|
|
31
|
+
|
|
32
|
+
// 创建适配器
|
|
33
|
+
const adapter = new OSSAdapter({
|
|
34
|
+
accessKeyId: 'your-access-key-id', // 必需:OSS 访问凭证
|
|
35
|
+
accessKeySecret: 'your-access-key-secret', // 必需:OSS 访问凭证
|
|
36
|
+
region: 'oss-cn-hangzhou', // 必需:OSS 区域
|
|
37
|
+
bucket: 'your-bucket-name' // 可选:Bucket 名称
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// 创建 FSD 实例
|
|
41
|
+
const fsd = FSD({ adapter });
|
|
16
42
|
```
|
|
17
43
|
|
|
18
|
-
|
|
44
|
+
### 配置选项说明
|
|
19
45
|
|
|
20
|
-
|
|
46
|
+
| 选项 | 类型 | 必需 | 默认值 | 说明 |
|
|
47
|
+
|------|------|------|--------|------|
|
|
48
|
+
| `accessKeyId` | string | 是 | - | OSS 访问凭证 |
|
|
49
|
+
| `accessKeySecret` | string | 是 | - | OSS 访问凭证 |
|
|
50
|
+
| `region` | string | 是 | - | OSS 区域(如 oss-cn-hangzhou) |
|
|
51
|
+
| `bucket` | string | 否 | - | Bucket 名称(也可在 path 中指定) |
|
|
52
|
+
| `root` | string | 否 | '/' | 存储根路径 |
|
|
53
|
+
| `urlPrefix` | string | 否 | - | URL 前缀,用于生成访问链接 |
|
|
54
|
+
| `publicRead` | boolean | 否 | false | 是否公共读,控制生成的 URL 是否需要签名 |
|
|
55
|
+
| `internal` | boolean | 否 | false | 是否使用内网访问,与应用在同一区域时更快且免费 |
|
|
56
|
+
| `secure` | boolean | 否 | true | 是否使用 HTTPS |
|
|
57
|
+
| `timeout` | number | 否 | - | 请求超时时间(毫秒) |
|
|
58
|
+
| `accountId` | string | 否 | - | STS 角色扮演:阿里云账号 ID |
|
|
59
|
+
| `roleName` | string | 否 | - | STS 角色扮演:角色名称 |
|
|
60
|
+
| `callbackUrl` | string | 否 | - | 上传回调 URL,上传完成后 OSS 会调用此接口 |
|
|
61
|
+
| `thumbs` | object | 否 | - | 缩略图配置,key 为缩略图名称,value 为 OSS 处理参数 |
|
|
21
62
|
|
|
22
|
-
| 选项 | 类型 | 必须 | 说明 |
|
|
23
|
-
| --------------- | ---------------- | ---- | ---------------------------- |
|
|
24
|
-
| root | string | | 以OSS子目录作为存储根路径,例如 '/uploads' |
|
|
25
|
-
| urlPrefix | string | | URL前缀,用于生成下载链接 |
|
|
26
|
-
| publicRead | boolean | | bucket是否允许公共读 |
|
|
27
|
-
| accessKeyId | string | Yes | OSS访问KEY |
|
|
28
|
-
| accessKeySecret | string | Yes | OSS访问秘钥 |
|
|
29
|
-
| bucket | string | | |
|
|
30
|
-
| region | string | | |
|
|
31
|
-
| internal | boolean | | |
|
|
32
|
-
| secure | boolean | | |
|
|
33
|
-
| timeout | string \| number | | |
|
|
34
|
-
| accountId | string | | 阿里云账号,用于边缘上传生成STS角色扮演令牌 |
|
|
35
|
-
| roleName | string | | 阿里云角色名,用于边缘上传生成STS角色扮演令牌 |
|
|
36
|
-
| callbackUrl | string | | 边缘上传后的回调地址 |
|
|
37
63
|
|
|
38
|
-
|
|
64
|
+
## License
|
|
39
65
|
|
|
66
|
+
MIT
|
package/index.d.ts
CHANGED
|
@@ -1,78 +1,525 @@
|
|
|
1
1
|
import { Adapter } from 'fsd';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* OSSAdapter 配置选项
|
|
5
|
+
*
|
|
6
|
+
* 阿里云 OSS 对象存储适配器的初始化配置。
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* const adapter = new OSSAdapter({
|
|
11
|
+
* accessKeyId: 'your-access-key-id',
|
|
12
|
+
* accessKeySecret: 'your-access-key-secret',
|
|
13
|
+
* region: 'oss-cn-hangzhou',
|
|
14
|
+
* bucket: 'my-bucket'
|
|
15
|
+
* });
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
3
18
|
export interface OSSAdapterOptions {
|
|
19
|
+
/**
|
|
20
|
+
* OSS 存储根路径(可选)
|
|
21
|
+
*
|
|
22
|
+
* 以 OSS 子目录作为存储根路径。
|
|
23
|
+
* 所有文件操作都会在此路径下进行。
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* root: '/uploads'
|
|
28
|
+
* // 文件路径: '/uploads/file.txt'
|
|
29
|
+
* // OSS 实际路径: 'uploads/file.txt'
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
4
32
|
root?: string;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* URL 前缀(可选)
|
|
36
|
+
*
|
|
37
|
+
* 用于生成访问链接时添加前缀。
|
|
38
|
+
* 通常配合 CDN 或反向代理使用。
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* urlPrefix: 'https://cdn.example.com',
|
|
43
|
+
* // file.createUrl() 返回: https://cdn.example.com/uploads/file.txt
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
5
46
|
urlPrefix?: string;
|
|
47
|
+
|
|
6
48
|
/**
|
|
7
|
-
*
|
|
49
|
+
* 是否公共读(可选)
|
|
50
|
+
*
|
|
51
|
+
* 控制生成的 URL 是否需要签名访问。
|
|
52
|
+
*
|
|
53
|
+
* - `true`: 公共 Bucket,生成直接访问 URL(无需签名)
|
|
54
|
+
* - `false`: 私有 Bucket,生成带签名的临时 URL(默认值)
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* // 公共访问
|
|
59
|
+
* publicRead: true,
|
|
60
|
+
* // URL: https://bucket.oss-cn-hangzhou.aliyuncs.com/file.jpg
|
|
61
|
+
*
|
|
62
|
+
* // 私有访问
|
|
63
|
+
* publicRead: false,
|
|
64
|
+
* // URL: https://bucket.oss-cn-hangzhou.aliyuncs.com/file.jpg?OSSAccessKeyId=...&Expires=...
|
|
65
|
+
* ```
|
|
8
66
|
*/
|
|
9
67
|
publicRead?: boolean;
|
|
10
|
-
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* OSS 访问 Key ID(必需)
|
|
71
|
+
*
|
|
72
|
+
* 阿里云 OSS 的 Access Key ID。
|
|
73
|
+
*
|
|
74
|
+
* @example
|
|
75
|
+
* ```typescript
|
|
76
|
+
* accessKeyId: 'LTAI5txxxxxxxxxxxxx'
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
11
79
|
accessKeyId: string;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* OSS 访问 Key Secret(必需)
|
|
83
|
+
*
|
|
84
|
+
* 阿里云 OSS 的 Access Key Secret。
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* accessKeySecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* 请妥善保管此密钥,不要提交到代码仓库。
|
|
93
|
+
* 建议使用环境变量存储。
|
|
94
|
+
*/
|
|
12
95
|
accessKeySecret: string;
|
|
13
|
-
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* OSS Bucket 名称(可选)
|
|
99
|
+
*
|
|
100
|
+
* 如果不指定,需要在上传时在 path 中包含 bucket 名称。
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* bucket: 'my-bucket',
|
|
105
|
+
* // 文件路径: '/uploads/file.txt'
|
|
106
|
+
* // OSS 实际路径: 'my-bucket/uploads/file.txt'
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
bucket?: string;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* OSS 区域代码(必需)
|
|
113
|
+
*
|
|
114
|
+
* 阿里云 OSS 的区域代码,如 'oss-cn-hangzhou'。
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```typescript
|
|
118
|
+
* region: 'oss-cn-hangzhou', // 华东1(杭州)
|
|
119
|
+
* region: 'oss-cn-shanghai', // 华东2(上海)
|
|
120
|
+
* region: 'oss-us-west-1' // 美西1(硅谷)
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @remarks
|
|
124
|
+
* 常见区域代码:
|
|
125
|
+
* - oss-cn-hangzhou: 华东1(杭州)
|
|
126
|
+
* - oss-cn-shanghai: 华东2(上海)
|
|
127
|
+
* - oss-cn-beijing: 华北2(北京)
|
|
128
|
+
* - oss-cn-shenzhen: 华南1(深圳)
|
|
129
|
+
* - oss-cn-hongkong: 香港
|
|
130
|
+
* - oss-us-west-1: 美西1(硅谷)
|
|
131
|
+
*/
|
|
14
132
|
region: string;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 是否内网访问(可选)
|
|
136
|
+
*
|
|
137
|
+
* - `true`: 使用内网地址(更快且免费)
|
|
138
|
+
* - `false`: 使用公网地址(默认值)
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```typescript
|
|
142
|
+
* // 如果应用和 OSS 在同一区域,使用内网
|
|
143
|
+
* internal: true,
|
|
144
|
+
* // 实际 endpoint: bucket.oss-cn-hangzhou-internal.aliyuncs.com
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
15
147
|
internal?: boolean;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 是否使用 HTTPS(可选)
|
|
151
|
+
*
|
|
152
|
+
* - `true`: 使用 HTTPS(默认值)
|
|
153
|
+
* - `false`: 使用 HTTP
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```typescript
|
|
157
|
+
* secure: true, // https://bucket.oss-cn-hangzhou.aliyuncs.com
|
|
158
|
+
* secure: false, // http://bucket.oss-cn-hangzhou.aliyuncs.com
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
16
161
|
secure?: boolean;
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 请求超时时间(可选)
|
|
165
|
+
*
|
|
166
|
+
* 单位:毫秒(数字)或秒(字符串)
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```typescript
|
|
170
|
+
* timeout: 60000, // 60 秒(毫秒)
|
|
171
|
+
* timeout: '60s' // 60 秒(字符串)
|
|
172
|
+
* timeout: 120000 // 120 秒
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
17
175
|
timeout?: string | number;
|
|
18
|
-
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 阿里云账号 ID(可选)
|
|
179
|
+
*
|
|
180
|
+
* 用于 STS 角色扮演,生成边缘上传的临时凭证。
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* accountId: '1234567890123456',
|
|
185
|
+
* roleName: 'AliyunOSSRole'
|
|
186
|
+
* ```
|
|
187
|
+
*
|
|
188
|
+
* @remarks
|
|
189
|
+
* 当同时提供 `accountId` 和 `roleName` 时,适配器会生成 STS 临时凭证。
|
|
190
|
+
* 临时凭证用于客户端直接上传到 OSS,无需通过服务器中转。
|
|
191
|
+
*/
|
|
19
192
|
accountId?: string;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* 阿里云角色名称(可选)
|
|
196
|
+
*
|
|
197
|
+
* 用于 STS 角色扮演,配合 `accountId` 使用。
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* accountId: '1234567890123456',
|
|
202
|
+
* roleName: 'AliyunOSSRole'
|
|
203
|
+
* ```
|
|
204
|
+
*/
|
|
20
205
|
roleName?: string;
|
|
21
|
-
|
|
206
|
+
|
|
22
207
|
/**
|
|
23
|
-
*
|
|
208
|
+
* 上传回调 URL(可选)
|
|
209
|
+
*
|
|
210
|
+
* 文件上传完成后,OSS 会调用此 URL 通知应用。
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* callbackUrl: 'https://api.example.com/oss/callback'
|
|
215
|
+
* ```
|
|
216
|
+
*
|
|
217
|
+
* @remarks
|
|
218
|
+
* 回调 body 包含:
|
|
219
|
+
* - bucket: Bucket 名称
|
|
220
|
+
* - path: 文件路径
|
|
221
|
+
* - etag: 文件 ETag
|
|
222
|
+
* - size: 文件大小
|
|
223
|
+
* - mimeType: MIME 类型
|
|
224
|
+
* - imageInfo: 图片信息(如果是图片)
|
|
225
|
+
*/
|
|
226
|
+
callbackUrl?: string;
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* 缩略图配置(可选)
|
|
230
|
+
*
|
|
231
|
+
* 配置缩略图规格和样式。
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```typescript
|
|
235
|
+
* thumbs: {
|
|
236
|
+
* 'small': '?x-oss-process=style/small',
|
|
237
|
+
* 'medium': '?x-oss-process=style/medium',
|
|
238
|
+
* 'large': '?x-oss-process=style/large'
|
|
239
|
+
* }
|
|
240
|
+
* ```
|
|
241
|
+
*
|
|
242
|
+
* @remarks
|
|
243
|
+
* 缩略图通过 OSS 图片处理服务生成。
|
|
244
|
+
* 使用时在 `createUrl()` 中指定 thumb 选项。
|
|
24
245
|
*/
|
|
25
246
|
thumbs?: {
|
|
26
|
-
|
|
247
|
+
/**
|
|
248
|
+
* 缩略图规格名
|
|
249
|
+
*
|
|
250
|
+
* 值为 OSS 图片处理参数。
|
|
251
|
+
* 常用:
|
|
252
|
+
* - `?x-oss-process=image/resize,w_100`: 调整宽度到 100px
|
|
253
|
+
* - `?x-oss-process=image/crop,w_100,h_100`: 裁剪 100x100
|
|
254
|
+
* - `?x-oss-process=style/name`: 使用命名样式
|
|
255
|
+
*/
|
|
27
256
|
[name: string]: string;
|
|
28
257
|
};
|
|
29
258
|
}
|
|
30
259
|
|
|
260
|
+
/**
|
|
261
|
+
* 上传凭证
|
|
262
|
+
*
|
|
263
|
+
* 包含临时访问凭证的返回值。
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```typescript
|
|
267
|
+
* {
|
|
268
|
+
* auth: {
|
|
269
|
+
* accessKeyId: 'STS.xxxxxxxxxxxxxx',
|
|
270
|
+
* accessKeySecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
|
271
|
+
* stsToken: 'CAESxxxx...',
|
|
272
|
+
* bucket: 'my-bucket',
|
|
273
|
+
* endpoint: 'oss-cn-hangzhou.aliyuncs.com'
|
|
274
|
+
* },
|
|
275
|
+
* path: '/uploads/file.jpg',
|
|
276
|
+
* expiration: 17001234567890,
|
|
277
|
+
* callback: undefined
|
|
278
|
+
* }
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
31
281
|
export interface UploadToken {
|
|
282
|
+
/**
|
|
283
|
+
* 认证信息
|
|
284
|
+
*
|
|
285
|
+
* 包含临时的访问密钥和 STS Token。
|
|
286
|
+
*/
|
|
32
287
|
auth: {
|
|
288
|
+
/**
|
|
289
|
+
* 临时 Access Key ID
|
|
290
|
+
*/
|
|
33
291
|
accessKeyId: string;
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* 临时 Access Key Secret
|
|
295
|
+
*/
|
|
34
296
|
accessKeySecret: string;
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* STS 临时 Token
|
|
300
|
+
*/
|
|
35
301
|
stsToken: string;
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Bucket 名称
|
|
305
|
+
*/
|
|
36
306
|
bucket: string;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* OSS 访问地址
|
|
310
|
+
*/
|
|
37
311
|
endpoint: string;
|
|
38
312
|
};
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* 上传目标路径
|
|
316
|
+
*
|
|
317
|
+
* 文件在 OSS 中的完整路径。
|
|
318
|
+
*/
|
|
39
319
|
path: string;
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* 凭证过期时间(Unix 时间戳,秒)
|
|
323
|
+
*
|
|
324
|
+
* 过期后凭证将失效。
|
|
325
|
+
*/
|
|
40
326
|
expiration: number;
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 回调数据(可选)
|
|
330
|
+
*
|
|
331
|
+
* 如果配置了 `callbackUrl`,此项存在。
|
|
332
|
+
*/
|
|
41
333
|
callback?: any;
|
|
42
334
|
}
|
|
43
335
|
|
|
336
|
+
/**
|
|
337
|
+
* 带自动刷新的上传凭证
|
|
338
|
+
*
|
|
339
|
+
* 包含自动刷新 STS Token 的上传凭证。
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```typescript
|
|
343
|
+
* {
|
|
344
|
+
* auth: {
|
|
345
|
+
* accessKeyId: 'STS.xxxxxxxxxxxxxx',
|
|
346
|
+
* accessKeySecret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
|
|
347
|
+
* stsToken: 'CAESxxxx...',
|
|
348
|
+
* bucket: 'my-bucket',
|
|
349
|
+
* endpoint: 'oss-cn-hangzhou.aliyuncs.com',
|
|
350
|
+
* refreshSTSToken: [Function: refresh]
|
|
351
|
+
* },
|
|
352
|
+
* path: '/uploads/file.jpg',
|
|
353
|
+
* expiration: 17001234567890
|
|
354
|
+
* }
|
|
355
|
+
* ```
|
|
356
|
+
*
|
|
357
|
+
* @remarks
|
|
358
|
+
* 当凭证即将过期时,调用 `refreshSTSToken()` 获取新的临时凭证。
|
|
359
|
+
*/
|
|
44
360
|
export interface UploadTokenWithAutoRefresh {
|
|
361
|
+
/**
|
|
362
|
+
* 认证信息
|
|
363
|
+
*/
|
|
45
364
|
auth: {
|
|
365
|
+
/**
|
|
366
|
+
* 临时 Access Key ID
|
|
367
|
+
*/
|
|
46
368
|
accessKeyId: string;
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* 临时 Access Key Secret
|
|
372
|
+
*/
|
|
47
373
|
accessKeySecret: string;
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* STS 临时 Token
|
|
377
|
+
*/
|
|
48
378
|
stsToken: string;
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Bucket 名称
|
|
382
|
+
*/
|
|
49
383
|
bucket: string;
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* OSS 访问地址
|
|
387
|
+
*/
|
|
50
388
|
endpoint: string;
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* 自动刷新 STS Token 的函数
|
|
392
|
+
*
|
|
393
|
+
* 调用此函数获取新的临时凭证。
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* ```typescript
|
|
397
|
+
* const newToken = await auth.refreshSTSToken();
|
|
398
|
+
* console.log(newToken.stsToken);
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
51
401
|
refreshSTSToken: () => Promise<{
|
|
52
402
|
accessKeyId: string;
|
|
53
403
|
accessKeySecret: string;
|
|
54
404
|
stsToken: string;
|
|
55
405
|
}>;
|
|
56
406
|
};
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* 上传目标路径
|
|
410
|
+
*/
|
|
57
411
|
path: string;
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* 凭证过期时间
|
|
415
|
+
*/
|
|
58
416
|
expiration: number;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* 回调数据(可选)
|
|
420
|
+
*/
|
|
59
421
|
callback?: any;
|
|
60
422
|
}
|
|
61
423
|
|
|
62
|
-
|
|
424
|
+
/**
|
|
425
|
+
* 阿里云 OSS 适配器
|
|
426
|
+
*
|
|
427
|
+
* 提供对阿里云 OSS 对象存储的访问能力。
|
|
428
|
+
*
|
|
429
|
+
* @remarks
|
|
430
|
+
* ### 核心特性
|
|
431
|
+
* - 完整的文件 CRUD 操作(create, read, update, delete)
|
|
432
|
+
* - 分段上传(大文件优化)
|
|
433
|
+
* - STS 角色扮演(边缘上传)
|
|
434
|
+
* - 上传回调通知
|
|
435
|
+
* - 缩略图支持
|
|
436
|
+
*
|
|
437
|
+
* ### 不支持的操作
|
|
438
|
+
* - `mkdir()` - OSS 无目录概念
|
|
439
|
+
* - `createReadStream({ start })` - 不支持 start 选项
|
|
440
|
+
*
|
|
441
|
+
* ### 已废弃的选项
|
|
442
|
+
* - `endpoint` - 已废弃,使用 `region/[internal]/[secure]` 代替
|
|
443
|
+
*
|
|
444
|
+
* ### 自定义 OSS 客户端
|
|
445
|
+
* - 内部使用 `SimpleOSSClient` 封装阿里云 OSS API
|
|
446
|
+
*
|
|
447
|
+
* ### STS 角色扮演
|
|
448
|
+
* - 配置 `accountId` + `roleName` 时自动启用
|
|
449
|
+
* - 用于客户端直接上传到 OSS(边缘上传)
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* ```typescript
|
|
453
|
+
* import OSSAdapter from 'fsd-oss';
|
|
454
|
+
* import FSD from 'fsd';
|
|
455
|
+
*
|
|
456
|
+
* // 基础配置
|
|
457
|
+
* const adapter = new OSSAdapter({
|
|
458
|
+
* accessKeyId: process.env.OSS_ACCESS_KEY_ID,
|
|
459
|
+
* accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
|
|
460
|
+
* region: process.env.OSS_REGION,
|
|
461
|
+
* bucket: process.env.OSS_BUCKET
|
|
462
|
+
* });
|
|
463
|
+
*
|
|
464
|
+
* const fsd = FSD({ adapter });
|
|
465
|
+
*
|
|
466
|
+
* // 上传文件
|
|
467
|
+
* await fsd('/uploads/file.jpg').write(buffer);
|
|
468
|
+
*
|
|
469
|
+
* // 生成访问 URL
|
|
470
|
+
* const url = await fsd('/uploads/file.jpg').createUrl({ expires: 3600 });
|
|
471
|
+
* ```
|
|
472
|
+
*/
|
|
473
|
+
export default class OSSAdapter extends Adapter<OSSAdapterOptions> {
|
|
63
474
|
/**
|
|
64
475
|
* 创建上传凭证
|
|
65
|
-
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
476
|
+
*
|
|
477
|
+
* 生成用于客户端直接上传到 OSS 的临时凭证。
|
|
478
|
+
*
|
|
479
|
+
* @param path - 文件路径
|
|
480
|
+
* @param meta - 文件元数据(可选)
|
|
481
|
+
* @param durationSeconds - 凭证有效期(秒),默认 3600(1 小时)
|
|
482
|
+
* @returns 上传凭证对象
|
|
483
|
+
*
|
|
484
|
+
* @example
|
|
485
|
+
* ```typescript
|
|
486
|
+
* // 生成 1 小时有效的上传凭证
|
|
487
|
+
* const token = await adapter.createUploadToken(
|
|
488
|
+
* '/uploads/photo.jpg',
|
|
489
|
+
* { contentType: 'image/jpeg' },
|
|
490
|
+
* 3600
|
|
491
|
+
* );
|
|
492
|
+
*
|
|
493
|
+
* // 将 token 发送给前端
|
|
494
|
+
* res.json({ uploadToken: token });
|
|
495
|
+
* ```
|
|
496
|
+
*
|
|
497
|
+
* @remarks
|
|
498
|
+
* 如果配置了 `accountId` 和 `roleName`,会生成 STS 临时凭证。
|
|
68
499
|
*/
|
|
69
500
|
createUploadToken: (path: string, meta?: any, durationSeconds?: number) => Promise<UploadToken>;
|
|
70
501
|
|
|
71
502
|
/**
|
|
72
503
|
* 创建带自动刷新的上传凭证
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
504
|
+
*
|
|
505
|
+
* 生成支持自动刷新 STS Token 的上传凭证。
|
|
506
|
+
*
|
|
507
|
+
* @param path - 文件路径
|
|
508
|
+
* @param meta - 文件元数据(可选)
|
|
509
|
+
* @param durationSeconds - 凭证有效期(秒),默认 3600(1 小时)
|
|
510
|
+
* @returns 带刷新功能的上传凭证对象
|
|
511
|
+
*
|
|
512
|
+
* @example
|
|
513
|
+
* ```typescript
|
|
514
|
+
* const token = await adapter.createUploadTokenWithAutoRefresh(
|
|
515
|
+
* '/uploads/large-video.mp4',
|
|
516
|
+
* { contentType: 'video/mp4' },
|
|
517
|
+
* 3600
|
|
518
|
+
* );
|
|
519
|
+
*
|
|
520
|
+
* // 当凭证快过期时,自动刷新
|
|
521
|
+
* const newAuth = await token.auth.refreshSTSToken();
|
|
522
|
+
* ```
|
|
76
523
|
*/
|
|
77
524
|
createUploadTokenWithAutoRefresh: (
|
|
78
525
|
path: string,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fsd-oss",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Aliyun OSS adapter for fsd",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -9,11 +9,15 @@
|
|
|
9
9
|
"prepublish": "npm run build"
|
|
10
10
|
},
|
|
11
11
|
"repository": "https://github.com/liangxingchen/fsd/tree/master/packages/fsd-oss",
|
|
12
|
-
"author":
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "Liang",
|
|
14
|
+
"email": "liang@miaomo.cn",
|
|
15
|
+
"url": "https://github.com/liangxingchen"
|
|
16
|
+
},
|
|
13
17
|
"license": "MIT",
|
|
14
18
|
"dependencies": {
|
|
15
19
|
"@alicloud/pop-core": "^1.8.0",
|
|
16
|
-
"akita": "^1.
|
|
20
|
+
"akita": "^1.2.0",
|
|
17
21
|
"async": "*",
|
|
18
22
|
"crypto-js": "^4.2.0",
|
|
19
23
|
"debug": "^4.4.3",
|
|
@@ -21,5 +25,6 @@
|
|
|
21
25
|
"minimatch": "^9.0.5",
|
|
22
26
|
"slash": "^3.0.0",
|
|
23
27
|
"xml2js": "^0.6.2"
|
|
24
|
-
}
|
|
28
|
+
},
|
|
29
|
+
"gitHead": "ea754919fb95b49deffd529b5c01c66da0dc08f9"
|
|
25
30
|
}
|