-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: set up S3 connector to get presigned PUT URL #439
Merged
+152
−99
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f4be05
feat: set up S3 connector to get presigned PUT URL
dcshzj a46da1c
fix: add more env vars and remove unused schema
dcshzj 11c29be
fix: add asset router to app router
dcshzj 766b49e
fix: switch to use mutation and perform logging
dcshzj f4f1e5a
chore: add new env vars to turbo.json
dcshzj 34a2564
chore: add more groups to dependabot
dcshzj 9cbf6c9
fix: add missing env vars in test env
dcshzj 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 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 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 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 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 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 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 was deleted.
Oops, something went wrong.
This file contains 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,26 @@ | ||
import type { PutObjectCommandInput } from "@aws-sdk/client-s3" | ||
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3" | ||
import { getSignedUrl } from "@aws-sdk/s3-request-presigner" | ||
|
||
import { env } from "~/env.mjs" | ||
|
||
const { S3_REGION, S3_UNSAFE_ASSETS_BUCKET_NAME } = env | ||
|
||
export const storage = new S3Client({ | ||
region: S3_REGION, | ||
}) | ||
|
||
export const generateSignedPutUrl = async ({ | ||
Key, | ||
}: Pick<PutObjectCommandInput, "Key">): Promise<string> => { | ||
return getSignedUrl( | ||
storage, | ||
new PutObjectCommand({ | ||
Bucket: S3_UNSAFE_ASSETS_BUCKET_NAME, | ||
Key, | ||
}), | ||
{ | ||
expiresIn: 60 * 5, // 5 minutes | ||
}, | ||
) | ||
} |
This file contains 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,8 @@ | ||
import { z } from "zod" | ||
|
||
export const getPresignedPutUrlSchema = z.object({ | ||
siteId: z.number().min(1), | ||
fileName: z.string({ | ||
required_error: "Missing file name", | ||
}), | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains 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 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,33 @@ | ||
import { getPresignedPutUrlSchema } from "~/schemas/asset" | ||
import { protectedProcedure, router } from "~/server/trpc" | ||
import { getFileKey, getPresignedPutUrl } from "./asset.service" | ||
|
||
export const assetRouter = router({ | ||
getPresignedPutUrl: protectedProcedure | ||
.input(getPresignedPutUrlSchema) | ||
.mutation(async ({ ctx, input: { siteId, fileName } }) => { | ||
const fileKey = getFileKey({ | ||
siteId, | ||
fileName, | ||
}) | ||
|
||
const presignedPutUrl = await getPresignedPutUrl({ | ||
key: fileKey, | ||
}) | ||
|
||
ctx.logger.info( | ||
`Generated presigned PUT URL for ${fileKey} for site ${siteId}`, | ||
{ | ||
userId: ctx.session?.userId, | ||
siteId, | ||
fileName, | ||
fileKey, | ||
}, | ||
) | ||
|
||
return { | ||
fileKey, | ||
presignedPutUrl, | ||
} | ||
}), | ||
}) |
This file contains 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,21 @@ | ||
import { randomUUID } from "crypto" | ||
import type { z } from "zod" | ||
|
||
import type { getPresignedPutUrlSchema } from "~/schemas/asset" | ||
import { generateSignedPutUrl } from "~/lib/s3" | ||
|
||
export const getFileKey = ({ | ||
siteId, | ||
fileName, | ||
}: z.infer<typeof getPresignedPutUrlSchema>) => { | ||
// NOTE: We're using a random folder name to prevent collisions | ||
const folderName = randomUUID() | ||
|
||
return `${siteId}/${folderName}/${fileName}` | ||
} | ||
|
||
export const getPresignedPutUrl = async ({ key }: { key: string }) => { | ||
return generateSignedPutUrl({ | ||
Key: key, | ||
}) | ||
} |
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hwy not just use the resource id since it's guaranteed to be unique
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm this is the asset itself and it's possible to have multiple images within the same page resource, so will need a more globally unique ID.