@ computesdk/s3 (帮助)
AWS S3存储提供者为ComputeSDK - 对象存储与S3兼容的API.
安装
npm install @computesdk/s3
使用量
import { s3 } from '@computesdk/s3';
// Create storage instance
const storage = s3({
accessKeyId: process.env.TIGRIS_STORAGE_ACCESS_KEY_ID,
secretAccessKey: process.env.TIGRIS_STORAGE_SECRET_ACCESS_KEY,
endpoint: 'https://t3.storage.dev'
});
// Upload a file
await storage.upload('my-bucket', 'path/to/file.txt', 'Hello, World!');
// Upload with options
await storage.upload('my-bucket', 'image.png', buffer, {
contentType: 'image/png',
metadata: { author: 'user-123' }
});
// Download a file
const result = await storage.download('my-bucket', 'path/to/file.txt');
console.log(Buffer.from(result.data).toString());
// Delete a file
await storage.delete('my-bucket', 'path/to/file.txt');
// List objects
const list = await storage.list('my-bucket', { prefix: 'path/to/' });
for (const obj of list.objects) {
console.log(`${obj.key}: ${obj.size} bytes`);
}
配置
| 选项 | 类型 | 需求 | 默认 | 说明 |
|---|---|---|---|---|
accessKeyId | 字符串 | 无* | TIGRIS_STORAGE_ACCESS_KEY_ID 环境 | 访问密钥 ID |
secretAccessKey | 字符串 | 无* | TIGRIS_STORAGE_SECRET_ACCESS_KEY 环境 | 秘密访问密钥 |
endpoint | 字符串 | 没有 | TIGRIS_STORAGE_ENDPOINT 打开,然后 https://t3.storage.dev | S3兼容服务的终点 |
* Ether在配置或设置环境变量中提供.
API 密码
upload(bucket, key, data, options?)
上传数据到 S3 .
- 桶头: Bucket 名称
- 键对象密钥/路径
- 数据数字 :
Uint8Array或者说string要上传(节点)Buffer因为它是Uint8Array子类) - 选项: 可选上传配置
contentType: MIME 类型( 默认 ):application/octet-stream(中文(简体) )metadata: 自定义元数据对象
回返 Promise<StorageObject> 含有上传对象的元数据.
download(bucket, key)
从S3下载数据.
- 桶头: Bucket 名称
- 键对象密钥/路径
回返 Promise<DownloadResult> 包含:
data数字 :Uint8Array带有对象内容( 节点)Buffer可用Buffer.from(data)(中文(简体) )size: 以字节表示的对象大小contentType: MIME 类型etag实体标签lastModified: 上次修改日期metadata: 自定义元数据
delete(bucket, key)
从 S3 删除对象 .
- 桶头: Bucket 名称
- 键对象密钥/路径
回返 Promise<void>。 。 。 .
list(bucket, options?)
在桶中列出对象 .
- 桶头: Bucket 名称
- 选项: 可选列表配置
prefix: 按前缀过滤maxKeys: 最大结果( 默认值: 1000)continuationToken: 贴花符
回返 Promise<ListResult> 包含:
objects: 阵列StorageObjecttruncated: 是否有更多结果continuationToken: 下一页按键
getClient()
获取 AWS S3 基础客户端用于高级操作.
回返 S3Client 从 @aws-sdk/client-s3。 。 。 .
环境变量
export AWS_ACCESS_KEY_ID=your-access-key
export AWS_SECRET_ACCESS_KEY=your-secret-key
export AWS_REGION=us-east-1 # optional, defaults to us-east-1
许可证
麻省理工学院