import { CreateBucketCommand, HeadObjectCommand, S3Client, } from '@aws-sdk/client-s3'; import { OssBase } from '@kevisual/oss/s3.ts'; import { useConfig } from '@kevisual/use-config'; const config = useConfig(); export const bucketName = config.S3_BUCKET_NAME || 'resources'; export const s3Client = new S3Client({ credentials: { accessKeyId: config.S3_ACCESS_KEY_ID || '', secretAccessKey: config.S3_SECRET_ACCESS_KEY || '', }, region: config.S3_REGION, endpoint: config.S3_ENDPOINT, // minio配置 forcePathStyle: true, }); // 判断 bucketName 是否存在,不存在则创建 (async () => { try { await s3Client.send(new HeadObjectCommand({ Bucket: bucketName, Key: '' })); console.log(`Bucket ${bucketName} exists.`); } catch (error) { console.log(`Bucket ${bucketName} does not exist. Creating...`); if (config.S3_ENDPOINT?.includes?.('9000')) { // 创建 bucket await s3Client.send(new CreateBucketCommand({ Bucket: bucketName })); console.log(`Bucket ${bucketName} created.`); } } })(); if (!s3Client) { throw new Error('S3 client not initialized'); } export const oss = new OssBase({ client: s3Client, bucketName: bucketName, prefix: '', }) export const minioResources = `${config.S3_ENDPOINT}/${bucketName}`;