@ducktors/ storagebus-抽象
存储巴士存储适配器的抽象基类. 这个包为创建与Surverbus生态系统合作的存储适配器提供了基础.
安装
npm install @ducktors/storagebus-abstract
# or
pnpm add @ducktors/storagebus-abstract
# or
yarn add @ducktors/storagebus-abstract
使用量
本套件拟用作执行自定义存储适配器的基类. 如果您想直接使用存储溶液,请查看可用的适配器:
- @ducktors/ storagebus - 当地 - 本地文件系统存储
- @ducktors/ streagebus-s3 (英语) - AWS S3存储器
- @ducktors/ staragebus-gcs (英语) - 谷歌云存储
- @ducktors/ streagebus-memory (英语) - 记忆存储
创建自定义适配器
import { Readable } from 'node:stream'
import { Storage, AbstractStorageOptions } from '@ducktors/storagebus-abstract'
interface MyStorageOptions extends AbstractStorageOptions {
// Add your custom options here
}
class MyStorage extends Storage {
constructor(opts: MyStorageOptions) {
super(opts)
}
async write(fileName: string, fileReadable: Readable): Promise<string> {
// Implement write logic
}
async exists(fileName: string): Promise<boolean> {
// Implement exists logic
}
async read(fileName: string): Promise<Readable> {
// Implement read logic
}
async remove(fileName: string): Promise<void> {
// Implement remove logic
}
async copy(key: string, destKey: string): Promise<string> {
// Implement copy logic
}
async move(key: string, destKey: string): Promise<string> {
// Implement move logic
}
}
API 密码
构造选项
| 选项 | 类型 | 默认 | 说明 |
|---|---|---|---|
debug | boolean | false | 启用调试日志 |
logger | Logger | defaultLogger | 自定义日志实例 |
sanitizeKey | boolean | (key: string) => string | false | 将文件密钥设置为安全 。 何时 true,使用内置消毒器. 也可以是自定义函数 |
摘要方法
所有存储适配器必须执行这些方法:
| 方法 | 签署 | 说明 |
|---|---|---|
write | (fileName: string, fileReadable: Readable) => Promise<string> | 写入要存储的文件 |
exists | (fileName: string) => Promise<boolean> | 检查文件是否存在 |
read | (fileName: string) => Promise<Readable> | 从存储中读取文件 |
remove | (fileName: string) => Promise<void> | 从存储中删除文件 |
copy | (key: string, destKey: string) => Promise<string> | 在存储中复制文件 |
move | (key: string, destKey: string) => Promise<string> | 在存储中移动文件 |
帮助方法
| 方法 | 签署 | 说明 |
|---|---|---|
toBuffer | (readableStream: Readable) => Promise<Buffer> | 将可读流转换为缓冲 |
许可证
麻省理工学院