fix(file): S3 file storage resolves the issue of inconsistent end_point and region.
This commit is contained in:
@@ -114,6 +114,7 @@ class Settings:
|
|||||||
S3_ACCESS_KEY_ID: str = os.getenv("S3_ACCESS_KEY_ID", "")
|
S3_ACCESS_KEY_ID: str = os.getenv("S3_ACCESS_KEY_ID", "")
|
||||||
S3_SECRET_ACCESS_KEY: str = os.getenv("S3_SECRET_ACCESS_KEY", "")
|
S3_SECRET_ACCESS_KEY: str = os.getenv("S3_SECRET_ACCESS_KEY", "")
|
||||||
S3_BUCKET_NAME: str = os.getenv("S3_BUCKET_NAME", "")
|
S3_BUCKET_NAME: str = os.getenv("S3_BUCKET_NAME", "")
|
||||||
|
S3_ENDPOINT_URL: str = os.getenv("S3_ENDPOINT_URL", "")
|
||||||
|
|
||||||
# VOLC ASR settings
|
# VOLC ASR settings
|
||||||
VOLC_APP_KEY: str = os.getenv("VOLC_APP_KEY", "")
|
VOLC_APP_KEY: str = os.getenv("VOLC_APP_KEY", "")
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ class StorageFactory:
|
|||||||
access_key_id=settings.S3_ACCESS_KEY_ID,
|
access_key_id=settings.S3_ACCESS_KEY_ID,
|
||||||
secret_access_key=settings.S3_SECRET_ACCESS_KEY,
|
secret_access_key=settings.S3_SECRET_ACCESS_KEY,
|
||||||
bucket_name=settings.S3_BUCKET_NAME,
|
bucket_name=settings.S3_BUCKET_NAME,
|
||||||
|
endpoint_url=settings.S3_ENDPOINT_URL,
|
||||||
)
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -35,6 +35,19 @@ class S3Storage(StorageBackend):
|
|||||||
bucket_name: The name of the S3 bucket.
|
bucket_name: The name of the S3 bucket.
|
||||||
region: The AWS region.
|
region: The AWS region.
|
||||||
"""
|
"""
|
||||||
|
AMAZON_S3_ENDPOINT_MAP = {
|
||||||
|
"us-east-1": "https://s3.us-east-1.amazonaws.com", # 特殊:无地域后缀
|
||||||
|
"us-east-2": "https://s3.us-east-2.amazonaws.com",
|
||||||
|
"us-west-1": "https://s3.us-west-1.amazonaws.com",
|
||||||
|
"us-west-2": "https://s3.us-west-2.amazonaws.com",
|
||||||
|
"ap-east-1": "https://s3.ap-east-1.amazonaws.com", # 香港
|
||||||
|
"ap-southeast-1": "https://s3.ap-southeast-1.amazonaws.com", # 新加坡
|
||||||
|
"ap-southeast-2": "https://s3.ap-southeast-2.amazonaws.com", # 悉尼
|
||||||
|
"ap-northeast-1": "https://s3.ap-northeast-1.amazonaws.com", # 东京
|
||||||
|
"eu-central-1": "https://s3.eu-central-1.amazonaws.com", # 法兰克福
|
||||||
|
"eu-west-1": "https://s3.eu-west-1.amazonaws.com", # 爱尔兰
|
||||||
|
# 可根据需要扩展其他地域
|
||||||
|
}
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
@@ -42,6 +55,7 @@ class S3Storage(StorageBackend):
|
|||||||
access_key_id: str,
|
access_key_id: str,
|
||||||
secret_access_key: str,
|
secret_access_key: str,
|
||||||
bucket_name: str,
|
bucket_name: str,
|
||||||
|
endpoint_url: Optional[str] = None
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Initialize the S3Storage backend.
|
Initialize the S3Storage backend.
|
||||||
@@ -51,6 +65,7 @@ class S3Storage(StorageBackend):
|
|||||||
access_key_id: The AWS access key ID.
|
access_key_id: The AWS access key ID.
|
||||||
secret_access_key: The AWS secret access key.
|
secret_access_key: The AWS secret access key.
|
||||||
bucket_name: The name of the S3 bucket.
|
bucket_name: The name of the S3 bucket.
|
||||||
|
endpoint_url: The complete URL to use for the constructed client.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
StorageConfigError: If any required configuration is missing.
|
StorageConfigError: If any required configuration is missing.
|
||||||
@@ -69,10 +84,19 @@ class S3Storage(StorageBackend):
|
|||||||
self.region = region
|
self.region = region
|
||||||
self.bucket_name = bucket_name
|
self.bucket_name = bucket_name
|
||||||
|
|
||||||
|
if not endpoint_url:
|
||||||
|
# 优先匹配内置映射表(解决特殊地域)
|
||||||
|
if region in self.AMAZON_S3_ENDPOINT_MAP:
|
||||||
|
endpoint_url = self.AMAZON_S3_ENDPOINT_MAP[region]
|
||||||
|
# 兜底:通用拼接(适配未配置的新地域)
|
||||||
|
else:
|
||||||
|
endpoint_url = f"https://s3.{region}.amazonaws.com"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.client = boto3.client(
|
self.client = boto3.client(
|
||||||
"s3",
|
"s3",
|
||||||
region_name=region,
|
region_name=region,
|
||||||
|
endpoint_url=endpoint_url,
|
||||||
aws_access_key_id=access_key_id,
|
aws_access_key_id=access_key_id,
|
||||||
aws_secret_access_key=secret_access_key,
|
aws_secret_access_key=secret_access_key,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user