@agentback/files-s3 0.3.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/LICENSE +33 -0
- package/README.md +43 -0
- package/dist/__tests__/integration/s3-file-store.integration.d.ts +2 -0
- package/dist/__tests__/integration/s3-file-store.integration.d.ts.map +1 -0
- package/dist/__tests__/integration/s3-file-store.integration.js +35 -0
- package/dist/__tests__/integration/s3-file-store.integration.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/s3-file-store.d.ts +34 -0
- package/dist/s3-file-store.d.ts.map +1 -0
- package/dist/s3-file-store.js +98 -0
- package/dist/s3-file-store.js.map +1 -0
- package/package.json +39 -0
- package/src/__tests__/integration/s3-file-store.integration.ts +42 -0
- package/src/index.ts +5 -0
- package/src/s3-file-store.ts +161 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Copyright (c) Ninemind.ai 2026.
|
|
2
|
+
Node module: AgentBack
|
|
3
|
+
This project is licensed under the MIT License, full text below.
|
|
4
|
+
|
|
5
|
+
--------
|
|
6
|
+
|
|
7
|
+
MIT License
|
|
8
|
+
|
|
9
|
+
Copyright (c) Ninemind.ai 2026
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
12
|
+
this software and
|
|
13
|
+
associated documentation files (the "Software"), to deal in the Software without
|
|
14
|
+
restriction, including
|
|
15
|
+
without limitation the rights to use, copy, modify, merge, publish, distribute,
|
|
16
|
+
sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is furnished
|
|
18
|
+
to do so, subject to the
|
|
19
|
+
following conditions:
|
|
20
|
+
|
|
21
|
+
The above copyright notice and this permission notice shall be included in all
|
|
22
|
+
copies or substantial
|
|
23
|
+
portions of the Software.
|
|
24
|
+
|
|
25
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
26
|
+
IMPLIED, INCLUDING BUT NOT
|
|
27
|
+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
|
|
28
|
+
AND NONINFRINGEMENT. IN NO
|
|
29
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
30
|
+
OTHER LIABILITY, WHETHER
|
|
31
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
32
|
+
CONNECTION WITH THE SOFTWARE OR THE
|
|
33
|
+
USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @agentback/files-s3
|
|
2
|
+
|
|
3
|
+
> S3-backed `FileStore` adapter for [`@agentback/files`](../files).
|
|
4
|
+
|
|
5
|
+
Implements the `FileStore` port against any S3-compatible backend (AWS S3,
|
|
6
|
+
localstack, MinIO, R2) using AWS SDK v3. Uploads stream through
|
|
7
|
+
`@aws-sdk/lib-storage`'s `Upload` (no full-file buffering); downloads stream
|
|
8
|
+
from `GetObject`. Ports the proven dapp5 `s3.service` recipe onto the port.
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import {S3FileStore} from '@agentback/files-s3';
|
|
14
|
+
import {FILE_STORE} from '@agentback/files';
|
|
15
|
+
|
|
16
|
+
const store = new S3FileStore({
|
|
17
|
+
bucket: 'my-uploads',
|
|
18
|
+
keyPrefix: 'files/', // optional namespacing
|
|
19
|
+
clientConfig: {region: 'us-east-1'}, // or pass an existing {client}
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
app.bind(FILE_STORE).to(store); // the REST file recipe now uses S3
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
- **`put`** streams the body to S3, recording `filename` in object metadata and
|
|
26
|
+
`contentType` as `Content-Type`; returns size + ETag (via a follow-up HEAD).
|
|
27
|
+
- **`get`** returns the object stream + metadata; a missing key throws
|
|
28
|
+
`FileNotFoundError` (→ 404 at the REST layer).
|
|
29
|
+
- **`exists`/`delete`** map to HeadObject/DeleteObject.
|
|
30
|
+
- **`presignedPut`/`presignedGet`** issue time-limited URLs for a
|
|
31
|
+
direct-to-S3 flow (default 15 min).
|
|
32
|
+
|
|
33
|
+
## Testing
|
|
34
|
+
|
|
35
|
+
The conformance suite from `@agentback/files/testing` runs against a real
|
|
36
|
+
endpoint, gated on env (skipped otherwise — same pattern as the BullMQ/Redis
|
|
37
|
+
tests):
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
S3_TEST_ENDPOINT=http://localhost:4566 S3_TEST_BUCKET=test \
|
|
41
|
+
AWS_ACCESS_KEY_ID=test AWS_SECRET_ACCESS_KEY=test \
|
|
42
|
+
pnpm -F @agentback/files-s3 test
|
|
43
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3-file-store.integration.d.ts","sourceRoot":"","sources":["../../../src/__tests__/integration/s3-file-store.integration.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright Ninemind.ai 2026. All Rights Reserved.
|
|
2
|
+
// This file is licensed under the MIT License.
|
|
3
|
+
// License text available at https://opensource.org/license/mit/
|
|
4
|
+
import { describe, it } from 'vitest';
|
|
5
|
+
import { runFileStoreConformance } from '@agentback/files/testing';
|
|
6
|
+
import { S3FileStore } from '../../index.js';
|
|
7
|
+
// Gated like the BullMQ/Redis tests: runs only against a real S3-compatible
|
|
8
|
+
// endpoint (localstack/minio/AWS). Set S3_TEST_ENDPOINT + S3_TEST_BUCKET
|
|
9
|
+
// (and AWS creds) to exercise it; otherwise it is skipped.
|
|
10
|
+
const endpoint = process.env.S3_TEST_ENDPOINT;
|
|
11
|
+
const bucket = process.env.S3_TEST_BUCKET;
|
|
12
|
+
if (endpoint && bucket) {
|
|
13
|
+
runFileStoreConformance('S3FileStore', () => new S3FileStore({
|
|
14
|
+
bucket,
|
|
15
|
+
keyPrefix: 'files-s3-test/',
|
|
16
|
+
clientConfig: {
|
|
17
|
+
endpoint,
|
|
18
|
+
region: process.env.AWS_REGION ?? 'us-east-1',
|
|
19
|
+
forcePathStyle: true, // localstack/minio path-style addressing
|
|
20
|
+
credentials: {
|
|
21
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? 'test',
|
|
22
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? 'test',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
}));
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// eslint-disable-next-line no-console
|
|
29
|
+
console.log('[files-s3] S3_TEST_ENDPOINT / S3_TEST_BUCKET not set — skipping S3 ' +
|
|
30
|
+
'conformance (point them at localstack/minio/AWS to run)');
|
|
31
|
+
describe.skip('S3FileStore conformance (needs S3_TEST_ENDPOINT + S3_TEST_BUCKET)', () => {
|
|
32
|
+
it('skipped — no S3 endpoint configured', () => { });
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=s3-file-store.integration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3-file-store.integration.js","sourceRoot":"","sources":["../../../src/__tests__/integration/s3-file-store.integration.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,+CAA+C;AAC/C,gEAAgE;AAEhE,OAAO,EAAC,QAAQ,EAAE,EAAE,EAAC,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAC,uBAAuB,EAAC,MAAM,0BAA0B,CAAC;AACjE,OAAO,EAAC,WAAW,EAAC,MAAM,gBAAgB,CAAC;AAE3C,4EAA4E;AAC5E,yEAAyE;AACzE,2DAA2D;AAC3D,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC;AAC9C,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;AAE1C,IAAI,QAAQ,IAAI,MAAM,EAAE,CAAC;IACvB,uBAAuB,CACrB,aAAa,EACb,GAAG,EAAE,CACH,IAAI,WAAW,CAAC;QACd,MAAM;QACN,SAAS,EAAE,gBAAgB;QAC3B,YAAY,EAAE;YACZ,QAAQ;YACR,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,WAAW;YAC7C,cAAc,EAAE,IAAI,EAAE,yCAAyC;YAC/D,WAAW,EAAE;gBACX,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB,IAAI,MAAM;gBACpD,eAAe,EAAE,OAAO,CAAC,GAAG,CAAC,qBAAqB,IAAI,MAAM;aAC7D;SACF;KACF,CAAC,CACL,CAAC;AACJ,CAAC;KAAM,CAAC;IACN,sCAAsC;IACtC,OAAO,CAAC,GAAG,CACT,qEAAqE;QACnE,yDAAyD,CAC5D,CAAC;IACF,QAAQ,CAAC,IAAI,CAAC,mEAAmE,EAAE,GAAG,EAAE;QACtF,EAAE,CAAC,qCAAqC,EAAE,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;IACtD,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAC,WAAW,EAAE,KAAK,kBAAkB,EAAC,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,+CAA+C;AAC/C,gEAAgE;AAEhE,OAAO,EAAC,WAAW,EAA0B,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
2
|
+
import { S3Client, type S3ClientConfig } from '@aws-sdk/client-s3';
|
|
3
|
+
import { type FileStore, type PresignOptions, type PutOptions, type RetrievedFile, type StoredFile } from '@agentback/files';
|
|
4
|
+
export interface S3FileStoreOptions {
|
|
5
|
+
/** Target bucket. */
|
|
6
|
+
bucket: string;
|
|
7
|
+
/** Reuse an existing client, or… */
|
|
8
|
+
client?: S3Client;
|
|
9
|
+
/** …construct one from this config (region, endpoint, credentials, …). */
|
|
10
|
+
clientConfig?: S3ClientConfig;
|
|
11
|
+
/** Optional key prefix namespacing every object (e.g. `'uploads/'`). */
|
|
12
|
+
keyPrefix?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* S3-backed {@link FileStore}. Streams uploads via `@aws-sdk/lib-storage`'s
|
|
16
|
+
* `Upload` (no full-file buffering) and streams downloads from `GetObject`,
|
|
17
|
+
* recording `filename` in object metadata. Ports the dapp5 `s3.service`
|
|
18
|
+
* recipe onto the framework port. Also implements the optional presigned-URL
|
|
19
|
+
* hooks for a direct-to-S3 flow.
|
|
20
|
+
*/
|
|
21
|
+
export declare class S3FileStore implements FileStore {
|
|
22
|
+
private readonly client;
|
|
23
|
+
private readonly bucket;
|
|
24
|
+
private readonly prefix;
|
|
25
|
+
constructor(opts: S3FileStoreOptions);
|
|
26
|
+
private k;
|
|
27
|
+
put(key: string, body: Readable | Buffer, opts?: PutOptions): Promise<StoredFile>;
|
|
28
|
+
get(key: string): Promise<RetrievedFile>;
|
|
29
|
+
exists(key: string): Promise<boolean>;
|
|
30
|
+
delete(key: string): Promise<void>;
|
|
31
|
+
presignedPut(key: string, opts?: PutOptions & PresignOptions): Promise<string>;
|
|
32
|
+
presignedGet(key: string, opts?: PresignOptions): Promise<string>;
|
|
33
|
+
}
|
|
34
|
+
//# sourceMappingURL=s3-file-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3-file-store.d.ts","sourceRoot":"","sources":["../src/s3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mB,GACxB,OAAO,CAAC,MAAM,CAAC;CAOnB"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Copyright Ninemind.ai 2026. All Rights Reserved.
|
|
2
|
+
// This file is licensed under the MIT License.
|
|
3
|
+
// License text available at https://opensource.org/license/mit/
|
|
4
|
+
import { DeleteObjectCommand, GetObjectCommand, HeadObjectCommand, PutObjectCommand, S3Client, } from '@aws-sdk/client-s3';
|
|
5
|
+
import { Upload } from '@aws-sdk/lib-storage';
|
|
6
|
+
import { getSignedUrl } from '@aws-sdk/s3-request-presigner';
|
|
7
|
+
import { FileNotFoundError, } from '@agentback/files';
|
|
8
|
+
/**
|
|
9
|
+
* S3-backed {@link FileStore}. Streams uploads via `@aws-sdk/lib-storage`'s
|
|
10
|
+
* `Upload` (no full-file buffering) and streams downloads from `GetObject`,
|
|
11
|
+
* recording `filename` in object metadata. Ports the dapp5 `s3.service`
|
|
12
|
+
* recipe onto the framework port. Also implements the optional presigned-URL
|
|
13
|
+
* hooks for a direct-to-S3 flow.
|
|
14
|
+
*/
|
|
15
|
+
export class S3FileStore {
|
|
16
|
+
constructor(opts) {
|
|
17
|
+
this.client = opts.client ?? new S3Client(opts.clientConfig ?? {});
|
|
18
|
+
this.bucket = opts.bucket;
|
|
19
|
+
this.prefix = opts.keyPrefix ?? '';
|
|
20
|
+
}
|
|
21
|
+
k(key) {
|
|
22
|
+
return this.prefix + key;
|
|
23
|
+
}
|
|
24
|
+
async put(key, body, opts = {}) {
|
|
25
|
+
const upload = new Upload({
|
|
26
|
+
client: this.client,
|
|
27
|
+
params: {
|
|
28
|
+
Bucket: this.bucket,
|
|
29
|
+
Key: this.k(key),
|
|
30
|
+
Body: body,
|
|
31
|
+
...(opts.contentType ? { ContentType: opts.contentType } : {}),
|
|
32
|
+
Metadata: {
|
|
33
|
+
...(opts.filename ? { filename: opts.filename } : {}),
|
|
34
|
+
...(opts.metadata ?? {}),
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
const res = await upload.done();
|
|
39
|
+
// lib-storage doesn't return the stored size; a HEAD is the reliable source.
|
|
40
|
+
const head = await this.client.send(new HeadObjectCommand({ Bucket: this.bucket, Key: this.k(key) }));
|
|
41
|
+
return {
|
|
42
|
+
key,
|
|
43
|
+
size: head.ContentLength ?? 0,
|
|
44
|
+
contentType: head.ContentType,
|
|
45
|
+
etag: res.ETag ?? head.ETag,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async get(key) {
|
|
49
|
+
try {
|
|
50
|
+
const res = await this.client.send(new GetObjectCommand({ Bucket: this.bucket, Key: this.k(key) }));
|
|
51
|
+
return {
|
|
52
|
+
key,
|
|
53
|
+
stream: res.Body,
|
|
54
|
+
size: res.ContentLength ?? 0,
|
|
55
|
+
contentType: res.ContentType,
|
|
56
|
+
filename: res.Metadata?.filename,
|
|
57
|
+
metadata: res.Metadata,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
if (isNotFound(err))
|
|
62
|
+
throw new FileNotFoundError(key);
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
async exists(key) {
|
|
67
|
+
try {
|
|
68
|
+
await this.client.send(new HeadObjectCommand({ Bucket: this.bucket, Key: this.k(key) }));
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
if (isNotFound(err))
|
|
73
|
+
return false;
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async delete(key) {
|
|
78
|
+
await this.client.send(new DeleteObjectCommand({ Bucket: this.bucket, Key: this.k(key) }));
|
|
79
|
+
}
|
|
80
|
+
async presignedPut(key, opts = {}) {
|
|
81
|
+
return getSignedUrl(this.client, new PutObjectCommand({
|
|
82
|
+
Bucket: this.bucket,
|
|
83
|
+
Key: this.k(key),
|
|
84
|
+
...(opts.contentType ? { ContentType: opts.contentType } : {}),
|
|
85
|
+
}), { expiresIn: opts.expiresInSec ?? 900 });
|
|
86
|
+
}
|
|
87
|
+
async presignedGet(key, opts = {}) {
|
|
88
|
+
return getSignedUrl(this.client, new GetObjectCommand({ Bucket: this.bucket, Key: this.k(key) }), { expiresIn: opts.expiresInSec ?? 900 });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** S3 "not found" across GetObject (NoSuchKey) and HeadObject (NotFound/404). */
|
|
92
|
+
function isNotFound(err) {
|
|
93
|
+
const e = err;
|
|
94
|
+
return (e?.name === 'NoSuchKey' ||
|
|
95
|
+
e?.name === 'NotFound' ||
|
|
96
|
+
e?.$metadata?.httpStatusCode === 404);
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=s3-file-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"s3-file-store.js","sourceRoot":"","sources":["../src/s3-file-store.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,+CAA+C;AAC/C,gEAAgE;AAGhE,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,QAAQ,GAET,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAC,MAAM,EAAC,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAC,YAAY,EAAC,MAAM,+BAA+B,CAAC;AAC3D,OAAO,EACL,iBAAiB,GAMlB,MAAM,kBAAkB,CAAC;AAa1B;;;;;;GAMG;AACH,MAAM,OAAO,WAAW;IAKtB,YAAY,IAAwB;QAClC,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,QAAQ,CAAC,IAAI,CAAC,YAAY,IAAI,EAAE,CAAC,CAAC;QACnE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC;QAC1B,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,SAAS,IAAI,EAAE,CAAC;IACrC,CAAC;IAEO,CAAC,CAAC,GAAW;QACnB,OAAO,IAAI,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ACJ,CAAC;IAED,KAAK,CAAC,YAAY,CAChB,GAAW,EACX,OAAoC,EAAE;QAEtC,OAAO,YAAY,CACjB,IAAI,CAAC,MAAM,EACX,IAAI,gBAAgB,CAAC;YACnB,MAAM,EAAE,IAAI,CAAC,MAAM;YACnB,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC;YAChB,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC,EAAC,WAAW,EAAE,IAAI,CAAC,WAAW,EAAC,CAAC,CAAC,CAAC,EAAE,CAAC;SAC7D,CAAC,EACF,EAAC,SAAS,EAAE,IAAI,CAAC,YAAY,IAAI,GAAG,EAAC,CACtC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,YAAY,CAChB,GAAW,EACX,OAAuB,EAAE;QAEzB,OAAO,YAAY,CACjB,IAAI,CAAC,MAAM,EACX,IAAI,gBAAgB,CAAC,EAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,CAAC,GAAG,CAAC,EAAC,CAAC,EAC7D,EAAC,SAAS,EAAE,IAAI,CAAC,YAAY,IAAI,GAAG,EAAC,CACtC,CAAC;IACJ,CAAC;CACF;AAED,iFAAiF;AACjF,SAAS,UAAU,CAAC,GAAY;IAC9B,MAAM,CAAC,GAAG,GAA6D,CAAC;IACxE,OAAO,CACL,CAAC,EAAE,IAAI,KAAK,WAAW;QACvB,CAAC,EAAE,IAAI,KAAK,UAAU;QACtB,CAAC,EAAE,SAAS,EAAE,cAAc,KAAK,GAAG,CACrC,CAAC;AACJ,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agentback/files-s3",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "S3-backed FileStore adapter for @agentback/files (AWS SDK v3 + lib-storage streaming)",
|
|
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
|
+
"files": [
|
|
15
|
+
"dist",
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@aws-sdk/client-s3": "^3.658.0",
|
|
20
|
+
"@aws-sdk/lib-storage": "^3.658.0",
|
|
21
|
+
"@aws-sdk/s3-request-presigner": "^3.658.0",
|
|
22
|
+
"tslib": "^2.8.1",
|
|
23
|
+
"@agentback/files": "~0.3.0"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"vitest": "~4.1.8"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22.13"
|
|
30
|
+
},
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"build": "tsc -b",
|
|
37
|
+
"clean": "rm -rf dist *.tsbuildinfo"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Copyright Ninemind.ai 2026. All Rights Reserved.
|
|
2
|
+
// This file is licensed under the MIT License.
|
|
3
|
+
// License text available at https://opensource.org/license/mit/
|
|
4
|
+
|
|
5
|
+
import {describe, it} from 'vitest';
|
|
6
|
+
import {runFileStoreConformance} from '@agentback/files/testing';
|
|
7
|
+
import {S3FileStore} from '../../index.js';
|
|
8
|
+
|
|
9
|
+
// Gated like the BullMQ/Redis tests: runs only against a real S3-compatible
|
|
10
|
+
// endpoint (localstack/minio/AWS). Set S3_TEST_ENDPOINT + S3_TEST_BUCKET
|
|
11
|
+
// (and AWS creds) to exercise it; otherwise it is skipped.
|
|
12
|
+
const endpoint = process.env.S3_TEST_ENDPOINT;
|
|
13
|
+
const bucket = process.env.S3_TEST_BUCKET;
|
|
14
|
+
|
|
15
|
+
if (endpoint && bucket) {
|
|
16
|
+
runFileStoreConformance(
|
|
17
|
+
'S3FileStore',
|
|
18
|
+
() =>
|
|
19
|
+
new S3FileStore({
|
|
20
|
+
bucket,
|
|
21
|
+
keyPrefix: 'files-s3-test/',
|
|
22
|
+
clientConfig: {
|
|
23
|
+
endpoint,
|
|
24
|
+
region: process.env.AWS_REGION ?? 'us-east-1',
|
|
25
|
+
forcePathStyle: true, // localstack/minio path-style addressing
|
|
26
|
+
credentials: {
|
|
27
|
+
accessKeyId: process.env.AWS_ACCESS_KEY_ID ?? 'test',
|
|
28
|
+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? 'test',
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
} else {
|
|
34
|
+
// eslint-disable-next-line no-console
|
|
35
|
+
console.log(
|
|
36
|
+
'[files-s3] S3_TEST_ENDPOINT / S3_TEST_BUCKET not set — skipping S3 ' +
|
|
37
|
+
'conformance (point them at localstack/minio/AWS to run)',
|
|
38
|
+
);
|
|
39
|
+
describe.skip('S3FileStore conformance (needs S3_TEST_ENDPOINT + S3_TEST_BUCKET)', () => {
|
|
40
|
+
it('skipped — no S3 endpoint configured', () => {});
|
|
41
|
+
});
|
|
42
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Copyright Ninemind.ai 2026. All Rights Reserved.
|
|
2
|
+
// This file is licensed under the MIT License.
|
|
3
|
+
// License text available at https://opensource.org/license/mit/
|
|
4
|
+
|
|
5
|
+
import type {Readable} from 'node:stream';
|
|
6
|
+
import {
|
|
7
|
+
DeleteObjectCommand,
|
|
8
|
+
GetObjectCommand,
|
|
9
|
+
HeadObjectCommand,
|
|
10
|
+
PutObjectCommand,
|
|
11
|
+
S3Client,
|
|
12
|
+
type S3ClientConfig,
|
|
13
|
+
} from '@aws-sdk/client-s3';
|
|
14
|
+
import {Upload} from '@aws-sdk/lib-storage';
|
|
15
|
+
import {getSignedUrl} from '@aws-sdk/s3-request-presigner';
|
|
16
|
+
import {
|
|
17
|
+
FileNotFoundError,
|
|
18
|
+
type FileStore,
|
|
19
|
+
type PresignOptions,
|
|
20
|
+
type PutOptions,
|
|
21
|
+
type RetrievedFile,
|
|
22
|
+
type StoredFile,
|
|
23
|
+
} from '@agentback/files';
|
|
24
|
+
|
|
25
|
+
export interface S3FileStoreOptions {
|
|
26
|
+
/** Target bucket. */
|
|
27
|
+
bucket: string;
|
|
28
|
+
/** Reuse an existing client, or… */
|
|
29
|
+
client?: S3Client;
|
|
30
|
+
/** …construct one from this config (region, endpoint, credentials, …). */
|
|
31
|
+
clientConfig?: S3ClientConfig;
|
|
32
|
+
/** Optional key prefix namespacing every object (e.g. `'uploads/'`). */
|
|
33
|
+
keyPrefix?: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* S3-backed {@link FileStore}. Streams uploads via `@aws-sdk/lib-storage`'s
|
|
38
|
+
* `Upload` (no full-file buffering) and streams downloads from `GetObject`,
|
|
39
|
+
* recording `filename` in object metadata. Ports the dapp5 `s3.service`
|
|
40
|
+
* recipe onto the framework port. Also implements the optional presigned-URL
|
|
41
|
+
* hooks for a direct-to-S3 flow.
|
|
42
|
+
*/
|
|
43
|
+
export class S3FileStore implements FileStore {
|
|
44
|
+
private readonly client: S3Client;
|
|
45
|
+
private readonly bucket: string;
|
|
46
|
+
private readonly prefix: string;
|
|
47
|
+
|
|
48
|
+
constructor(opts: S3FileStoreOptions) {
|
|
49
|
+
this.client = opts.client ?? new S3Client(opts.clientConfig ?? {});
|
|
50
|
+
this.bucket = opts.bucket;
|
|
51
|
+
this.prefix = opts.keyPrefix ?? '';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private k(key: string): string {
|
|
55
|
+
return this.prefix + key;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async put(
|
|
59
|
+
key: string,
|
|
60
|
+
body: Readable | Buffer,
|
|
61
|
+
opts: PutOptions = {},
|
|
62
|
+
): Promise<StoredFile> {
|
|
63
|
+
const upload = new Upload({
|
|
64
|
+
client: this.client,
|
|
65
|
+
params: {
|
|
66
|
+
Bucket: this.bucket,
|
|
67
|
+
Key: this.k(key),
|
|
68
|
+
Body: body,
|
|
69
|
+
...(opts.contentType ? {ContentType: opts.contentType} : {}),
|
|
70
|
+
Metadata: {
|
|
71
|
+
...(opts.filename ? {filename: opts.filename} : {}),
|
|
72
|
+
...(opts.metadata ?? {}),
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
const res = await upload.done();
|
|
77
|
+
// lib-storage doesn't return the stored size; a HEAD is the reliable source.
|
|
78
|
+
const head = await this.client.send(
|
|
79
|
+
new HeadObjectCommand({Bucket: this.bucket, Key: this.k(key)}),
|
|
80
|
+
);
|
|
81
|
+
return {
|
|
82
|
+
key,
|
|
83
|
+
size: head.ContentLength ?? 0,
|
|
84
|
+
contentType: head.ContentType,
|
|
85
|
+
etag: (res as {ETag?: string}).ETag ?? head.ETag,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async get(key: string): Promise<RetrievedFile> {
|
|
90
|
+
try {
|
|
91
|
+
const res = await this.client.send(
|
|
92
|
+
new GetObjectCommand({Bucket: this.bucket, Key: this.k(key)}),
|
|
93
|
+
);
|
|
94
|
+
return {
|
|
95
|
+
key,
|
|
96
|
+
stream: res.Body as Readable,
|
|
97
|
+
size: res.ContentLength ?? 0,
|
|
98
|
+
contentType: res.ContentType,
|
|
99
|
+
filename: res.Metadata?.filename,
|
|
100
|
+
metadata: res.Metadata,
|
|
101
|
+
};
|
|
102
|
+
} catch (err) {
|
|
103
|
+
if (isNotFound(err)) throw new FileNotFoundError(key);
|
|
104
|
+
throw err;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async exists(key: string): Promise<boolean> {
|
|
109
|
+
try {
|
|
110
|
+
await this.client.send(
|
|
111
|
+
new HeadObjectCommand({Bucket: this.bucket, Key: this.k(key)}),
|
|
112
|
+
);
|
|
113
|
+
return true;
|
|
114
|
+
} catch (err) {
|
|
115
|
+
if (isNotFound(err)) return false;
|
|
116
|
+
throw err;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async delete(key: string): Promise<void> {
|
|
121
|
+
await this.client.send(
|
|
122
|
+
new DeleteObjectCommand({Bucket: this.bucket, Key: this.k(key)}),
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async presignedPut(
|
|
127
|
+
key: string,
|
|
128
|
+
opts: PutOptions & PresignOptions = {},
|
|
129
|
+
): Promise<string> {
|
|
130
|
+
return getSignedUrl(
|
|
131
|
+
this.client,
|
|
132
|
+
new PutObjectCommand({
|
|
133
|
+
Bucket: this.bucket,
|
|
134
|
+
Key: this.k(key),
|
|
135
|
+
...(opts.contentType ? {ContentType: opts.contentType} : {}),
|
|
136
|
+
}),
|
|
137
|
+
{expiresIn: opts.expiresInSec ?? 900},
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async presignedGet(
|
|
142
|
+
key: string,
|
|
143
|
+
opts: PresignOptions = {},
|
|
144
|
+
): Promise<string> {
|
|
145
|
+
return getSignedUrl(
|
|
146
|
+
this.client,
|
|
147
|
+
new GetObjectCommand({Bucket: this.bucket, Key: this.k(key)}),
|
|
148
|
+
{expiresIn: opts.expiresInSec ?? 900},
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** S3 "not found" across GetObject (NoSuchKey) and HeadObject (NotFound/404). */
|
|
154
|
+
function isNotFound(err: unknown): boolean {
|
|
155
|
+
const e = err as {name?: string; $metadata?: {httpStatusCode?: number}};
|
|
156
|
+
return (
|
|
157
|
+
e?.name === 'NoSuchKey' ||
|
|
158
|
+
e?.name === 'NotFound' ||
|
|
159
|
+
e?.$metadata?.httpStatusCode === 404
|
|
160
|
+
);
|
|
161
|
+
}
|