@ducktors/ staragebus-gcs (英语)
Google 云存储适配器 存储总线。 。 。 .
安装
npm install @ducktors/storagebus-gcs
# or
pnpm add @ducktors/storagebus-gcs
# or
yarn add @ducktors/storagebus-gcs
使用量
import { Storage } from '@ducktors/storagebus-gcs'
import { Readable } from 'node:stream'
// Using explicit credentials
const storage = new Storage({
bucket: 'your-gcs-bucket',
projectId: 'your-gcp-project-id',
clientEmail: 'your-gcp-client-email',
privateKey: 'your-gcp-private-key',
})
// Or using Application Default Credentials (ADC)
const storageWithADC = new Storage({
bucket: 'your-gcs-bucket',
})
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 密码
构造选项
| 选项 | 类型 | 需求 | 说明 |
|---|---|---|---|
bucket | string | 对 | GCS 桶名 |
projectId | string | 没有 | GCP 项目 ID (如果使用 ADC 可选择) |
clientEmail | string | 没有 | GCP 服务账户客户端电子邮件( 如果使用 ADC 可选择) |
privateKey | string | 没有 | GCP 服务账户私有密钥( 如果使用 ADC 可选择) |
debug | boolean | 没有 | 启用调试日志 |
logger | Logger | 没有 | 自定义日志实例 |
sanitizeKey | boolean | (key: string) => string | 没有 | 将文件密钥设置为安全 |
方法
| 方法 | 签署 | 说明 |
|---|---|---|
write | (key: string, fileReadable: Readable) => Promise<string> | 写一个文件到存储 。 根据文件扩展名自动设置内容类型 |
exists | (key: string) => Promise<boolean> | 检查文件是否存在 |
read | (key: string) => Promise<Readable> | 从存储中读取文件 |
remove | (key: string) => Promise<void> | 从存储中删除文件 |
copy | (key: string, destKey: string) => Promise<string> | 在桶内复制文件 |
move | (key: string, destKey: string) => Promise<string> | 在桶内移动文件 |
认证
这个适配器支持两种认证方法:
- 全权证书: 传出
projectId, (中文(简体) ),clientEmail,以及privateKey到构造器 - 应用程序默认证书( ADC):如果不提供证书,适配器会使用ADC. 当运行在 Google Cloud 基础设施时或当您配置了此设备时,此功能是有用的
GOOGLE_APPLICATION_CREDENTIALS环境变量
许可证
麻省理工学院