Add missing S3Bucket class to fix import error
- Implement S3Bucket class in utils.py for legacy compatibility - Add upload_file and get_file_url methods - Fix ImportError that was preventing app startup
This commit is contained in:
59
rog/utils.py
59
rog/utils.py
@ -378,3 +378,62 @@ class S3Bucket:
|
||||
logger.error(f"予期しないエラーが発生しました: {str(e)}")
|
||||
return False
|
||||
|
||||
|
||||
class S3Bucket:
|
||||
"""
|
||||
レガシーS3Bucketクラス - 既存コードとの互換性のため
|
||||
"""
|
||||
def __init__(self, bucket_name):
|
||||
self.bucket_name = bucket_name
|
||||
try:
|
||||
self.s3_client = boto3.client(
|
||||
's3',
|
||||
aws_access_key_id=getattr(settings, 'AWS_ACCESS_KEY', None),
|
||||
aws_secret_access_key=getattr(settings, 'AWS_SECRET_ACCESS_KEY', None),
|
||||
region_name=getattr(settings, 'AWS_REGION', 'us-west-2')
|
||||
)
|
||||
logger.info(f"S3Bucket initialized for bucket: {self.bucket_name}")
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to initialize S3Bucket: {e}")
|
||||
self.s3_client = None
|
||||
|
||||
def upload_file(self, local_file_path, s3_key):
|
||||
"""
|
||||
ローカルファイルをS3にアップロード
|
||||
|
||||
Args:
|
||||
local_file_path: アップロードするローカルファイルのパス
|
||||
s3_key: S3内でのキー(ファイルパス)
|
||||
|
||||
Returns:
|
||||
bool: 成功時True、失敗時False
|
||||
"""
|
||||
if not self.s3_client:
|
||||
logger.error("S3 client not initialized")
|
||||
return False
|
||||
|
||||
try:
|
||||
self.s3_client.upload_file(
|
||||
local_file_path,
|
||||
self.bucket_name,
|
||||
s3_key,
|
||||
ExtraArgs={'ACL': 'public-read'}
|
||||
)
|
||||
logger.info(f"Successfully uploaded {local_file_path} to s3://{self.bucket_name}/{s3_key}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to upload {local_file_path} to S3: {e}")
|
||||
return False
|
||||
|
||||
def get_file_url(self, s3_key):
|
||||
"""
|
||||
S3ファイルのパブリックURLを生成
|
||||
|
||||
Args:
|
||||
s3_key: S3内でのキー(ファイルパス)
|
||||
|
||||
Returns:
|
||||
str: ファイルのURL
|
||||
"""
|
||||
aws_region = getattr(settings, 'AWS_REGION', 'us-west-2')
|
||||
return f"https://{self.bucket_name}.s3.{aws_region}.amazonaws.com/{s3_key}"
|
||||
|
||||
Reference in New Issue
Block a user