-
Notifications
You must be signed in to change notification settings - Fork 177
add: s3-lite override for imageLoader #750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7148d08
add: s3 lite for imageLoader
sommeeeer fe29ca1
lint
sommeeeer 499d3f2
fix
sommeeeer c38e65c
review
sommeeeer 9bd2290
fix changeset
sommeeeer fada51f
fallback region
sommeeeer 7ea5029
changeset
sommeeeer aa76b0c
review
sommeeeer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| --- | ||
| "@opennextjs/aws": patch | ||
| --- | ||
|
|
||
| add: s3 lite override for loading images in the image optimization server | ||
|
|
||
| `s3-lite` override for image loading. Uses `aws4fetch` to get the objects from your s3 bucket. This will make the image optimization server work without the aws s3 sdk. This override introduces a new environment variable called `BUCKET_REGION`. It will fallback to `AWS_REGION` ?? `AWS_DEFAULT_REGION` if undefined. This will require no additional change in IAC for most users. | ||
|
|
||
| ```ts | ||
| import type { OpenNextConfig } from '@opennextjs/aws/types/open-next'; | ||
| const config = { | ||
| default: {}, | ||
| imageOptimization: { | ||
| loader: 's3-lite', | ||
| }, | ||
| } satisfies OpenNextConfig; | ||
|
|
||
| export default config; | ||
| ``` |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { Readable } from "node:stream"; | ||
| import type { ReadableStream } from "node:stream/web"; | ||
| import { AwsClient } from "aws4fetch"; | ||
|
|
||
| import type { ImageLoader } from "types/overrides"; | ||
| import { FatalError } from "utils/error"; | ||
|
|
||
| let awsClient: AwsClient | null = null; | ||
|
|
||
| const { BUCKET_NAME, BUCKET_KEY_PREFIX } = process.env; | ||
| // https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime | ||
| // If AWS_REGION is defined it will override AWS_DEFAULT_REGION | ||
| const BUCKET_REGION = | ||
| process.env.BUCKET_REGION ?? | ||
| process.env.AWS_REGION ?? | ||
| process.env.AWS_DEFAULT_REGION; | ||
|
|
||
| const getAwsClient = () => { | ||
| if (awsClient) { | ||
| return awsClient; | ||
| } | ||
| awsClient = new AwsClient({ | ||
| accessKeyId: process.env.AWS_ACCESS_KEY_ID!, | ||
| secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!, | ||
| sessionToken: process.env.AWS_SESSION_TOKEN, | ||
| region: BUCKET_REGION, | ||
| }); | ||
| return awsClient; | ||
| }; | ||
|
|
||
| function ensureEnvExists() { | ||
| if (!(BUCKET_NAME || BUCKET_KEY_PREFIX || BUCKET_REGION)) { | ||
| throw new FatalError("Bucket name, region and key prefix must be defined!"); | ||
| } | ||
| } | ||
|
|
||
| const awsFetch = async (key: string, options: RequestInit) => { | ||
| const client = getAwsClient(); | ||
| const url = `https://${BUCKET_NAME}.s3.${BUCKET_REGION}.amazonaws.com/${key}`; | ||
| return client.fetch(url, options); | ||
| }; | ||
|
|
||
| const s3Loader: ImageLoader = { | ||
| name: "s3-lite", | ||
| load: async (key: string) => { | ||
| ensureEnvExists(); | ||
| const keyPrefix = BUCKET_KEY_PREFIX?.replace(/^\/|\/$/g, ""); | ||
| const response = await awsFetch( | ||
| keyPrefix | ||
| ? `${keyPrefix}/${key.replace(/^\//, "")}` | ||
| : key.replace(/^\//, ""), | ||
| { | ||
| method: "GET", | ||
| }, | ||
| ); | ||
|
|
||
| if (response.status === 404) { | ||
| throw new FatalError("The specified key does not exist."); | ||
| } | ||
| if (response.status !== 200) { | ||
| throw new FatalError( | ||
| `Failed to get image. Status code: ${response.status}`, | ||
| ); | ||
| } | ||
|
|
||
| if (!response.body) { | ||
| throw new FatalError("No body in aws4fetch s3 response"); | ||
| } | ||
|
|
||
| // We need to cast it else there will be a TypeError: o.pipe is not a function | ||
| const body = Readable.fromWeb(response.body as ReadableStream<Uint8Array>); | ||
|
|
||
| return { | ||
| body: body, | ||
| contentType: response.headers.get("content-type") ?? undefined, | ||
| cacheControl: response.headers.get("cache-control") ?? undefined, | ||
| }; | ||
| }, | ||
| }; | ||
|
|
||
| export default s3Loader; | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.