@ducktors/ storagebus - 当地
本地文件系统存储适配器 存储总线。 。 。 .
安装
npm install @ducktors/storagebus-local
# or
pnpm add @ducktors/storagebus-local
# or
yarn add @ducktors/storagebus-local
使用量
import { Storage } from '@ducktors/storagebus-local'
import { Readable } from 'node:stream'
// Using an absolute path
const storage = new Storage({
rootFolder: '/path/to/storage',
})
// Using a relative path (will be placed in system temp directory)
const storageTmp = new Storage({
rootFolder: 'my-storage',
})
// Alternative: use 'bucket' instead of 'rootFolder'
const storageAlt = new Storage({
bucket: '/path/to/storage',
})
async function main() {
const readable = Readable.from('Hello, world!')
// Write a file
const writtenFile = await storage.write('path/to/file.txt', readable)
// Check if file exists
const exists = await storage.exists('path/to/file.txt')
// Read a file
const fileStream = await storage.read('path/to/file.txt')
// Copy a file
const copiedFile = await storage.copy('path/to/file.txt', 'path/to/copy.txt')
// Move a file
const movedFile = await storage.move('path/to/copy.txt', 'new/path/file.txt')
// Delete a file
await storage.remove('path/to/file.txt')
}
API 密码
构造选项
你必须提供其中之一 rootFolder 或者说 bucket数字 :
| 选项 | 类型 | 需求 | 说明 |
|---|---|---|---|
rootFolder | string | 对* | 文件存储根目录 |
bucket | string | 对* | 别名 rootFolder |
debug | boolean | 没有 | 启用调试日志 |
logger | Logger | 没有 | 自定义日志实例 |
sanitizeKey | boolean | (key: string) => string | 没有 | 将文件密钥设置为安全 |
*以太过 rootFolder 或者说 bucket 必须提供,但不能同时提供.
方法
| 方法 | 签署 | 说明 |
|---|---|---|
write | (filePath: string, fileReadable: Readable) => Promise<string> | 写一个文件到存储 。 自动创建子目录 |
exists | (filePath: string) => Promise<boolean> | 检查文件是否存在 |
read | (filePath: string) => Promise<Readable> | 从存储中读取文件 |
remove | (filePath: string) => Promise<void> | 从存储中删除文件 |
copy | (filePath: string, destFilePath: string) => Promise<string> | 复制一个文件。 自动创建目标子目录 |
move | (filePath: string, destFilePath: string) => Promise<string> | 移动一个文件。 自动处理交叉设备 |
路径分辨率
- 绝对路径: 现用
- 相对路径: 放在系统临时目录内(
os.tmpdir()(中文(简体) )
如果根目录不存在,则自动创建.
许可证
麻省理工学院