luoyu-oss-unified 0.0.1
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/.prettierrc +4 -0
- package/README.md +496 -0
- package/dist/lib/adapters/aliyun-adapter.d.ts +20 -0
- package/dist/lib/adapters/aliyun-adapter.d.ts.map +1 -0
- package/dist/lib/adapters/aliyun-adapter.js +198 -0
- package/dist/lib/adapters/aliyun-adapter.js.map +1 -0
- package/dist/lib/adapters/base-adapter.d.ts +29 -0
- package/dist/lib/adapters/base-adapter.d.ts.map +1 -0
- package/dist/lib/adapters/base-adapter.js +128 -0
- package/dist/lib/adapters/base-adapter.js.map +1 -0
- package/dist/lib/adapters/minio-adapter.d.ts +18 -0
- package/dist/lib/adapters/minio-adapter.d.ts.map +1 -0
- package/dist/lib/adapters/minio-adapter.js +176 -0
- package/dist/lib/adapters/minio-adapter.js.map +1 -0
- package/dist/lib/adapters/qiniu-adapter.d.ts +17 -0
- package/dist/lib/adapters/qiniu-adapter.d.ts.map +1 -0
- package/dist/lib/adapters/qiniu-adapter.js +234 -0
- package/dist/lib/adapters/qiniu-adapter.js.map +1 -0
- package/dist/lib/adapters/tencent-adapter.d.ts +14 -0
- package/dist/lib/adapters/tencent-adapter.d.ts.map +1 -0
- package/dist/lib/adapters/tencent-adapter.js +178 -0
- package/dist/lib/adapters/tencent-adapter.js.map +1 -0
- package/dist/lib/core/multipart-upload.d.ts +10 -0
- package/dist/lib/core/multipart-upload.d.ts.map +1 -0
- package/dist/lib/core/multipart-upload.js +92 -0
- package/dist/lib/core/multipart-upload.js.map +1 -0
- package/dist/lib/core/oss-client.d.ts +33 -0
- package/dist/lib/core/oss-client.d.ts.map +1 -0
- package/dist/lib/core/oss-client.js +126 -0
- package/dist/lib/core/oss-client.js.map +1 -0
- package/dist/lib/index.d.ts +16 -0
- package/dist/lib/index.d.ts.map +1 -0
- package/dist/lib/index.js +40 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/types/enums.d.ts +18 -0
- package/dist/lib/types/enums.d.ts.map +1 -0
- package/dist/lib/types/enums.js +24 -0
- package/dist/lib/types/enums.js.map +1 -0
- package/dist/lib/types/index.d.ts +95 -0
- package/dist/lib/types/index.d.ts.map +1 -0
- package/dist/lib/types/index.js +6 -0
- package/dist/lib/types/index.js.map +1 -0
- package/dist/lib/utils/index.d.ts +2 -0
- package/dist/lib/utils/index.d.ts.map +1 -0
- package/dist/lib/utils/index.js +6 -0
- package/dist/lib/utils/index.js.map +1 -0
- package/dist/lib/utils/src/dome.d.ts +8 -0
- package/dist/lib/utils/src/dome.d.ts.map +1 -0
- package/dist/lib/utils/src/dome.js +15 -0
- package/dist/lib/utils/src/dome.js.map +1 -0
- package/dist/packages/adapters/aliyun-adapter.d.ts +19 -0
- package/dist/packages/adapters/aliyun-adapter.js +196 -0
- package/dist/packages/adapters/aliyun-adapter.js.map +1 -0
- package/dist/packages/adapters/base-adapter.d.ts +28 -0
- package/dist/packages/adapters/base-adapter.js +118 -0
- package/dist/packages/adapters/base-adapter.js.map +1 -0
- package/dist/packages/adapters/minio-adapter.d.ts +17 -0
- package/dist/packages/adapters/minio-adapter.js +175 -0
- package/dist/packages/adapters/minio-adapter.js.map +1 -0
- package/dist/packages/adapters/qiniu-adapter.d.ts +16 -0
- package/dist/packages/adapters/qiniu-adapter.js +214 -0
- package/dist/packages/adapters/qiniu-adapter.js.map +1 -0
- package/dist/packages/adapters/tencent-adapter.d.ts +13 -0
- package/dist/packages/adapters/tencent-adapter.js +177 -0
- package/dist/packages/adapters/tencent-adapter.js.map +1 -0
- package/dist/packages/core/multipart-upload.d.ts +9 -0
- package/dist/packages/core/multipart-upload.js +86 -0
- package/dist/packages/core/multipart-upload.js.map +1 -0
- package/dist/packages/core/oss-client.d.ts +32 -0
- package/dist/packages/core/oss-client.js +108 -0
- package/dist/packages/core/oss-client.js.map +1 -0
- package/dist/packages/index.d.ts +10 -0
- package/dist/packages/index.js +35 -0
- package/dist/packages/index.js.map +1 -0
- package/dist/packages/types/enums.d.ts +17 -0
- package/dist/packages/types/enums.js +24 -0
- package/dist/packages/types/enums.js.map +1 -0
- package/dist/packages/types/index.d.ts +82 -0
- package/dist/packages/types/index.js +6 -0
- package/dist/packages/types/index.js.map +1 -0
- package/dist/packages/utils/index.d.ts +1 -0
- package/dist/packages/utils/index.js +6 -0
- package/dist/packages/utils/index.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/dist/tsconfig.lib.tsbuildinfo +1 -0
- package/downloaded.jpg +0 -0
- package/eslint.config.mjs +35 -0
- package/nest-cli.json +8 -0
- package/package.json +86 -0
- package/packages/adapters/aliyun-adapter.ts +252 -0
- package/packages/adapters/base-adapter.ts +174 -0
- package/packages/adapters/minio-adapter.ts +186 -0
- package/packages/adapters/qiniu-adapter.ts +247 -0
- package/packages/adapters/tencent-adapter.ts +186 -0
- package/packages/core/multipart-upload.ts +116 -0
- package/packages/core/oss-client.ts +169 -0
- package/packages/index.ts +15 -0
- package/packages/types/enums.ts +19 -0
- package/packages/types/index.ts +102 -0
- package/packages/utils/index.ts +1 -0
- package/src/app.controller.ts +22 -0
- package/src/app.module.ts +10 -0
- package/src/app.service.ts +8 -0
- package/src/main.ts +8 -0
- package/src/test.oss.ts +116 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +32 -0
- package/tsconfig.lib.json +27 -0
- package/upload/large.mp4 +0 -0
- package/upload//344/270/213/350/275/275 copy 2.jpg +0 -0
- package/upload//344/270/213/350/275/275 copy.jpg +0 -0
- package/upload//344/270/213/350/275/275.jpg +0 -0
- package//346/211/223/345/214/205.md +30 -0
package/.prettierrc
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
# luoyu-oss-unified - 统一对象存储客户端
|
|
2
|
+
|
|
3
|
+
一个统一的多平台对象存储客户端,支持 MinIO、阿里云、腾讯云、七牛云等多种对象存储服务。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🚀 统一的 API 接口,支持多种对象存储服务
|
|
8
|
+
- 📦 轻量级设计,易于集成
|
|
9
|
+
- 🔧 支持上传、下载、删除、列表、目录操作等功能
|
|
10
|
+
- 🔄 支持分片上传和断点续传
|
|
11
|
+
- 🛡️ TypeScript 支持,类型安全
|
|
12
|
+
- ⚡ 事件监听机制,支持进度监控和错误处理
|
|
13
|
+
|
|
14
|
+
## 安装
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install luoyu-oss-unified
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## 快速开始
|
|
21
|
+
|
|
22
|
+
### 基本使用
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { OSSClient, OSSProvider } from 'luoyu-oss-unified';
|
|
26
|
+
|
|
27
|
+
// MinIO 配置
|
|
28
|
+
const minioConfig = {
|
|
29
|
+
provider: OSSProvider.MINIO,
|
|
30
|
+
endpoint: 'http://your-minio-server.com',
|
|
31
|
+
accessKeyId: 'your-access-key',
|
|
32
|
+
accessKeySecret: 'your-secret-key',
|
|
33
|
+
bucket: 'your-bucket',
|
|
34
|
+
region: 'us-east-1'
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// 创建客户端
|
|
38
|
+
const ossClient = new OSSClient(minioConfig);
|
|
39
|
+
|
|
40
|
+
// 上传文件
|
|
41
|
+
await ossClient.upload({
|
|
42
|
+
path: 'images/photo.jpg',
|
|
43
|
+
file: fs.readFileSync('./photo.jpg'),
|
|
44
|
+
contentType: 'image/jpeg'
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### 七牛云配置示例
|
|
49
|
+
|
|
50
|
+
```typescript
|
|
51
|
+
const qiniuConfig = {
|
|
52
|
+
provider: OSSProvider.QINIU,
|
|
53
|
+
accessKeyId: 'your-access-key',
|
|
54
|
+
accessKeySecret: 'your-secret-key',
|
|
55
|
+
bucket: 'your-bucket',
|
|
56
|
+
region: 'z0', // z0: 华东, z1: 华北, z2: 华南, na0: 北美, as0: 新加坡
|
|
57
|
+
domain: 'your-domain.qiniudn.com'
|
|
58
|
+
};
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## API 文档
|
|
62
|
+
|
|
63
|
+
### OSSClient
|
|
64
|
+
|
|
65
|
+
#### 构造函数
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
new OSSClient(config: OSSConfig)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
**参数:**
|
|
72
|
+
|
|
73
|
+
- `config` - 配置对象
|
|
74
|
+
|
|
75
|
+
**配置选项:**
|
|
76
|
+
|
|
77
|
+
- [provider](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L3-L3) - 存储提供商 ([MINIO](file://e:\project\code\npm\ts-project\src\packages\types\enums.ts#L4-L4), `ALIBABA`, [TENCENT](file://e:\project\code\npm\ts-project\src\packages\types\enums.ts#L2-L2), [QINIU](file://e:\project\code\npm\ts-project\src\packages\types\enums.ts#L3-L3))
|
|
78
|
+
- [endpoint](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L8-L8) - 服务端点
|
|
79
|
+
- [accessKeyId](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L4-L4) - 访问密钥 ID
|
|
80
|
+
- [accessKeySecret](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L5-L5) - 访问密钥
|
|
81
|
+
- [bucket](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L6-L6) - 存储桶名称
|
|
82
|
+
- [region](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L7-L7) - 区域
|
|
83
|
+
- [secure](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L10-L10) - 是否使用 HTTPS (默认: true)
|
|
84
|
+
- [domain](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L9-L9) - 自定义域名 (可选)
|
|
85
|
+
|
|
86
|
+
#### 上传文件
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
await ossClient.upload(options: UploadOptions)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**参数:**
|
|
93
|
+
|
|
94
|
+
- [path](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L16-L16) - 文件路径
|
|
95
|
+
- [file](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L17-L17) - 文件内容 (Buffer | string | ReadableStream)
|
|
96
|
+
- [contentType](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L72-L72) - 内容类型 (可选)
|
|
97
|
+
- [mode](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L22-L22) - 上传模式 (普通/分片)
|
|
98
|
+
- [partSize](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L23-L23) - 分片大小 (分片上传时使用)
|
|
99
|
+
- [progressCallback](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L21-L21) - 进度回调函数
|
|
100
|
+
|
|
101
|
+
**示例:**
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
await ossClient.upload({
|
|
105
|
+
path: 'files/document.pdf',
|
|
106
|
+
file: fs.readFileSync('./document.pdf'),
|
|
107
|
+
contentType: 'application/pdf',
|
|
108
|
+
progressCallback: (progress) => {
|
|
109
|
+
console.log(`上传进度: ${progress.percent}%`);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### 下载文件
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const buffer = await ossClient.download(options: DownloadOptions)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
**参数:**
|
|
121
|
+
|
|
122
|
+
- [path](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L16-L16) - 文件路径
|
|
123
|
+
- [destination](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L36-L36) - 本地保存路径 (可选)
|
|
124
|
+
|
|
125
|
+
**示例:**
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
const fileBuffer = await ossClient.download({
|
|
129
|
+
path: 'images/photo.jpg'
|
|
130
|
+
});
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
#### 删除文件
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
await ossClient.delete(path: string)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**示例:**
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
await ossClient.delete('images/photo.jpg');
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### 列出文件
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
const result = await ossClient.list(options: ListOptions)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
**参数:**
|
|
152
|
+
|
|
153
|
+
- [prefix](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L52-L52) - 前缀过滤 (可选)
|
|
154
|
+
- [limit](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L60-L60) - 返回数量限制 (可选)
|
|
155
|
+
- [delimiter](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L56-L56) - 分隔符 (可选)
|
|
156
|
+
- [marker](file://e:\project\code\npm\ts-project\src\packages\types\index.ts#L64-L64) - 分页标记 (可选)
|
|
157
|
+
|
|
158
|
+
**示例:**
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
const result = await ossClient.list({
|
|
162
|
+
prefix: 'images/',
|
|
163
|
+
limit: 10
|
|
164
|
+
});
|
|
165
|
+
console.log(result.objects); // 文件列表
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### 创建目录
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
await ossClient.createDirectory(path: string)
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
**示例:**
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
await ossClient.createDirectory('documents/');
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
#### 列出目录
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
const directories = await ossClient.listDirectories(prefix?: string)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
**示例:**
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
const dirs = await ossClient.listDirectories('images/');
|
|
190
|
+
console.log(dirs); // 目录列表
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
#### 获取签名 URL
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
const url = await ossClient.getSignedUrl(path: string, expires: number)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
**参数:**
|
|
200
|
+
|
|
201
|
+
- `path` - 文件路径
|
|
202
|
+
- `expires` - 过期时间 (秒,默认: 3600)
|
|
203
|
+
|
|
204
|
+
**示例:**
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
const url = await ossClient.getSignedUrl('images/photo.jpg', 3600);
|
|
208
|
+
console.log(url); // 可访问的临时链接
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
#### 批量操作
|
|
212
|
+
|
|
213
|
+
##### 批量上传
|
|
214
|
+
|
|
215
|
+
```typescript
|
|
216
|
+
const results = await ossClient.batchUpload(uploads: UploadOptions[])
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
**示例:**
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
const results = await ossClient.batchUpload([
|
|
223
|
+
{ path: 'images/1.jpg', file: fs.readFileSync('./1.jpg') },
|
|
224
|
+
{ path: 'images/2.jpg', file: fs.readFileSync('./2.jpg') }
|
|
225
|
+
]);
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
##### 批量删除
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
await ossClient.batchDelete(paths: string[])
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
**示例:**
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
await ossClient.batchDelete(['images/1.jpg', 'images/2.jpg']);
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## 高级功能扩展
|
|
241
|
+
|
|
242
|
+
### 事件监听器
|
|
243
|
+
|
|
244
|
+
```typescript
|
|
245
|
+
import { OSSClient, UploadOptions } from 'luoyu-oss-unified';
|
|
246
|
+
|
|
247
|
+
class EnhancedOSSClient extends OSSClient {
|
|
248
|
+
private eventListeners: Map<string, Function[]> = new Map();
|
|
249
|
+
|
|
250
|
+
on(event: string, listener: Function) {
|
|
251
|
+
if (!this.eventListeners.has(event)) {
|
|
252
|
+
this.eventListeners.set(event, []);
|
|
253
|
+
}
|
|
254
|
+
this.eventListeners.get(event)!.push(listener);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
emit(event: string, ...args: any[]) {
|
|
258
|
+
const listeners = this.eventListeners.get(event) || [];
|
|
259
|
+
listeners.forEach(listener => listener(...args));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
async uploadWithEvents(options: UploadOptions) {
|
|
263
|
+
this.emit('upload:start', options);
|
|
264
|
+
try {
|
|
265
|
+
const result = await super.upload(options);
|
|
266
|
+
this.emit('upload:complete', result);
|
|
267
|
+
return result;
|
|
268
|
+
} catch (error) {
|
|
269
|
+
this.emit('upload:error', error);
|
|
270
|
+
throw error;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// 使用示例
|
|
276
|
+
const enhancedClient = new EnhancedOSSClient(minioConfig);
|
|
277
|
+
|
|
278
|
+
enhancedClient.on('upload:start', (options) => {
|
|
279
|
+
console.log('上传开始:', options.path);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
enhancedClient.on('upload:complete', (result) => {
|
|
283
|
+
console.log('上传完成:', result);
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
enhancedClient.on('upload:error', (error) => {
|
|
287
|
+
console.error('上传错误:', error);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
// 触发带事件的上传
|
|
291
|
+
await enhancedClient.uploadWithEvents({
|
|
292
|
+
path: 'files/document.pdf',
|
|
293
|
+
file: fs.readFileSync('./document.pdf')
|
|
294
|
+
});
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### 分片上传
|
|
298
|
+
|
|
299
|
+
对于大文件,推荐使用分片上传:
|
|
300
|
+
|
|
301
|
+
```typescript
|
|
302
|
+
await ossClient.upload({
|
|
303
|
+
path: 'videos/large-video.mp4',
|
|
304
|
+
file: fs.readFileSync('./large-video.mp4'),
|
|
305
|
+
mode: UploadMode.MULTIPART,
|
|
306
|
+
partSize: 10 * 1024 * 1024, // 10MB 每片
|
|
307
|
+
progressCallback: (progress) => {
|
|
308
|
+
console.log(`Upload progress: ${progress.percent}%`);
|
|
309
|
+
}
|
|
310
|
+
});
|
|
311
|
+
```
|
|
312
|
+
|
|
313
|
+
### 存在性检查
|
|
314
|
+
|
|
315
|
+
```typescript
|
|
316
|
+
const exists = await ossClient.exists('images/photo.jpg');
|
|
317
|
+
console.log(exists); // true/false
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
### 获取文件信息
|
|
321
|
+
|
|
322
|
+
```typescript
|
|
323
|
+
const fileInfo = await ossClient.head('images/photo.jpg');
|
|
324
|
+
if (fileInfo) {
|
|
325
|
+
console.log(fileInfo.size); // 文件大小
|
|
326
|
+
console.log(fileInfo.lastModified); // 最后修改时间
|
|
327
|
+
}
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
## 支持的提供商
|
|
331
|
+
|
|
332
|
+
- ✅ **MinIO** - 高性能对象存储
|
|
333
|
+
- ✅ **七牛云** - 国内主流 CDN 服务商
|
|
334
|
+
- ⏳ **阿里云 OSS** - 企业级对象存储服务
|
|
335
|
+
- ⏳ **腾讯云 COS** - 腾讯云对象存储
|
|
336
|
+
|
|
337
|
+
## 环境要求
|
|
338
|
+
|
|
339
|
+
- Node.js >= 14.0.0
|
|
340
|
+
- TypeScript >= 4.0.0 (可选,但推荐)
|
|
341
|
+
|
|
342
|
+
## 完整测试代码示例
|
|
343
|
+
|
|
344
|
+
```typescript
|
|
345
|
+
import { OSSClient, OSSConfig, OSSProvider, UploadMode } from 'luoyu-oss-unified';
|
|
346
|
+
import fs from 'fs';
|
|
347
|
+
import path from 'path';
|
|
348
|
+
|
|
349
|
+
// MinIO 配置
|
|
350
|
+
const minioConfig = {
|
|
351
|
+
provider: OSSProvider.MINIO,
|
|
352
|
+
endpoint: 'http://your-minio-server.com',
|
|
353
|
+
accessKeyId: 'your-access-key',
|
|
354
|
+
accessKeySecret: 'your-secret-key',
|
|
355
|
+
bucket: 'your-bucket',
|
|
356
|
+
region: 'us-east-1'
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
// 七牛云配置
|
|
360
|
+
const qiniuConfig: OSSConfig = {
|
|
361
|
+
provider: OSSProvider.QINIU,
|
|
362
|
+
accessKeyId: 'your-access-key',
|
|
363
|
+
accessKeySecret: 'your-secret-key',
|
|
364
|
+
bucket: 'your-bucket',
|
|
365
|
+
region: 'z0',
|
|
366
|
+
domain: 'your-domain.qiniudn.com'
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
// 创建客户端
|
|
370
|
+
const ossClient = new OSSClient(minioConfig);
|
|
371
|
+
|
|
372
|
+
// 上传文件
|
|
373
|
+
export async function uploadFile() {
|
|
374
|
+
const filePath = path.join(__dirname, '../upload/file.jpg');
|
|
375
|
+
if (!fs.existsSync(filePath)) {
|
|
376
|
+
throw new Error(`File does not exist: ${filePath}`);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const result = await ossClient.upload({
|
|
380
|
+
path: 'images/test.jpg',
|
|
381
|
+
file: fs.readFileSync(filePath),
|
|
382
|
+
contentType: 'image/jpeg',
|
|
383
|
+
progressCallback: (progress) => {
|
|
384
|
+
console.log(`Upload progress: ${progress.percent}%`);
|
|
385
|
+
}
|
|
386
|
+
});
|
|
387
|
+
console.log('File uploaded:', result);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// 下载文件
|
|
391
|
+
export async function downloadFile() {
|
|
392
|
+
const buffer = await ossClient.download({
|
|
393
|
+
path: 'images/test.jpg'
|
|
394
|
+
});
|
|
395
|
+
return buffer;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// 列出文件
|
|
399
|
+
export async function listFiles() {
|
|
400
|
+
const result = await ossClient.list({
|
|
401
|
+
prefix: 'images/',
|
|
402
|
+
limit: 10
|
|
403
|
+
});
|
|
404
|
+
console.log('Files:', result.objects);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
// 列出目录
|
|
408
|
+
export async function listDirectories() {
|
|
409
|
+
const dirs = await ossClient.listDirectories('images/');
|
|
410
|
+
console.log('Directories:', dirs);
|
|
411
|
+
return dirs;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// 创建目录
|
|
415
|
+
export async function createDir() {
|
|
416
|
+
await ossClient.createDirectory('documents/');
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// 上传大文件
|
|
420
|
+
export async function uploadLargeFile() {
|
|
421
|
+
const filePath = path.join(__dirname, '../upload/large-file.mp4');
|
|
422
|
+
if (!fs.existsSync(filePath)) {
|
|
423
|
+
throw new Error(`File does not exist: ${filePath}`);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
const result = await ossClient.upload({
|
|
427
|
+
path: 'videos/large.mp4',
|
|
428
|
+
file: fs.readFileSync(filePath),
|
|
429
|
+
mode: UploadMode.MULTIPART,
|
|
430
|
+
partSize: 10 * 1024 * 1024,
|
|
431
|
+
progressCallback: (progress) => {
|
|
432
|
+
console.log(`Upload progress: ${progress.percent}%`);
|
|
433
|
+
}
|
|
434
|
+
});
|
|
435
|
+
console.log('Large file uploaded:', result);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// 获取签名 URL
|
|
439
|
+
export async function getPresignedUrl() {
|
|
440
|
+
const url = await ossClient.getSignedUrl('images/test.jpg', 3600);
|
|
441
|
+
console.log('Presigned URL:', url);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// 批量操作
|
|
445
|
+
export async function batchOperations() {
|
|
446
|
+
const uploadResults = await ossClient.batchUpload([
|
|
447
|
+
{ path: 'images/1.jpg', file: fs.readFileSync(path.join(__dirname, '../upload/image1.jpg')) },
|
|
448
|
+
{ path: 'images/2.jpg', file: fs.readFileSync(path.join(__dirname, '../upload/image2.jpg')) }
|
|
449
|
+
]);
|
|
450
|
+
console.log('Upload results:', uploadResults);
|
|
451
|
+
|
|
452
|
+
// 批量删除
|
|
453
|
+
await ossClient.batchDelete(['images/1.jpg', 'images/2.jpg']);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// 带事件监听的高级客户端示例
|
|
457
|
+
class EnhancedOSSClient extends OSSClient {
|
|
458
|
+
private eventListeners: Map<string, Function[]> = new Map();
|
|
459
|
+
|
|
460
|
+
on(event: string, listener: Function) {
|
|
461
|
+
if (!this.eventListeners.has(event)) {
|
|
462
|
+
this.eventListeners.set(event, []);
|
|
463
|
+
}
|
|
464
|
+
this.eventListeners.get(event)!.push(listener);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
emit(event: string, ...args: any[]) {
|
|
468
|
+
const listeners = this.eventListeners.get(event) || [];
|
|
469
|
+
listeners.forEach(listener => listener(...args));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
async uploadWithEvents(options: UploadOptions) {
|
|
473
|
+
this.emit('upload:start', options);
|
|
474
|
+
try {
|
|
475
|
+
const result = await super.upload(options);
|
|
476
|
+
this.emit('upload:complete', result);
|
|
477
|
+
return result;
|
|
478
|
+
} catch (error) {
|
|
479
|
+
this.emit('upload:error', error);
|
|
480
|
+
throw error;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
```
|
|
485
|
+
|
|
486
|
+
## 贡献
|
|
487
|
+
|
|
488
|
+
欢迎提交 Issue 和 Pull Request!
|
|
489
|
+
|
|
490
|
+
## 许可证
|
|
491
|
+
|
|
492
|
+
MIT License
|
|
493
|
+
|
|
494
|
+
---
|
|
495
|
+
|
|
496
|
+
这个 npm 插件提供了统一的 API 来操作不同的对象存储服务,让开发者可以轻松切换存储提供商而无需更改业务代码。通过事件监听器机制,还可以实现更精细的进度监控和错误处理。
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { BaseOSSAdapter } from './base-adapter';
|
|
2
|
+
import { type OSSConfig, type UploadOptions, type DownloadOptions, type ListOptions, type ListResult, type OSSObject, type MultipartUploadInfo, type PartInfo } from '../types';
|
|
3
|
+
export declare class AliyunOSSAdapter extends BaseOSSAdapter {
|
|
4
|
+
private client;
|
|
5
|
+
constructor(config: OSSConfig);
|
|
6
|
+
upload(options: UploadOptions): Promise<string>;
|
|
7
|
+
download(options: DownloadOptions): Promise<Buffer>;
|
|
8
|
+
delete(path: string): Promise<void>;
|
|
9
|
+
list(options?: ListOptions): Promise<ListResult>;
|
|
10
|
+
head(path: string): Promise<OSSObject | null>;
|
|
11
|
+
exists(path: string): Promise<boolean>;
|
|
12
|
+
getSignedUrl(path: string, expires?: number): Promise<string>;
|
|
13
|
+
initiateMultipartUpload(path: string, partSize?: number): Promise<MultipartUploadInfo>;
|
|
14
|
+
uploadPart(uploadInfo: MultipartUploadInfo, partNumber: number, data: Buffer, start: number, end: number): Promise<PartInfo>;
|
|
15
|
+
completeMultipartUpload(uploadInfo: MultipartUploadInfo, parts: PartInfo[]): Promise<string>;
|
|
16
|
+
abortMultipartUpload(uploadInfo: MultipartUploadInfo): Promise<void>;
|
|
17
|
+
listParts(uploadInfo: MultipartUploadInfo): Promise<PartInfo[]>;
|
|
18
|
+
private uploadMultipartParts;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=aliyun-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aliyun-adapter.d.ts","sourceRoot":"","sources":["../../../packages/adapters/aliyun-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EACH,KAAK,SAAS,EACd,KAAK,aAAa,EAClB,KAAK,eAAe,EACpB,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,mBAAmB,EACxB,KAAK,QAAQ,EAEhB,MAAM,UAAU,CAAC;AAElB,qBAAa,gBAAiB,SAAQ,cAAc;IAChD,OAAO,CAAC,MAAM,CAAM;gBAER,MAAM,EAAE,SAAS;IAavB,MAAM,CAAC,OAAO,EAAE,aAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAuC/C,QAAQ,CAAC,OAAO,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC;IAcnD,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAInC,IAAI,CAAC,OAAO,GAAE,WAAgB,GAAG,OAAO,CAAC,UAAU,CAAC;IAuBpD,IAAI,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;IAkB7C,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAKtC,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,MAAa,GAAG,OAAO,CAAC,MAAM,CAAC;IAInE,uBAAuB,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,MAAwB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IASvG,UAAU,CACZ,UAAU,EAAE,mBAAmB,EAC/B,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAC,MAAM,EACZ,GAAG,EAAC,MAAM,GACX,OAAO,CAAC,QAAQ,CAAC;IAiBd,uBAAuB,CACzB,UAAU,EAAE,mBAAmB,EAC/B,KAAK,EAAE,QAAQ,EAAE,GAClB,OAAO,CAAC,MAAM,CAAC;IAmBZ,oBAAoB,CAAC,UAAU,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAOpE,SAAS,CAAC,UAAU,EAAE,mBAAmB,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;YAavD,oBAAoB;CAwCrC"}
|