@casfa/storage-s3 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +182 -0
- package/README.zh-CN.md +182 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +97 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -0
package/README.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# @casfa/storage-s3
|
|
2
|
+
|
|
3
|
+
S3 storage provider for CAS.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @casfa/storage-s3
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Overview
|
|
12
|
+
|
|
13
|
+
An S3-backed storage provider for CAS (Content-Addressable Storage). Suitable for cloud deployments with high availability and durability requirements.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
### Basic Usage
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { createS3Storage } from '@casfa/storage-s3';
|
|
21
|
+
|
|
22
|
+
const storage = createS3Storage({
|
|
23
|
+
bucket: 'my-cas-bucket',
|
|
24
|
+
region: 'us-east-1',
|
|
25
|
+
prefix: 'cas/', // Optional key prefix
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Store data
|
|
29
|
+
await storage.put('node:abcd1234...', data);
|
|
30
|
+
|
|
31
|
+
// Retrieve data
|
|
32
|
+
const data = await storage.get('node:abcd1234...');
|
|
33
|
+
|
|
34
|
+
// Check existence
|
|
35
|
+
const exists = await storage.has('node:abcd1234...');
|
|
36
|
+
|
|
37
|
+
// Delete
|
|
38
|
+
const deleted = await storage.delete('node:abcd1234...');
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### With LRU Cache
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { createS3StorageWithCache } from '@casfa/storage-s3';
|
|
45
|
+
|
|
46
|
+
const storage = createS3StorageWithCache({
|
|
47
|
+
bucket: 'my-cas-bucket',
|
|
48
|
+
region: 'us-east-1',
|
|
49
|
+
cacheSize: 1000, // Max items in cache
|
|
50
|
+
cacheMaxAge: 300000, // 5 minutes TTL
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// Cache reduces S3 API calls for repeated reads
|
|
54
|
+
const data = await storage.get('node:abcd1234...');
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### With Custom S3 Client
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
61
|
+
import { createS3Storage } from '@casfa/storage-s3';
|
|
62
|
+
|
|
63
|
+
const s3Client = new S3Client({
|
|
64
|
+
region: 'us-east-1',
|
|
65
|
+
credentials: {
|
|
66
|
+
accessKeyId: 'AKIA...',
|
|
67
|
+
secretAccessKey: '...',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const storage = createS3Storage({
|
|
72
|
+
bucket: 'my-cas-bucket',
|
|
73
|
+
client: s3Client,
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Configuration
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
interface S3StorageConfig {
|
|
81
|
+
// Required: S3 bucket name
|
|
82
|
+
bucket: string;
|
|
83
|
+
|
|
84
|
+
// AWS region (required if not using custom client)
|
|
85
|
+
region?: string;
|
|
86
|
+
|
|
87
|
+
// Optional: Key prefix for all objects
|
|
88
|
+
prefix?: string;
|
|
89
|
+
|
|
90
|
+
// Optional: Custom S3 client
|
|
91
|
+
client?: S3Client;
|
|
92
|
+
|
|
93
|
+
// Optional: Storage class for new objects
|
|
94
|
+
storageClass?: 'STANDARD' | 'STANDARD_IA' | 'GLACIER' | 'DEEP_ARCHIVE';
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Cache Options
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
interface S3StorageWithCacheConfig extends S3StorageConfig {
|
|
102
|
+
// Max items in LRU cache (default: 1000)
|
|
103
|
+
cacheSize?: number;
|
|
104
|
+
|
|
105
|
+
// Cache TTL in milliseconds (default: 60000)
|
|
106
|
+
cacheMaxAge?: number;
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## S3 Key Structure
|
|
111
|
+
|
|
112
|
+
Objects are stored with sharded prefixes for better S3 performance:
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
{prefix}ab/cd/abcd1234...
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Where `ab` and `cd` are the first 4 characters of the hash, providing good distribution across S3 partitions.
|
|
119
|
+
|
|
120
|
+
## API Reference
|
|
121
|
+
|
|
122
|
+
### Functions
|
|
123
|
+
|
|
124
|
+
- `createS3Storage(config)` - Create S3 storage without cache
|
|
125
|
+
- `createS3StorageWithCache(config)` - Create S3 storage with LRU cache
|
|
126
|
+
|
|
127
|
+
### StorageProvider Interface
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
interface StorageProvider {
|
|
131
|
+
get(key: string): Promise<Uint8Array | null>;
|
|
132
|
+
put(key: string, data: Uint8Array): Promise<void>;
|
|
133
|
+
has(key: string): Promise<boolean>;
|
|
134
|
+
delete(key: string): Promise<boolean>;
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## AWS Permissions
|
|
139
|
+
|
|
140
|
+
Required IAM permissions for the S3 bucket:
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
{
|
|
144
|
+
"Version": "2012-10-17",
|
|
145
|
+
"Statement": [
|
|
146
|
+
{
|
|
147
|
+
"Effect": "Allow",
|
|
148
|
+
"Action": [
|
|
149
|
+
"s3:GetObject",
|
|
150
|
+
"s3:PutObject",
|
|
151
|
+
"s3:DeleteObject",
|
|
152
|
+
"s3:HeadObject"
|
|
153
|
+
],
|
|
154
|
+
"Resource": "arn:aws:s3:::my-cas-bucket/*"
|
|
155
|
+
}
|
|
156
|
+
]
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Performance Tips
|
|
161
|
+
|
|
162
|
+
1. **Use caching** to reduce S3 API calls and latency
|
|
163
|
+
2. **Use S3 Transfer Acceleration** for global access
|
|
164
|
+
3. **Choose appropriate storage class** based on access patterns
|
|
165
|
+
4. **Enable S3 Intelligent-Tiering** for cost optimization
|
|
166
|
+
5. **Use regional endpoints** to minimize latency
|
|
167
|
+
|
|
168
|
+
## Cost Considerations
|
|
169
|
+
|
|
170
|
+
- CAS data is immutable, so versioning is not needed
|
|
171
|
+
- Consider S3 Intelligent-Tiering for infrequently accessed data
|
|
172
|
+
- Monitor PUT/GET request costs for high-throughput workloads
|
|
173
|
+
|
|
174
|
+
## Related Packages
|
|
175
|
+
|
|
176
|
+
- `@casfa/storage-core` - Core types and utilities
|
|
177
|
+
- `@casfa/storage-fs` - File system storage (for local deployment)
|
|
178
|
+
- `@casfa/storage-memory` - In-memory storage (for testing)
|
|
179
|
+
|
|
180
|
+
## License
|
|
181
|
+
|
|
182
|
+
MIT
|
package/README.zh-CN.md
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# @casfa/storage-s3
|
|
2
|
+
|
|
3
|
+
基于 S3 的 CAS 存储提供者。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
bun add @casfa/storage-s3
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 概述
|
|
12
|
+
|
|
13
|
+
基于 S3 的 CAS(内容寻址存储)存储提供者。适用于对高可用性和数据持久性有要求的云端部署。
|
|
14
|
+
|
|
15
|
+
## 使用方法
|
|
16
|
+
|
|
17
|
+
### 基本用法
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { createS3Storage } from '@casfa/storage-s3';
|
|
21
|
+
|
|
22
|
+
const storage = createS3Storage({
|
|
23
|
+
bucket: 'my-cas-bucket',
|
|
24
|
+
region: 'us-east-1',
|
|
25
|
+
prefix: 'cas/', // 可选的键前缀
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// 存储数据
|
|
29
|
+
await storage.put('node:abcd1234...', data);
|
|
30
|
+
|
|
31
|
+
// 检索数据
|
|
32
|
+
const data = await storage.get('node:abcd1234...');
|
|
33
|
+
|
|
34
|
+
// 检查是否存在
|
|
35
|
+
const exists = await storage.has('node:abcd1234...');
|
|
36
|
+
|
|
37
|
+
// 删除
|
|
38
|
+
const deleted = await storage.delete('node:abcd1234...');
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### 带 LRU 缓存
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { createS3StorageWithCache } from '@casfa/storage-s3';
|
|
45
|
+
|
|
46
|
+
const storage = createS3StorageWithCache({
|
|
47
|
+
bucket: 'my-cas-bucket',
|
|
48
|
+
region: 'us-east-1',
|
|
49
|
+
cacheSize: 1000, // 缓存最大条目数
|
|
50
|
+
cacheMaxAge: 300000, // 5 分钟 TTL
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
// 缓存减少 S3 API 调用,降低重复读取延迟
|
|
54
|
+
const data = await storage.get('node:abcd1234...');
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### 使用自定义 S3 客户端
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
61
|
+
import { createS3Storage } from '@casfa/storage-s3';
|
|
62
|
+
|
|
63
|
+
const s3Client = new S3Client({
|
|
64
|
+
region: 'us-east-1',
|
|
65
|
+
credentials: {
|
|
66
|
+
accessKeyId: 'AKIA...',
|
|
67
|
+
secretAccessKey: '...',
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const storage = createS3Storage({
|
|
72
|
+
bucket: 'my-cas-bucket',
|
|
73
|
+
client: s3Client,
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## 配置
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
interface S3StorageConfig {
|
|
81
|
+
// 必需:S3 桶名称
|
|
82
|
+
bucket: string;
|
|
83
|
+
|
|
84
|
+
// AWS 区域(不使用自定义客户端时必需)
|
|
85
|
+
region?: string;
|
|
86
|
+
|
|
87
|
+
// 可选:所有对象的键前缀
|
|
88
|
+
prefix?: string;
|
|
89
|
+
|
|
90
|
+
// 可选:自定义 S3 客户端
|
|
91
|
+
client?: S3Client;
|
|
92
|
+
|
|
93
|
+
// 可选:新对象的存储类别
|
|
94
|
+
storageClass?: 'STANDARD' | 'STANDARD_IA' | 'GLACIER' | 'DEEP_ARCHIVE';
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 缓存选项
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
interface S3StorageWithCacheConfig extends S3StorageConfig {
|
|
102
|
+
// LRU 缓存最大条目数(默认: 1000)
|
|
103
|
+
cacheSize?: number;
|
|
104
|
+
|
|
105
|
+
// 缓存 TTL,单位毫秒(默认: 60000)
|
|
106
|
+
cacheMaxAge?: number;
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## S3 键结构
|
|
111
|
+
|
|
112
|
+
对象使用分片前缀存储,以获得更好的 S3 性能:
|
|
113
|
+
|
|
114
|
+
```
|
|
115
|
+
{prefix}ab/cd/abcd1234...
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
其中 `ab` 和 `cd` 是哈希的前 4 个字符,可在 S3 分区间提供良好的分布。
|
|
119
|
+
|
|
120
|
+
## API 参考
|
|
121
|
+
|
|
122
|
+
### 函数
|
|
123
|
+
|
|
124
|
+
- `createS3Storage(config)` - 创建不带缓存的 S3 存储
|
|
125
|
+
- `createS3StorageWithCache(config)` - 创建带 LRU 缓存的 S3 存储
|
|
126
|
+
|
|
127
|
+
### StorageProvider 接口
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
interface StorageProvider {
|
|
131
|
+
get(key: string): Promise<Uint8Array | null>;
|
|
132
|
+
put(key: string, data: Uint8Array): Promise<void>;
|
|
133
|
+
has(key: string): Promise<boolean>;
|
|
134
|
+
delete(key: string): Promise<boolean>;
|
|
135
|
+
}
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
## AWS 权限
|
|
139
|
+
|
|
140
|
+
S3 桶所需的 IAM 权限:
|
|
141
|
+
|
|
142
|
+
```json
|
|
143
|
+
{
|
|
144
|
+
"Version": "2012-10-17",
|
|
145
|
+
"Statement": [
|
|
146
|
+
{
|
|
147
|
+
"Effect": "Allow",
|
|
148
|
+
"Action": [
|
|
149
|
+
"s3:GetObject",
|
|
150
|
+
"s3:PutObject",
|
|
151
|
+
"s3:DeleteObject",
|
|
152
|
+
"s3:HeadObject"
|
|
153
|
+
],
|
|
154
|
+
"Resource": "arn:aws:s3:::my-cas-bucket/*"
|
|
155
|
+
}
|
|
156
|
+
]
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## 性能建议
|
|
161
|
+
|
|
162
|
+
1. **使用缓存** 以减少 S3 API 调用和延迟
|
|
163
|
+
2. **使用 S3 Transfer Acceleration** 以实现全球访问
|
|
164
|
+
3. **根据访问模式选择合适的存储类别**
|
|
165
|
+
4. **启用 S3 Intelligent-Tiering** 以优化成本
|
|
166
|
+
5. **使用区域端点** 以最小化延迟
|
|
167
|
+
|
|
168
|
+
## 成本考虑
|
|
169
|
+
|
|
170
|
+
- CAS 数据是不可变的,因此不需要版本控制
|
|
171
|
+
- 对于不常访问的数据,考虑使用 S3 Intelligent-Tiering
|
|
172
|
+
- 对于高吞吐量工作负载,注意监控 PUT/GET 请求费用
|
|
173
|
+
|
|
174
|
+
## 相关包
|
|
175
|
+
|
|
176
|
+
- `@casfa/storage-core` - 核心类型与工具
|
|
177
|
+
- `@casfa/storage-fs` - 文件系统存储(用于本地部署)
|
|
178
|
+
- `@casfa/storage-memory` - 内存存储(用于测试)
|
|
179
|
+
|
|
180
|
+
## 许可证
|
|
181
|
+
|
|
182
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { S3Client } from '@aws-sdk/client-s3';
|
|
2
|
+
import { StorageProvider } from '@casfa/storage-core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* S3 Storage Provider for CAS
|
|
6
|
+
*
|
|
7
|
+
* Implements StorageProvider with:
|
|
8
|
+
* - LRU cache for key existence checks
|
|
9
|
+
* - S3 backend storage
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* S3 Storage configuration
|
|
14
|
+
*/
|
|
15
|
+
type S3StorageConfig = {
|
|
16
|
+
/** S3 bucket name */
|
|
17
|
+
bucket: string;
|
|
18
|
+
/** Optional S3 client (for testing or custom config) */
|
|
19
|
+
client?: S3Client;
|
|
20
|
+
/** LRU cache size for key existence (default: 10000) */
|
|
21
|
+
cacheSize?: number;
|
|
22
|
+
/** Key prefix in S3 (default: "cas/sha256/") */
|
|
23
|
+
prefix?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Create an S3-backed storage provider
|
|
27
|
+
*/
|
|
28
|
+
declare const createS3Storage: (config: S3StorageConfig) => StorageProvider;
|
|
29
|
+
/**
|
|
30
|
+
* Create S3 storage with cache control methods (for testing)
|
|
31
|
+
*/
|
|
32
|
+
declare const createS3StorageWithCache: (config: S3StorageConfig) => {
|
|
33
|
+
clearCache: () => void;
|
|
34
|
+
getCacheStats: () => {
|
|
35
|
+
size: number;
|
|
36
|
+
};
|
|
37
|
+
has: (key: string) => Promise<boolean>;
|
|
38
|
+
get: (key: string) => Promise<Uint8Array | null>;
|
|
39
|
+
put: (key: string, value: Uint8Array) => Promise<void>;
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { type S3StorageConfig, createS3Storage, createS3StorageWithCache };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/s3-storage.ts
|
|
2
|
+
import {
|
|
3
|
+
GetObjectCommand,
|
|
4
|
+
HeadObjectCommand,
|
|
5
|
+
PutObjectCommand,
|
|
6
|
+
S3Client
|
|
7
|
+
} from "@aws-sdk/client-s3";
|
|
8
|
+
import {
|
|
9
|
+
createLRUCache,
|
|
10
|
+
DEFAULT_CACHE_SIZE,
|
|
11
|
+
toStoragePath
|
|
12
|
+
} from "@casfa/storage-core";
|
|
13
|
+
var createS3Storage = (config) => {
|
|
14
|
+
const client = config.client ?? new S3Client({});
|
|
15
|
+
const bucket = config.bucket;
|
|
16
|
+
const prefix = config.prefix ?? "cas/sha256/";
|
|
17
|
+
const existsCache = createLRUCache(config.cacheSize ?? DEFAULT_CACHE_SIZE);
|
|
18
|
+
const toS3Key = (casKey) => toStoragePath(casKey, prefix);
|
|
19
|
+
const has = async (key) => {
|
|
20
|
+
const cached = existsCache.get(key);
|
|
21
|
+
if (cached !== void 0) {
|
|
22
|
+
return cached;
|
|
23
|
+
}
|
|
24
|
+
try {
|
|
25
|
+
await client.send(
|
|
26
|
+
new HeadObjectCommand({
|
|
27
|
+
Bucket: bucket,
|
|
28
|
+
Key: toS3Key(key)
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
existsCache.set(key, true);
|
|
32
|
+
return true;
|
|
33
|
+
} catch (error) {
|
|
34
|
+
const err = error;
|
|
35
|
+
if (err.name === "NotFound" || err.$metadata?.httpStatusCode === 404) {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const get = async (key) => {
|
|
42
|
+
try {
|
|
43
|
+
const result = await client.send(
|
|
44
|
+
new GetObjectCommand({
|
|
45
|
+
Bucket: bucket,
|
|
46
|
+
Key: toS3Key(key)
|
|
47
|
+
})
|
|
48
|
+
);
|
|
49
|
+
const bytes = await result.Body.transformToByteArray();
|
|
50
|
+
existsCache.set(key, true);
|
|
51
|
+
return new Uint8Array(bytes);
|
|
52
|
+
} catch (error) {
|
|
53
|
+
const err = error;
|
|
54
|
+
if (err.name === "NoSuchKey" || err.$metadata?.httpStatusCode === 404) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const put = async (key, value) => {
|
|
61
|
+
if (existsCache.get(key)) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
const exists = await has(key);
|
|
65
|
+
if (exists) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
await client.send(
|
|
69
|
+
new PutObjectCommand({
|
|
70
|
+
Bucket: bucket,
|
|
71
|
+
Key: toS3Key(key),
|
|
72
|
+
Body: value,
|
|
73
|
+
ContentType: "application/octet-stream"
|
|
74
|
+
})
|
|
75
|
+
);
|
|
76
|
+
existsCache.set(key, true);
|
|
77
|
+
};
|
|
78
|
+
return { has, get, put };
|
|
79
|
+
};
|
|
80
|
+
var createS3StorageWithCache = (config) => {
|
|
81
|
+
const client = config.client ?? new S3Client({});
|
|
82
|
+
const _bucket = config.bucket;
|
|
83
|
+
const prefix = config.prefix ?? "cas/sha256/";
|
|
84
|
+
const existsCache = createLRUCache(config.cacheSize ?? DEFAULT_CACHE_SIZE);
|
|
85
|
+
const _toS3Key = (casKey) => toStoragePath(casKey, prefix);
|
|
86
|
+
const storage = createS3Storage({ ...config, client });
|
|
87
|
+
return {
|
|
88
|
+
...storage,
|
|
89
|
+
clearCache: () => existsCache.clear(),
|
|
90
|
+
getCacheStats: () => ({ size: existsCache.size() })
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
export {
|
|
94
|
+
createS3Storage,
|
|
95
|
+
createS3StorageWithCache
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/s3-storage.ts"],"sourcesContent":["/**\n * S3 Storage Provider for CAS\n *\n * Implements StorageProvider with:\n * - LRU cache for key existence checks\n * - S3 backend storage\n */\n\nimport {\n GetObjectCommand,\n HeadObjectCommand,\n PutObjectCommand,\n S3Client,\n} from \"@aws-sdk/client-s3\";\nimport {\n createLRUCache,\n DEFAULT_CACHE_SIZE,\n type StorageProvider,\n toStoragePath,\n} from \"@casfa/storage-core\";\n\n/**\n * S3 Storage configuration\n */\nexport type S3StorageConfig = {\n /** S3 bucket name */\n bucket: string;\n /** Optional S3 client (for testing or custom config) */\n client?: S3Client;\n /** LRU cache size for key existence (default: 10000) */\n cacheSize?: number;\n /** Key prefix in S3 (default: \"cas/sha256/\") */\n prefix?: string;\n};\n\n/**\n * Create an S3-backed storage provider\n */\nexport const createS3Storage = (config: S3StorageConfig): StorageProvider => {\n const client = config.client ?? new S3Client({});\n const bucket = config.bucket;\n const prefix = config.prefix ?? \"cas/sha256/\";\n const existsCache = createLRUCache<string, boolean>(config.cacheSize ?? DEFAULT_CACHE_SIZE);\n\n const toS3Key = (casKey: string): string => toStoragePath(casKey, prefix);\n\n const has = async (key: string): Promise<boolean> => {\n // Check cache first\n const cached = existsCache.get(key);\n if (cached !== undefined) {\n return cached;\n }\n\n // Check S3\n try {\n await client.send(\n new HeadObjectCommand({\n Bucket: bucket,\n Key: toS3Key(key),\n })\n );\n existsCache.set(key, true);\n return true;\n } catch (error: unknown) {\n const err = error as { name?: string; $metadata?: { httpStatusCode?: number } };\n if (err.name === \"NotFound\" || err.$metadata?.httpStatusCode === 404) {\n // Don't cache non-existence (it might be uploaded later)\n return false;\n }\n throw error;\n }\n };\n\n const get = async (key: string): Promise<Uint8Array | null> => {\n try {\n const result = await client.send(\n new GetObjectCommand({\n Bucket: bucket,\n Key: toS3Key(key),\n })\n );\n\n const bytes = await result.Body!.transformToByteArray();\n\n // Mark as existing in cache\n existsCache.set(key, true);\n\n return new Uint8Array(bytes);\n } catch (error: unknown) {\n const err = error as { name?: string; $metadata?: { httpStatusCode?: number } };\n if (err.name === \"NoSuchKey\" || err.$metadata?.httpStatusCode === 404) {\n return null;\n }\n throw error;\n }\n };\n\n const put = async (key: string, value: Uint8Array): Promise<void> => {\n // Check cache first (avoid redundant writes)\n if (existsCache.get(key)) {\n return;\n }\n\n // Check if already exists in S3\n const exists = await has(key);\n if (exists) {\n return;\n }\n\n // Upload to S3\n await client.send(\n new PutObjectCommand({\n Bucket: bucket,\n Key: toS3Key(key),\n Body: value,\n ContentType: \"application/octet-stream\",\n })\n );\n\n // Mark as existing\n existsCache.set(key, true);\n };\n\n return { has, get, put };\n};\n\n/**\n * Create S3 storage with cache control methods (for testing)\n */\nexport const createS3StorageWithCache = (config: S3StorageConfig) => {\n const client = config.client ?? new S3Client({});\n const _bucket = config.bucket;\n const prefix = config.prefix ?? \"cas/sha256/\";\n const existsCache = createLRUCache<string, boolean>(config.cacheSize ?? DEFAULT_CACHE_SIZE);\n\n const _toS3Key = (casKey: string): string => toStoragePath(casKey, prefix);\n\n const storage = createS3Storage({ ...config, client });\n\n return {\n ...storage,\n clearCache: () => existsCache.clear(),\n getCacheStats: () => ({ size: existsCache.size() }),\n };\n};\n"],"mappings":";AAQA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EAEA;AAAA,OACK;AAmBA,IAAM,kBAAkB,CAAC,WAA6C;AAC3E,QAAM,SAAS,OAAO,UAAU,IAAI,SAAS,CAAC,CAAC;AAC/C,QAAM,SAAS,OAAO;AACtB,QAAM,SAAS,OAAO,UAAU;AAChC,QAAM,cAAc,eAAgC,OAAO,aAAa,kBAAkB;AAE1F,QAAM,UAAU,CAAC,WAA2B,cAAc,QAAQ,MAAM;AAExE,QAAM,MAAM,OAAO,QAAkC;AAEnD,UAAM,SAAS,YAAY,IAAI,GAAG;AAClC,QAAI,WAAW,QAAW;AACxB,aAAO;AAAA,IACT;AAGA,QAAI;AACF,YAAM,OAAO;AAAA,QACX,IAAI,kBAAkB;AAAA,UACpB,QAAQ;AAAA,UACR,KAAK,QAAQ,GAAG;AAAA,QAClB,CAAC;AAAA,MACH;AACA,kBAAY,IAAI,KAAK,IAAI;AACzB,aAAO;AAAA,IACT,SAAS,OAAgB;AACvB,YAAM,MAAM;AACZ,UAAI,IAAI,SAAS,cAAc,IAAI,WAAW,mBAAmB,KAAK;AAEpE,eAAO;AAAA,MACT;AACA,YAAM;AAAA,IACR;AAAA,EACF;AAEA,QAAM,MAAM,OAAO,QAA4C;AAC7D,QAAI;AACF,YAAM,SAAS,MAAM,OAAO;AAAA,QAC1B,IAAI,iBAAiB;AAAA,UACnB,QAAQ;AAAA,UACR,KAAK,QAAQ,GAAG;AAAA,QAClB,CAAC;AAAA,MACH;AAEA,YAAM,QAAQ,MAAM,OAAO,KAAM,qBAAqB;AAGtD,kBAAY,IAAI,KAAK,IAAI;AAEzB,aAAO,IAAI,WAAW,KAAK;AAAA,IAC7B,SAAS,OAAgB;AACvB,YAAM,MAAM;AACZ,UAAI,IAAI,SAAS,eAAe,IAAI,WAAW,mBAAmB,KAAK;AACrE,eAAO;AAAA,MACT;AACA,YAAM;AAAA,IACR;AAAA,EACF;AAEA,QAAM,MAAM,OAAO,KAAa,UAAqC;AAEnE,QAAI,YAAY,IAAI,GAAG,GAAG;AACxB;AAAA,IACF;AAGA,UAAM,SAAS,MAAM,IAAI,GAAG;AAC5B,QAAI,QAAQ;AACV;AAAA,IACF;AAGA,UAAM,OAAO;AAAA,MACX,IAAI,iBAAiB;AAAA,QACnB,QAAQ;AAAA,QACR,KAAK,QAAQ,GAAG;AAAA,QAChB,MAAM;AAAA,QACN,aAAa;AAAA,MACf,CAAC;AAAA,IACH;AAGA,gBAAY,IAAI,KAAK,IAAI;AAAA,EAC3B;AAEA,SAAO,EAAE,KAAK,KAAK,IAAI;AACzB;AAKO,IAAM,2BAA2B,CAAC,WAA4B;AACnE,QAAM,SAAS,OAAO,UAAU,IAAI,SAAS,CAAC,CAAC;AAC/C,QAAM,UAAU,OAAO;AACvB,QAAM,SAAS,OAAO,UAAU;AAChC,QAAM,cAAc,eAAgC,OAAO,aAAa,kBAAkB;AAE1F,QAAM,WAAW,CAAC,WAA2B,cAAc,QAAQ,MAAM;AAEzE,QAAM,UAAU,gBAAgB,EAAE,GAAG,QAAQ,OAAO,CAAC;AAErD,SAAO;AAAA,IACL,GAAG;AAAA,IACH,YAAY,MAAM,YAAY,MAAM;AAAA,IACpC,eAAe,OAAO,EAAE,MAAM,YAAY,KAAK,EAAE;AAAA,EACnD;AACF;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@casfa/storage-s3",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "S3 storage provider for CAS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"build": "tsup",
|
|
16
|
+
"dev": "tsup --watch",
|
|
17
|
+
"typecheck": "tsc --noEmit",
|
|
18
|
+
"lint": "biome check .",
|
|
19
|
+
"lint:fix": "biome check --write .",
|
|
20
|
+
"prepublishOnly": "bun run build"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@casfa/storage-core": "workspace:*",
|
|
24
|
+
"@aws-sdk/client-s3": "^3.975.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"typescript": "^5.7.2"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"README.md"
|
|
32
|
+
],
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"license": "MIT"
|
|
37
|
+
}
|