@vultro/storage-s3 0.0.0-stage → 0.0.1

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.
@@ -0,0 +1 @@
1
+ export { createS3BlobStore, createS3Storage, s3Storage, type CreateS3StorageOptions, type S3BlobStoreOptions, } from "./s3.js";
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { createS3BlobStore, createS3Storage, s3Storage } from "./s3.js";
package/dist/s3.d.ts ADDED
@@ -0,0 +1,92 @@
1
+ /**
2
+ * ============================================================================
3
+ * S3 存储后端(`@vultro/storage-s3`,Bun 原生实现)
4
+ * ============================================================================
5
+ *
6
+ * 直接使用 Bun 内置的 `Bun.S3Client`,不再手写 SigV4:
7
+ *
8
+ * - 签名、重试、分片、并发全部由 Bun 的原生实现负责;
9
+ * - 支持 AWS S3 以及 MinIO / Cloudflare R2 的 S3 网关等兼容服务;
10
+ * - 零额外依赖(不需要 `@aws-sdk/*`)。
11
+ *
12
+ * 配置里可以写:
13
+ *
14
+ * ```ts
15
+ * import { s3Storage } from "@vultro/storage-s3";
16
+ * export default defineConfig({ storage: s3Storage() });
17
+ * ```
18
+ *
19
+ * 凭据通过 env(`S3_ACCESS_KEY_ID` / `S3_SECRET_ACCESS_KEY`)读取,不写进
20
+ * 配置文件。`Bun.S3Client` 只存在于 Bun 进程;Cloudflare Worker 请使用
21
+ * `@vultro/storage-r2`。
22
+ */
23
+ import { type BlobStore, type StorageBackend, type StorageFactory } from "@vultro/core/storage";
24
+ /**
25
+ * S3(及兼容实现,如 MinIO / R2 的 S3 网关)的配置。
26
+ *
27
+ * 字段名与 `Bun.S3Client` 的选项保持一致,方便对照官方文档;`prefix` 是本
28
+ * 适配器额外加的概念(同一 bucket 放多个 deployment)。
29
+ */
30
+ export interface S3BlobStoreOptions {
31
+ /** bucket 名称。 */
32
+ bucket: string;
33
+ /**
34
+ * 区域,例如 `us-east-1`。
35
+ *
36
+ * MinIO 等私有部署通常用 `us-east-1` 占位——多数实现不校验这个值,
37
+ * 但签名算法需要它。
38
+ */
39
+ region: string;
40
+ accessKeyId: string;
41
+ secretAccessKey: string;
42
+ /**
43
+ * 自定义 endpoint,例如 `https://s3.us-east-1.amazonaws.com` 或
44
+ * `http://127.0.0.1:9000`(MinIO)。省略时 `Bun.S3Client` 会按 AWS
45
+ * 官方域名拼接。
46
+ */
47
+ endpoint?: string;
48
+ /** 对象 key 前缀,便于同一 bucket 放多个 deployment。 */
49
+ prefix?: string;
50
+ /** 临时凭据的 session token。 */
51
+ sessionToken?: string;
52
+ }
53
+ /**
54
+ * 创建基于 `Bun.S3Client` 的字节后端。
55
+ *
56
+ * 所有对象操作都交给 Bun 原生实现;这里只做两件事:
57
+ * 1. 把逻辑 key(`objects/<id>`)加上配置的前缀;
58
+ * 2. 把 `S3File` 的形状适配成 `BlobStore` 的 `{ bytes, contentType }`。
59
+ *
60
+ * ```ts
61
+ * const blobs = createS3BlobStore({
62
+ * bucket: "my-bucket",
63
+ * region: "us-east-1",
64
+ * accessKeyId: process.env.S3_ACCESS_KEY_ID!,
65
+ * secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
66
+ * });
67
+ * await createBunRuntime({ modules, dataDir: ".vultro", blobs });
68
+ * ```
69
+ */
70
+ export declare function createS3BlobStore(options: S3BlobStoreOptions): BlobStore;
71
+ /** 创建 S3 Storage 工厂的选项;未提供的字段从 env 读取。 */
72
+ export interface CreateS3StorageOptions {
73
+ bucket?: string;
74
+ region?: string;
75
+ endpoint?: string;
76
+ prefix?: string;
77
+ accessKeyId?: string;
78
+ secretAccessKey?: string;
79
+ sessionToken?: string;
80
+ /** 读取凭据的环境变量名;默认 `S3_ACCESS_KEY_ID` / `S3_SECRET_ACCESS_KEY`。 */
81
+ accessKeyEnv?: string;
82
+ secretKeyEnv?: string;
83
+ }
84
+ /**
85
+ * 创建 S3 Storage 工厂。
86
+ *
87
+ * 配置优先取 `options`,其次取 `context.env`,最后取 `process.env`。缺少
88
+ * bucket / region / 凭据时直接抛错,避免把文件静默写进 SQLite 副本。
89
+ */
90
+ export declare function createS3Storage(options?: CreateS3StorageOptions): StorageFactory;
91
+ /** 配置层选择器:`storage: s3Storage()`。 */
92
+ export declare function s3Storage(options?: CreateS3StorageOptions): StorageBackend;
package/dist/s3.js ADDED
@@ -0,0 +1,139 @@
1
+ import { createStorageFromContext } from "@vultro/core/storage";
2
+ // src/s3.ts
3
+ /**
4
+ * ============================================================================
5
+ * S3 存储后端(`@vultro/storage-s3`,Bun 原生实现)
6
+ * ============================================================================
7
+ *
8
+ * 直接使用 Bun 内置的 `Bun.S3Client`,不再手写 SigV4:
9
+ *
10
+ * - 签名、重试、分片、并发全部由 Bun 的原生实现负责;
11
+ * - 支持 AWS S3 以及 MinIO / Cloudflare R2 的 S3 网关等兼容服务;
12
+ * - 零额外依赖(不需要 `@aws-sdk/*`)。
13
+ *
14
+ * 配置里可以写:
15
+ *
16
+ * ```ts
17
+ * import { s3Storage } from "@vultro/storage-s3";
18
+ * export default defineConfig({ storage: s3Storage() });
19
+ * ```
20
+ *
21
+ * 凭据通过 env(`S3_ACCESS_KEY_ID` / `S3_SECRET_ACCESS_KEY`)读取,不写进
22
+ * 配置文件。`Bun.S3Client` 只存在于 Bun 进程;Cloudflare Worker 请使用
23
+ * `@vultro/storage-r2`。
24
+ */
25
+ /**
26
+ * 创建基于 `Bun.S3Client` 的字节后端。
27
+ *
28
+ * 所有对象操作都交给 Bun 原生实现;这里只做两件事:
29
+ * 1. 把逻辑 key(`objects/<id>`)加上配置的前缀;
30
+ * 2. 把 `S3File` 的形状适配成 `BlobStore` 的 `{ bytes, contentType }`。
31
+ *
32
+ * ```ts
33
+ * const blobs = createS3BlobStore({
34
+ * bucket: "my-bucket",
35
+ * region: "us-east-1",
36
+ * accessKeyId: process.env.S3_ACCESS_KEY_ID!,
37
+ * secretAccessKey: process.env.S3_SECRET_ACCESS_KEY!,
38
+ * });
39
+ * await createBunRuntime({ modules, dataDir: ".vultro", blobs });
40
+ * ```
41
+ */
42
+ function createS3BlobStore(options) {
43
+ const S3Client = globalThis.Bun?.S3Client;
44
+ if (!S3Client) throw new Error("Bun.S3Client is not available in this runtime; @vultro/runtime-bun 的 S3 适配器需要 Bun");
45
+ const prefix = options.prefix?.replace(/^\/+|\/+$/g, "");
46
+ /** 逻辑 key → bucket 内的实际 key。 */
47
+ const keyOf = (key) => prefix ? `${prefix}/${key}` : key;
48
+ const client = new S3Client({
49
+ bucket: options.bucket,
50
+ region: options.region,
51
+ accessKeyId: options.accessKeyId,
52
+ secretAccessKey: options.secretAccessKey,
53
+ ...options.endpoint ? { endpoint: options.endpoint } : {},
54
+ ...options.sessionToken ? { sessionToken: options.sessionToken } : {}
55
+ });
56
+ return {
57
+ kind: "s3",
58
+ async put(key, bytes, putOptions = {}) {
59
+ await client.write(keyOf(key), bytes, { ...putOptions.contentType ? { type: putOptions.contentType } : {} });
60
+ },
61
+ async get(key) {
62
+ const target = keyOf(key);
63
+ const file = client.file(target);
64
+ if (!await file.exists()) return null;
65
+ const bytes = new Uint8Array(await file.arrayBuffer());
66
+ const stats = await client.stat(target);
67
+ return {
68
+ bytes,
69
+ ...stats.type && stats.type !== "application/octet-stream" ? { contentType: stats.type } : {}
70
+ };
71
+ },
72
+ async delete(key) {
73
+ await client.delete(keyOf(key));
74
+ },
75
+ async head(key) {
76
+ const target = keyOf(key);
77
+ if (!await client.exists(target)) return null;
78
+ const stats = await client.stat(target);
79
+ return {
80
+ size: stats.size,
81
+ ...stats.type && stats.type !== "application/octet-stream" ? { contentType: stats.type } : {}
82
+ };
83
+ }
84
+ };
85
+ }
86
+ /** 从 StorageContext.env 或 Bun / Node 的 process.env 读取配置。 */
87
+ function readEnv(context, key) {
88
+ const value = context.env[key];
89
+ if (typeof value === "string" && value) return value;
90
+ const fallback = {}[key];
91
+ return typeof fallback === "string" && fallback ? fallback : void 0;
92
+ }
93
+ /**
94
+ * 创建 S3 Storage 工厂。
95
+ *
96
+ * 配置优先取 `options`,其次取 `context.env`,最后取 `process.env`。缺少
97
+ * bucket / region / 凭据时直接抛错,避免把文件静默写进 SQLite 副本。
98
+ */
99
+ function createS3Storage(options = {}) {
100
+ return (context) => {
101
+ const bucket = options.bucket ?? readEnv(context, "S3_BUCKET");
102
+ const region = options.region ?? readEnv(context, "S3_REGION") ?? "us-east-1";
103
+ const accessKeyId = options.accessKeyId ?? readEnv(context, options.accessKeyEnv ?? "S3_ACCESS_KEY_ID");
104
+ const secretAccessKey = options.secretAccessKey ?? readEnv(context, options.secretKeyEnv ?? "S3_SECRET_ACCESS_KEY");
105
+ if (!bucket || !accessKeyId || !secretAccessKey) throw new Error("S3 storage requires bucket, accessKeyId and secretAccessKey (pass options or set S3_BUCKET / S3_ACCESS_KEY_ID / S3_SECRET_ACCESS_KEY)");
106
+ const blobs = createS3BlobStore({
107
+ bucket,
108
+ region,
109
+ accessKeyId,
110
+ secretAccessKey,
111
+ ...options.endpoint ? { endpoint: options.endpoint } : {},
112
+ ...options.prefix ? { prefix: options.prefix } : {},
113
+ ...options.sessionToken ? { sessionToken: options.sessionToken } : {}
114
+ });
115
+ return createStorageFromContext(context, blobs);
116
+ };
117
+ }
118
+ /** 配置层选择器:`storage: s3Storage()`。 */
119
+ function s3Storage(options = {}) {
120
+ return {
121
+ name: "s3",
122
+ runtimes: ["bun", "node"],
123
+ module: "@vultro/storage-s3",
124
+ exportName: "createS3Storage",
125
+ options: {
126
+ ...options.bucket ? { bucket: options.bucket } : {},
127
+ ...options.region ? { region: options.region } : {},
128
+ ...options.endpoint ? { endpoint: options.endpoint } : {},
129
+ ...options.prefix ? { prefix: options.prefix } : {},
130
+ ...options.accessKeyEnv ? { accessKeyEnv: options.accessKeyEnv } : {},
131
+ ...options.secretKeyEnv ? { secretKeyEnv: options.secretKeyEnv } : {}
132
+ },
133
+ create: (runtime) => {
134
+ if (runtime !== "bun" && runtime !== "node") throw new Error(`s3Storage only supports bun/node, got `);
135
+ return createS3Storage(options);
136
+ }
137
+ };
138
+ }
139
+ export { createS3BlobStore, createS3Storage, s3Storage };
package/package.json CHANGED
@@ -1,6 +1,20 @@
1
1
  {
2
2
  "name": "@vultro/storage-s3",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.0.1",
4
+ "type": "module",
5
+ "description": "S3-compatible file storage backend for Vultro (Bun S3Client)",
6
+ "exports": {
7
+ ".": {
8
+ "types": "./dist/index.d.ts",
9
+ "import": "./dist/index.js",
10
+ "default": "./dist/index.js"
11
+ }
12
+ },
13
+ "dependencies": {
14
+ "@vultro/core": "0.0.1"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "types": "./dist/index.d.ts"
20
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.