-
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 3 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. Important to note that a new environment variable is required on the image optimization server for this override to work: `BUCKET_REGION`. | ||
|
|
||
| ```ts | ||
| import type { OpenNextConfig } from '@opennextjs/aws/types/open-next'; | ||
| const config = { | ||
| default: {}, | ||
| imageOptimization: { | ||
| loader: 's3-lite', // make sure you have the required env variables defined | ||
| }, | ||
| } 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
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,67 @@ | ||
| 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"; | ||
| import { customFetchClient } from "utils/fetch"; | ||
|
|
||
| let awsClient: AwsClient | null = null; | ||
|
|
||
| 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: process.env.BUCKET_REGION, | ||
sommeeeer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
| return awsClient; | ||
| }; | ||
|
|
||
| const { BUCKET_NAME, BUCKET_KEY_PREFIX, BUCKET_REGION } = process.env; | ||
|
|
||
| function ensureEnvExists() { | ||
| if (!(BUCKET_NAME || BUCKET_REGION || BUCKET_KEY_PREFIX)) { | ||
| throw new Error("Bucket name, region and key prefix must be defined!"); | ||
sommeeeer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| const awsFetch = async (key: string, options: RequestInit) => { | ||
| const client = getAwsClient(); | ||
| const url = `https://${BUCKET_NAME}.s3.${BUCKET_REGION}.amazonaws.com/${key}`; | ||
| return customFetchClient(client)(url, options); | ||
sommeeeer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| const s3Loader: ImageLoader = { | ||
| name: "s3", | ||
sommeeeer marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| load: async (key: string) => { | ||
| ensureEnvExists(); | ||
| const keyPrefix = BUCKET_KEY_PREFIX?.replace(/^\/|\/$/g, ""); | ||
| const response = await awsFetch( | ||
| keyPrefix | ||
| ? `${keyPrefix}/${key.replace(/^\//, "")}` | ||
| : key.replace(/^\//, ""), | ||
| { | ||
| method: "GET", | ||
| }, | ||
| ); | ||
|
|
||
sommeeeer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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.