feat: add s3 attachment (#291)

This commit is contained in:
Dream Hunter
2024-06-01 20:08:42 +08:00
committed by GitHub
parent e91bbe273a
commit 9725407c77
18 changed files with 1584 additions and 42 deletions

View File

@@ -0,0 +1,84 @@
import { HonoCustomType } from "../types";
import { Context } from "hono";
import {
S3Client,
ListObjectsV2Command,
GetObjectCommand,
PutObjectCommand
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
export const isS3Enabled = (c: Context<HonoCustomType>) => {
return !(!c.env.S3_ENDPOINT ||
!c.env.S3_ACCESS_KEY_ID ||
!c.env.S3_SECRET_ACCESS_KEY ||
!c.env.S3_BUCKET);
}
const getS3Client = (c: Context<HonoCustomType>) => {
if (
!c.env.S3_ENDPOINT ||
!c.env.S3_ACCESS_KEY_ID ||
!c.env.S3_SECRET_ACCESS_KEY ||
!c.env.S3_BUCKET
) {
throw new Error("S3 config is not set");
}
return new S3Client({
region: "auto",
endpoint: c.env.S3_ENDPOINT,
credentials: {
accessKeyId: c.env.S3_ACCESS_KEY_ID,
secretAccessKey: c.env.S3_SECRET_ACCESS_KEY,
},
});
}
export default {
getSignedGetUrl: async (c: Context<HonoCustomType>) => {
const { address } = c.get("jwtPayload")
const { key } = await c.req.json()
const client = getS3Client(c);
const url = await getSignedUrl(
client,
new GetObjectCommand({
Bucket: c.env.S3_BUCKET,
Key: `${address}/${key}`
}),
{ expiresIn: c.env.S3_URL_EXPIRES || 360 }
);
return c.json({ url });
},
getSignedPutUrl: async (c: Context<HonoCustomType>) => {
const { address } = c.get("jwtPayload")
const { key } = await c.req.json()
const client = getS3Client(c);
const url = await getSignedUrl(
client,
new PutObjectCommand({
Bucket: c.env.S3_BUCKET,
Key: `${address}/${key}`
}),
{ expiresIn: c.env.S3_URL_EXPIRES || 360 }
);
return c.json({ url });
},
list: async (c: Context<HonoCustomType>) => {
const { address } = c.get("jwtPayload")
const client = getS3Client(c);
const data = await client.send(
new ListObjectsV2Command({
Bucket: c.env.S3_BUCKET,
Prefix: `${address}/`
})
);
return c.json(
{
results: data?.Contents
?.map((v) => v.Key?.replace(`${address}/`, ""))
?.filter(k => k)
?.map((k) => ({ key: k }))
}
);
},
}