fix(storage): 🐛 Create S3 bucket if it doesn't exist (#7514)

Co-authored-by: 莫岳恒 <moyueheng@datagrand.com>
This commit is contained in:
Vimpas 2024-08-22 09:45:42 +08:00 committed by GitHub
parent 2c427e04be
commit 0006c6f0fd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -28,6 +28,16 @@ class S3Storage(BaseStorage):
region_name=app_config.get("S3_REGION"), region_name=app_config.get("S3_REGION"),
config=Config(s3={"addressing_style": app_config.get("S3_ADDRESS_STYLE")}), config=Config(s3={"addressing_style": app_config.get("S3_ADDRESS_STYLE")}),
) )
# 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
def save(self, filename, data): def save(self, filename, data):
self.client.put_object(Bucket=self.bucket_name, Key=filename, Body=data) self.client.put_object(Bucket=self.bucket_name, Key=filename, Body=data)