Created
July 15, 2025 13:48
-
-
Save cameronapak/3fabbaf9632785d059b20bb65a8ae996 to your computer and use it in GitHub Desktop.
My guess at s3 bucket adapter for bknd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import type { AstroBkndConfig } from "bknd/adapter/astro"; | |
| import type { APIContext } from "astro"; | |
| import { em, entity, number, text, libsql } from "bknd/data"; | |
| import { secureRandomString } from "bknd/utils"; | |
| import { StorageS3Adapter, MediaAdapterRegistry, type S3AdapterConfig } from "bknd/media"; | |
| import { syncTypes } from "bknd/plugins"; | |
| import { registries } from "bknd"; | |
| import { | |
| LIBSQL_DATABASE_TOKEN, | |
| LIBSQL_DATABASE_URL, | |
| S3_API_URL, | |
| S3_ACCESS_KEY, | |
| S3_SECRET_ACCESS_KEY | |
| } from "astro:env/server"; | |
| let registered = false; | |
| export function registerS3MediaAdapter() { | |
| if (!registered) { | |
| registries.media.register("s3", StorageS3Adapter); | |
| registered = true; | |
| } | |
| return (config: Partial<S3AdapterConfig> = {}) => { | |
| const adapter = new StorageS3Adapter({ | |
| url: config.url || "", | |
| access_key: config.access_key || "", | |
| secret_access_key: config.secret_access_key || "", | |
| ...config | |
| }); | |
| return adapter.toJSON(true); | |
| }; | |
| } | |
| export default { | |
| // we can use any libsql config, and if omitted, uses in-memory | |
| app: (ctx: APIContext) => ({ | |
| connection: libsql({ | |
| url: LIBSQL_DATABASE_URL ?? "file:.astro/content.db", | |
| authToken: LIBSQL_DATABASE_TOKEN | |
| }) | |
| }), | |
| // an initial config is only applied if the database is empty | |
| initialConfig: { | |
| media: { | |
| enabled: true, | |
| adapter: registerS3MediaAdapter()({ | |
| url: S3_API_URL || "", | |
| access_key: S3_ACCESS_KEY || "", | |
| secret_access_key: S3_SECRET_ACCESS_KEY || "" | |
| }) | |
| }, | |
| }, | |
| } as const satisfies AstroBkndConfig<APIContext>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment