feat(bucket): auto-create buckets and move files from temp to main buckets on property save
deploy-chabokan / deploy (push) Canceled after 0s
deploy-chabokan / deploy (push) Canceled after 0s
This commit is contained in:
+28
-5
@@ -1,6 +1,20 @@
|
||||
const minioClient = require('./minio')
|
||||
const fs = require('fs')
|
||||
|
||||
// Ensure buckets exist in MinIO
|
||||
const ensureBucketsExist = async (bucketNames = ['temp', 'properties', 'documents']) => {
|
||||
for (const bucketName of bucketNames) {
|
||||
try {
|
||||
const exists = await minioClient.bucketExists(bucketName).catch(() => false)
|
||||
if (!exists) {
|
||||
await minioClient.makeBucket(bucketName, process.env.AWS_REGION || 'us-east-1')
|
||||
console.log(`MinIO bucket "${bucketName}" created successfully.`)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`Error ensuring MinIO bucket "${bucketName}" exists:`, err.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Upload File to s3 bucket
|
||||
exports.uploadObject = async (file, bucketName) => {
|
||||
@@ -14,11 +28,12 @@ exports.uploadObject = async (file, bucketName) => {
|
||||
const uploadedFile = await minioClient.putObject(bucketName, `${file.filename}-${file.originalName}`, fileStream)
|
||||
|
||||
const protocol = process.env.AWS_USE_SSL === 'true' ? 'https://' : 'http://'
|
||||
const hostPort = process.env.AWS_PORT ? `${process.env.AWS_ENDPOINT}:${process.env.AWS_PORT}` : process.env.AWS_ENDPOINT
|
||||
uploadedFile.fileURL = protocol + hostPort + '/' + bucketName + '/' + file.filename + '-' + file.originalName
|
||||
const endpoint = process.env.AWS_ENDPOINT || 'localhost'
|
||||
const hostPort = (endpoint.includes(':') || !process.env.AWS_PORT) ? endpoint : `${endpoint}:${process.env.AWS_PORT}`
|
||||
const fullHost = hostPort.startsWith('http://') || hostPort.startsWith('https://') ? hostPort : `${protocol}${hostPort}`
|
||||
|
||||
uploadedFile.fileURL = fullHost.replace(/\/$/, '') + '/' + bucketName + '/' + file.filename + '-' + file.originalName
|
||||
uploadedFile.fileName = file.filename + '-' + file.originalName
|
||||
|
||||
uploadedFile.fileEndpoint = process.env.AWS_ENDPOINT
|
||||
|
||||
return uploadedFile
|
||||
@@ -52,10 +67,18 @@ exports.deleteObject = (fileName, bucketName) => {
|
||||
return minioClient.removeObject(bucketName, fileName)
|
||||
}
|
||||
|
||||
exports.copyObject = (targetBucket, fileName, sourceBucketAndFile) => {
|
||||
return minioClient.copyObject(targetBucket, fileName, sourceBucketAndFile)
|
||||
exports.copyObject = async (targetBucket, fileName, sourceBucketAndFile) => {
|
||||
const bucketExists = await minioClient.bucketExists(targetBucket).catch(() => false)
|
||||
if (!bucketExists) {
|
||||
await minioClient.makeBucket(targetBucket, process.env.AWS_REGION || 'us-east-1')
|
||||
}
|
||||
const conds = minioClient.CopyConditions ? new minioClient.CopyConditions() : null
|
||||
return minioClient.copyObject(targetBucket, fileName, sourceBucketAndFile, conds)
|
||||
}
|
||||
|
||||
exports.removeObject = (bucketName, fileName) => {
|
||||
return minioClient.removeObject(bucketName, fileName)
|
||||
}
|
||||
|
||||
exports.ensureBucketsExist = ensureBucketsExist
|
||||
exports.minioClient = minioClient
|
||||
|
||||
Reference in New Issue
Block a user