Add copy and move methods with required import

This commit is contained in:
Andrea Bugeja 2025-12-04 15:47:45 +01:00
parent 12316fca3b
commit 7e83591654

View file

@ -17,6 +17,7 @@
import logging import logging
import time import time
from minio import Minio from minio import Minio
from minio.commonconfig import CopySource
from minio.error import S3Error from minio.error import S3Error
from io import BytesIO from io import BytesIO
from common.decorator import singleton from common.decorator import singleton
@ -199,3 +200,37 @@ class RAGFlowMinio:
self.conn.remove_bucket(bucket) self.conn.remove_bucket(bucket)
except Exception: except Exception:
logging.exception(f"Fail to remove bucket {bucket}") logging.exception(f"Fail to remove bucket {bucket}")
def copy(self, src_bucket, src_path, dest_bucket, dest_path):
try:
if not self.conn.bucket_exists(dest_bucket):
self.conn.make_bucket(dest_bucket)
try:
self.conn.stat_object(src_bucket, src_path)
except Exception as e:
logging.exception(f"Source object not found: {src_bucket}/{src_path}, {e}")
return False
self.conn.copy_object(
dest_bucket,
dest_path,
CopySource(src_bucket, src_path),
)
return True
except Exception:
logging.exception(f"Fail to copy {src_bucket}/{src_path} -> {dest_bucket}/{dest_path}")
return False
def move(self, src_bucket, src_path, dest_bucket, dest_path):
try:
if self.copy(src_bucket, src_path, dest_bucket, dest_path):
self.rm(src_bucket, src_path)
return True
else:
logging.error(f"Copy failed, move aborted: {src_bucket}/{src_path}")
return False
except Exception:
logging.exception(f"Fail to move {src_bucket}/{src_path} -> {dest_bucket}/{dest_path}")
return False