2024-07-01 17:21:44 +08:00
|
|
|
from collections.abc import Generator
|
|
|
|
|
|
|
|
import boto3
|
|
|
|
from botocore.exceptions import ClientError
|
|
|
|
|
2024-10-22 11:01:32 +08:00
|
|
|
from configs import dify_config
|
2024-07-01 17:21:44 +08:00
|
|
|
from extensions.storage.base_storage import BaseStorage
|
|
|
|
|
|
|
|
|
2024-10-09 14:15:27 +08:00
|
|
|
class OracleOCIStorage(BaseStorage):
|
|
|
|
"""Implementation for Oracle OCI storage."""
|
|
|
|
|
2024-10-22 11:01:32 +08:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
self.bucket_name = dify_config.OCI_BUCKET_NAME
|
2024-07-01 17:21:44 +08:00
|
|
|
self.client = boto3.client(
|
2024-08-15 12:54:05 +08:00
|
|
|
"s3",
|
2024-10-22 11:01:32 +08:00
|
|
|
aws_secret_access_key=dify_config.OCI_SECRET_KEY,
|
|
|
|
aws_access_key_id=dify_config.OCI_ACCESS_KEY,
|
|
|
|
endpoint_url=dify_config.OCI_ENDPOINT,
|
|
|
|
region_name=dify_config.OCI_REGION,
|
2024-08-15 12:54:05 +08:00
|
|
|
)
|
2024-07-01 17:21:44 +08:00
|
|
|
|
|
|
|
def save(self, filename, data):
|
|
|
|
self.client.put_object(Bucket=self.bucket_name, Key=filename, Body=data)
|
|
|
|
|
|
|
|
def load_once(self, filename: str) -> bytes:
|
|
|
|
try:
|
2024-10-15 08:42:39 +08:00
|
|
|
data = self.client.get_object(Bucket=self.bucket_name, Key=filename)["Body"].read()
|
2024-07-01 17:21:44 +08:00
|
|
|
except ClientError as ex:
|
2024-08-15 12:54:05 +08:00
|
|
|
if ex.response["Error"]["Code"] == "NoSuchKey":
|
2024-07-01 17:21:44 +08:00
|
|
|
raise FileNotFoundError("File not found")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
return data
|
|
|
|
|
|
|
|
def load_stream(self, filename: str) -> Generator:
|
2024-10-25 10:11:25 +08:00
|
|
|
try:
|
|
|
|
response = self.client.get_object(Bucket=self.bucket_name, Key=filename)
|
|
|
|
yield from response["Body"].iter_chunks()
|
|
|
|
except ClientError as ex:
|
|
|
|
if ex.response["Error"]["Code"] == "NoSuchKey":
|
|
|
|
raise FileNotFoundError("File not found")
|
|
|
|
else:
|
|
|
|
raise
|
2024-07-01 17:21:44 +08:00
|
|
|
|
|
|
|
def download(self, filename, target_filepath):
|
2024-10-15 08:42:39 +08:00
|
|
|
self.client.download_file(self.bucket_name, filename, target_filepath)
|
2024-07-01 17:21:44 +08:00
|
|
|
|
|
|
|
def exists(self, filename):
|
2024-10-15 08:42:39 +08:00
|
|
|
try:
|
|
|
|
self.client.head_object(Bucket=self.bucket_name, Key=filename)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2024-07-01 17:21:44 +08:00
|
|
|
|
|
|
|
def delete(self, filename):
|
2024-08-15 12:54:05 +08:00
|
|
|
self.client.delete_object(Bucket=self.bucket_name, Key=filename)
|