2024-04-29 18:22:03 +08:00
|
|
|
from collections.abc import Generator
|
|
|
|
from contextlib import closing
|
|
|
|
|
|
|
|
import boto3
|
|
|
|
from botocore.client import Config
|
|
|
|
from botocore.exceptions import ClientError
|
|
|
|
from flask import Flask
|
|
|
|
|
|
|
|
from extensions.storage.base_storage import BaseStorage
|
|
|
|
|
|
|
|
|
|
|
|
class S3Storage(BaseStorage):
|
2024-08-15 12:54:05 +08:00
|
|
|
"""Implementation for s3 storage."""
|
|
|
|
|
2024-04-29 18:22:03 +08:00
|
|
|
def __init__(self, app: Flask):
|
|
|
|
super().__init__(app)
|
|
|
|
app_config = self.app.config
|
2024-08-15 12:54:05 +08:00
|
|
|
self.bucket_name = app_config.get("S3_BUCKET_NAME")
|
|
|
|
if app_config.get("S3_USE_AWS_MANAGED_IAM"):
|
2024-06-14 15:19:59 +08:00
|
|
|
session = boto3.Session()
|
2024-08-15 12:54:05 +08:00
|
|
|
self.client = session.client("s3")
|
2024-06-14 15:19:59 +08:00
|
|
|
else:
|
|
|
|
self.client = boto3.client(
|
2024-08-15 12:54:05 +08:00
|
|
|
"s3",
|
|
|
|
aws_secret_access_key=app_config.get("S3_SECRET_KEY"),
|
|
|
|
aws_access_key_id=app_config.get("S3_ACCESS_KEY"),
|
|
|
|
endpoint_url=app_config.get("S3_ENDPOINT"),
|
|
|
|
region_name=app_config.get("S3_REGION"),
|
|
|
|
config=Config(s3={"addressing_style": app_config.get("S3_ADDRESS_STYLE")}),
|
|
|
|
)
|
2024-08-22 09:45:42 +08:00
|
|
|
# create bucket
|
|
|
|
try:
|
|
|
|
self.client.head_bucket(Bucket=self.bucket_name)
|
|
|
|
except ClientError as e:
|
|
|
|
# if bucket not exists, create it
|
|
|
|
if e.response["Error"]["Code"] == "404":
|
|
|
|
self.client.create_bucket(Bucket=self.bucket_name)
|
|
|
|
else:
|
|
|
|
# other error, raise exception
|
|
|
|
raise
|
2024-04-29 18:22:03 +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:
|
|
|
|
with closing(self.client) as client:
|
2024-08-15 12:54:05 +08:00
|
|
|
data = client.get_object(Bucket=self.bucket_name, Key=filename)["Body"].read()
|
2024-04-29 18:22:03 +08:00
|
|
|
except ClientError as ex:
|
2024-08-15 12:54:05 +08:00
|
|
|
if ex.response["Error"]["Code"] == "NoSuchKey":
|
2024-04-29 18:22:03 +08:00
|
|
|
raise FileNotFoundError("File not found")
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
return data
|
|
|
|
|
|
|
|
def load_stream(self, filename: str) -> Generator:
|
|
|
|
def generate(filename: str = filename) -> Generator:
|
|
|
|
try:
|
|
|
|
with closing(self.client) as client:
|
|
|
|
response = client.get_object(Bucket=self.bucket_name, Key=filename)
|
2024-08-15 12:54:05 +08:00
|
|
|
yield from response["Body"].iter_chunks()
|
2024-04-29 18:22:03 +08:00
|
|
|
except ClientError as ex:
|
2024-08-15 12:54:05 +08:00
|
|
|
if ex.response["Error"]["Code"] == "NoSuchKey":
|
2024-04-29 18:22:03 +08:00
|
|
|
raise FileNotFoundError("File not found")
|
|
|
|
else:
|
|
|
|
raise
|
2024-08-15 12:54:05 +08:00
|
|
|
|
2024-04-29 18:22:03 +08:00
|
|
|
return generate()
|
|
|
|
|
|
|
|
def download(self, filename, target_filepath):
|
|
|
|
with closing(self.client) as client:
|
|
|
|
client.download_file(self.bucket_name, filename, target_filepath)
|
|
|
|
|
|
|
|
def exists(self, filename):
|
|
|
|
with closing(self.client) as client:
|
|
|
|
try:
|
|
|
|
client.head_object(Bucket=self.bucket_name, Key=filename)
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def delete(self, filename):
|
|
|
|
self.client.delete_object(Bucket=self.bucket_name, Key=filename)
|