# @ducktors/storagebus-gcs 中文文档

规范地址：https://npmcn.com/package/%40ducktors/storagebus-gcs/

版本：0\.11\.5
npm 元数据更新时间：2026-06-12T11:21:13.810Z
中文译文更新时间：2026-11-12T23:43:35.051Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40ducktors/storagebus-gcs
原文对照：https://npmcn.com/package/%40ducktors/storagebus-gcs/?lang=original
项目主页：https://github.com/ducktors/storagebus#readme
源码仓库：https://github.com/ducktors/storagebus

弃用说明：Package renamed to @storagebus/gcs\. Please migrate to @storagebus/gcs@^1\.0\.0\.

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：12.5 kB，9 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：4 个
- 首次发布：2022-11-29T21:54:26.239Z
- 最新版本发布：2025-12-19T16:25:53.709Z
- 近 30 天下载量：8000（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：6（近 12 个月 1 个）

## 中文简介

[![npm version](https://img.shields.io/npm/v/@ducktors/storagebus-gcs)](https://www.npmjs.com/package/@ducktors/storagebus-gcs) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

## 安装

```sh
npm install @ducktors/storagebus-gcs
```

也可以使用：`pnpm add @ducktors/storagebus-gcs`、`yarn add @ducktors/storagebus-gcs`、`bun add @ducktors/storagebus-gcs`

## README

# @ducktors/ staragebus-gcs (英语)

[![npm version](https://img.shields.io/npm/v/@ducktors/storagebus-gcs)](https://www.npmjs.com/package/@ducktors/storagebus-gcs) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Google 云存储适配器 [存储总线](https://github.com/ducktors/storagebus)。 。 。 .

## 安装

```bash
npm install @ducktors/storagebus-gcs
# or
pnpm add @ducktors/storagebus-gcs
# or
yarn add @ducktors/storagebus-gcs
```

## 使用量

```typescript
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>` | 在桶内移动文件 |

## 认证

这个适配器支持两种认证方法:

1.  **全权证书**: 传出 `projectId`, (中文(简体) ), `clientEmail`,以及 `privateKey` 到构造器
2.  **应用程序默认证书( ADC)**:如果不提供证书,适配器会使用ADC. 当运行在 Google Cloud 基础设施时或当您配置了此设备时,此功能是有用的 `GOOGLE_APPLICATION_CREDENTIALS` 环境变量

## 许可证

麻省理工学院

## 常见问题

### 如何安装 @ducktors/storagebus-gcs？

在项目目录运行 npm install @ducktors/storagebus-gcs，或使用 pnpm add / yarn add / bun add。

### @ducktors/storagebus-gcs 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @ducktors/storagebus-gcs 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @ducktors/storagebus-gcs 可以商用吗？

@ducktors/storagebus-gcs 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @ducktors/storagebus-gcs 的下载量有多少？

根据 npm 官方下载统计，@ducktors/storagebus-gcs 在 2026/10/13 – 2026/11/11 这 30 天内被下载 8,000 次，日均约 267 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @ducktors/storagebus-gcs 还在维护吗？

最新版本 0\.11\.5 发布于 2025/12/20（10 个月前）；至今共发布 6 个版本，其中过去 12 个月发布了 1 个；首次发布于 2022/11/30。作者已将其标记为弃用，新项目建议寻找替代方案。

