feat: support custom s3 service

This commit is contained in:
Hk-Gosuto
2023-12-10 21:02:49 +08:00
parent eb7711f832
commit b84da5e120
6 changed files with 88 additions and 35 deletions

View File

@@ -1,6 +1,5 @@
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import S3FileStorage from "../../../utils/r2_file_storage";
import S3FileStorage from "../../../utils/s3_file_storage";
async function handle(
req: NextRequest,
@@ -10,13 +9,6 @@ async function handle(
return NextResponse.json({ body: "OK" }, { status: 200 });
}
// const authResult = auth(req);
// if (authResult.error) {
// return NextResponse.json(authResult, {
// status: 401,
// });
// }
try {
var file = await S3FileStorage.get(params.path[0]);
return new Response(file?.transformToWebStream(), {
@@ -34,3 +26,4 @@ async function handle(
export const GET = handle;
export const runtime = "edge";
export const revalidate = 0;

View File

@@ -1,6 +1,6 @@
import { StructuredTool } from "langchain/tools";
import { z } from "zod";
import S3FileStorage from "../../utils/r2_file_storage";
import S3FileStorage from "../../utils/s3_file_storage";
export class DallEAPIWrapper extends StructuredTool {
name = "dalle_image_generator";

View File

@@ -1,5 +1,5 @@
import { Tool } from "langchain/tools";
import S3FileStorage from "../../utils/r2_file_storage";
import S3FileStorage from "../../utils/s3_file_storage";
export class StableDiffusionWrapper extends Tool {
name = "stable_diffusion_image_generator";

View File

@@ -12,13 +12,19 @@ const R2_ACCESS_KEY_ID = process.env.R2_ACCESS_KEY_ID;
const R2_SECRET_ACCESS_KEY = process.env.R2_SECRET_ACCESS_KEY;
const R2_BUCKET = process.env.R2_BUCKET;
const S3_ENDPOINT = process.env.S3_ENDPOINT;
const S3_ACCESS_KEY_ID = process.env.S3_ACCESS_KEY_ID;
const S3_SECRET_ACCESS_KEY = process.env.S3_SECRET_ACCESS_KEY;
const S3_BUCKET = process.env.S3_BUCKET;
const getR2Client = () => {
return new S3Client({
region: "auto",
endpoint: `https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
endpoint:
S3_ENDPOINT ?? `https://${R2_ACCOUNT_ID}.r2.cloudflarestorage.com`,
credentials: {
accessKeyId: R2_ACCESS_KEY_ID!,
secretAccessKey: R2_SECRET_ACCESS_KEY!,
accessKeyId: S3_ACCESS_KEY_ID ?? R2_ACCESS_KEY_ID!,
secretAccessKey: S3_SECRET_ACCESS_KEY ?? R2_SECRET_ACCESS_KEY!,
},
});
};
@@ -27,7 +33,7 @@ export default class S3FileStorage {
static async get(fileName: string) {
const file = await getR2Client().send(
new GetObjectCommand({
Bucket: R2_BUCKET,
Bucket: S3_BUCKET ?? R2_BUCKET,
Key: fileName,
}),
);
@@ -43,7 +49,7 @@ export default class S3FileStorage {
const signedUrl = await getSignedUrl(
getR2Client(),
new PutObjectCommand({
Bucket: R2_BUCKET,
Bucket: S3_BUCKET ?? R2_BUCKET,
Key: fileName,
}),
{ expiresIn: 60 },
@@ -59,7 +65,7 @@ export default class S3FileStorage {
return `/api/file/${fileName}`;
} catch (e) {
console.error("[R2]", e);
console.error("[S3]", e);
throw e;
}
}