This commit is contained in:
root
2026-06-07 23:18:53 +08:00
parent ebd3d6a317
commit 934b9a4a23
36 changed files with 2272 additions and 296 deletions

View File

@@ -6,12 +6,14 @@
import * as Minio from "minio";
import { getMinioConfig } from "./config";
import { getThemeConfig } from "@/config/digital/site.config";
import type { MinioConfig } from "./config";
const ensuredBuckets = new Set<string>();
/** 获取 MinIO 客户端实例(使用主题配置) */
export function getMinioClient(): Minio.Client {
export function getMinioClient(config?: MinioConfig): Minio.Client {
const theme = getThemeConfig();
const cfg = getMinioConfig(theme.services?.minio);
const cfg = config || getMinioConfig(theme.services?.minio);
return new Minio.Client({
endPoint: cfg.endPoint,
port: cfg.port,
@@ -23,6 +25,29 @@ export function getMinioClient(): Minio.Client {
});
}
export async function ensurePublicBucket(client: Minio.Client, bucket: string): Promise<void> {
if (ensuredBuckets.has(bucket)) return;
const exists = await client.bucketExists(bucket);
if (!exists) {
await client.makeBucket(bucket);
}
await client.setBucketPolicy(
bucket,
JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Principal: { AWS: ["*"] },
Action: ["s3:GetObject"],
Resource: [`arn:aws:s3:::${bucket}/*`],
},
],
})
);
ensuredBuckets.add(bucket);
}
export interface UploadResult {
url: string;
objectKey: string;
@@ -46,7 +71,8 @@ export async function uploadBuffer(
throw new Error("MinIO 存储未启用");
}
const client = getMinioClient();
const client = getMinioClient(cfg);
await ensurePublicBucket(client, cfg.bucket);
await client.putObject(cfg.bucket, objectKey, buffer, buffer.length, {
"Content-Type": contentType,
});