@ducktors/storagebus-s3

中文文档
0.12.1发布于 2025/12/20MITTypeScriptESM + CJS构建来源已验证近 30 天下载 4400

npm version License: MIT

此软件包已被标记为弃用

Package renamed to @storagebus/s3. Please migrate to @storagebus/s3@^1.0.0.

原始使用文档
文档目录10 节
  1. Installation
  2. Usage
  3. API
  4. Constructor Options
  5. Methods
  6. WriteOpts
  7. CopyOpts
  8. MoveOpts
  9. Authentication
  10. License

@ducktors/storagebus-s3

npm version License: MIT

AWS S3 storage adapter for Storagebus.

Installation

npm install @ducktors/storagebus-s3
# or
pnpm add @ducktors/storagebus-s3
# or
yarn add @ducktors/storagebus-s3

Usage

import { Storage } from '@ducktors/storagebus-s3'
import { Readable } from 'node:stream'

// Using explicit credentials
const storage = new Storage({
  bucket: 'your-s3-bucket',
  region: 'us-east-1',
  accessKeyId: 'your-access-key-id',
  secretAccessKey: 'your-secret-access-key',
})

// Or using default credentials (IAM role, environment variables, etc.)
const storageWithDefaultCreds = new Storage({
  bucket: 'your-s3-bucket',
  region: 'us-east-1',
})

async function main() {
  const readable = Readable.from('Hello, world!')

  // Write a file
  const writtenFile = await storage.write('path/to/file.txt', readable)

  // Write with progress tracking
  await storage.write('path/to/large-file.txt', readable, {
    progress: (p) => console.log(`Uploaded: ${p.loaded} bytes`),
  })

  // Write with server-side encryption
  await storage.write('path/to/encrypted.txt', readable, {
    entryption: {
      ServerSideEncryption: 'aws:kms',
      SSEKMSKeyId: 'your-kms-key-id',
    },
  })

  // 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')

  // Copy to a different bucket
  await storage.copy('path/to/file.txt', 'path/to/copy.txt', {
    destBucket: 'another-bucket',
  })

  // 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

Constructor Options

OptionTypeRequiredDescription
bucketstringYesS3 bucket name
regionstringNoAWS region (e.g., us-east-1)
accessKeyIdstringNoAWS access key ID (optional if using IAM roles or environment variables)
secretAccessKeystringNoAWS secret access key (optional if using IAM roles or environment variables)
debugbooleanNoEnable debug logging
loggerLoggerNoCustom logger instance
sanitizeKeyboolean | (key: string) => stringNoSanitize file keys

Methods

MethodSignatureDescription
write(key: string, fileReadable: Readable, opts?: WriteOpts) => Promise<string>Write a file to storage. Automatically sets Content-Type based on file extension
exists(key: string) => Promise<boolean>Check if a file exists
read(key: string) => Promise<Readable>Read a file from storage
remove(key: string) => Promise<void>Delete a file from storage
copy(key: string, destKey: string, opts?: CopyOpts) => Promise<string>Copy a file within or between buckets
move(key: string, destKey: string, opts?: MoveOpts) => Promise<string>Move a file within the bucket

WriteOpts

OptionTypeDescription
progress(p: Progress) => voidCallback function for upload progress
entryption.ServerSideEncryptionstringServer-side encryption algorithm (AES256, aws:kms, aws:kms:dsse)
entryption.SSEKMSKeyIdstringKMS key ID for encryption

CopyOpts

OptionTypeDescription
destBucketstringDestination bucket (defaults to source bucket)
entryption.ServerSideEncryptionstringServer-side encryption algorithm
entryption.SSEKMSKeyIdstringKMS key ID for encryption

MoveOpts

OptionTypeDescription
entryption.ServerSideEncryptionstringServer-side encryption algorithm
entryption.SSEKMSKeyIdstringKMS key ID for encryption

Authentication

This adapter supports multiple authentication methods:

  1. Explicit Credentials: Pass accessKeyId and secretAccessKey to the constructor
  2. Environment Variables: Set AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
  3. IAM Roles: When running on AWS infrastructure (EC2, Lambda, ECS, etc.)
  4. AWS Credentials File: ~/.aws/credentials

See AWS SDK credentials documentation for more details.

License

MIT

版本与发布记录

@ducktors/storagebus-s3 共发布 8 个版本,首次发布于 2022/11/29;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.12.1latest2025/12/2010 个月前
0.12.02025/12/1610 个月前
0.11.42023/03/023 年前
0.11.22023/02/043 年前
0.11.12023/01/173 年前
0.11.02022/11/303 年前

关于 @ducktors/storagebus-s3 的常见问题

如何安装 @ducktors/storagebus-s3?

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

@ducktors/storagebus-s3 支持 TypeScript 吗?

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

@ducktors/storagebus-s3 支持 ESM 还是 CommonJS?

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

@ducktors/storagebus-s3 可以商用吗?

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

@ducktors/storagebus-s3 的下载量有多少?

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

@ducktors/storagebus-s3 还在维护吗?

最新版本 0.12.1 发布于 2025/12/20(10 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 2 个;首次发布于 2022/11/29。作者已将其标记为弃用,新项目建议寻找替代方案。