mirror of
https://github.com/dreamhunter2333/cloudflare_temp_email.git
synced 2026-07-04 22:01:42 +08:00
feat: add s3 attachment (#291)
This commit is contained in:
84
worker/src/mails_api/s3_attachment.ts
Normal file
84
worker/src/mails_api/s3_attachment.ts
Normal 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 }))
|
||||
}
|
||||
);
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user