
AWS S3 存储适配器 存储总线。 。 。 .
安装
npm install @ducktors/storagebus-s3
# or
pnpm add @ducktors/storagebus-s3
# or
yarn add @ducktors/storagebus-s3
使用量
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 密码
构造选项
| 选项 | 类型 | 需求 | 说明 |
bucket | string | 对 | S3 桶名称 |
region | string | 没有 | AWS区域(例如, us-east-1(中文(简体) ) |
accessKeyId | string | 没有 | AWS 访问密钥ID( 如果使用 IAM 角色或环境变量可选择) |
secretAccessKey | string | 没有 | AWS 秘密访问密钥( 如果使用 IAM 角色或环境变量可选择) |
debug | boolean | 没有 | 启用调试日志 |
logger | Logger | 没有 | 自定义日志实例 |
sanitizeKey | boolean | (key: string) => string | 没有 | 将文件密钥设置为安全 |
方法
| 方法 | 签署 | 说明 |
write | (key: string, fileReadable: Readable, opts?: WriteOpts) => Promise<string> | 写一个文件到存储 。 根据文件扩展名自动设置内容类型 |
exists | (key: string) => Promise<boolean> | 检查文件是否存在 |
read | (key: string) => Promise<Readable> | 从存储中读取文件 |
remove | (key: string) => Promise<void> | 从存储中删除文件 |
copy | (key: string, destKey: string, opts?: CopyOpts) => Promise<string> | 在桶内或桶间复制文件 |
move | (key: string, destKey: string, opts?: MoveOpts) => Promise<string> | 在桶内移动文件 |
写入 Opts
| 选项 | 类型 | 说明 |
progress | (p: Progress) => void | 上传进度的召回功能 |
entryption.ServerSideEncryption | string | 服务器侧加密算法( E)AES256, (中文(简体) ), aws:kms, (中文(简体) ), aws:kms:dsse(中文(简体) ) |
entryption.SSEKMSKeyId | string | 加密的 KMS 密钥ID |
复制对象
| 选项 | 类型 | 说明 |
destBucket | string | 目标桶( 默认为源桶) |
entryption.ServerSideEncryption | string | 服务器侧加密算法 |
entryption.SSEKMSKeyId | string | 加密的 KMS 密钥ID |
移动 Opts
| 选项 | 类型 | 说明 |
entryption.ServerSideEncryption | string | 服务器侧加密算法 |
entryption.SSEKMSKeyId | string | 加密的 KMS 密钥ID |
认证
这个适配器支持多种认证方法:
- 全权证书: 传出
accessKeyId 财务报告和审定财务报表 secretAccessKey 到构造器
- 环境变量设置
AWS_ACCESS_KEY_ID 财务报告和审定财务报表 AWS_SECRET_ACCESS_KEY
- IAM 角色:在AWS基础设施上运行时(EC2,Lambda,ECS等)
- AWS 证书文件数字 :
~/.aws/credentials
见 AWS SDK 证书文件 更多细节.
许可证
麻省理工学院