-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set up S3 connector to get presigned PUT URL (#439)
* feat: set up S3 connector to get presigned PUT URL * fix: add more env vars and remove unused schema * fix: add asset router to app router * fix: switch to use mutation and perform logging * chore: add new env vars to turbo.json * chore: add more groups to dependabot
- Loading branch information
Showing
14 changed files
with
152 additions
and
99 deletions.
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