2023-01-28 18:42:22 +00:00
|
|
|
from boto3 import (
|
|
|
|
Session,
|
|
|
|
)
|
|
|
|
from io import BytesIO
|
2023-01-28 23:09:19 +00:00
|
|
|
from logging import getLogger
|
2023-01-28 18:42:22 +00:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
from ..params import (
|
|
|
|
ImageParams,
|
|
|
|
StageParams,
|
|
|
|
)
|
|
|
|
from ..utils import (
|
|
|
|
ServerContext,
|
|
|
|
)
|
|
|
|
|
2023-01-28 23:09:19 +00:00
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
2023-01-28 18:42:22 +00:00
|
|
|
|
|
|
|
def persist_s3(
|
|
|
|
_ctx: ServerContext,
|
|
|
|
_stage: StageParams,
|
|
|
|
_params: ImageParams,
|
|
|
|
source_image: Image.Image,
|
|
|
|
*,
|
|
|
|
output: str,
|
|
|
|
bucket: str,
|
|
|
|
endpoint_url: str = None,
|
|
|
|
profile_name: str = None,
|
|
|
|
) -> Image.Image:
|
2023-01-28 20:56:06 +00:00
|
|
|
session = Session(profile_name=profile_name)
|
|
|
|
s3 = session.client('s3', endpoint_url=endpoint_url)
|
2023-01-28 18:42:22 +00:00
|
|
|
|
|
|
|
data = BytesIO()
|
|
|
|
source_image.save(data, format='png')
|
2023-01-28 20:56:06 +00:00
|
|
|
data.seek(0)
|
2023-01-28 18:42:22 +00:00
|
|
|
|
|
|
|
try:
|
2023-01-28 23:09:19 +00:00
|
|
|
s3.upload_fileobj(data, bucket, output)
|
|
|
|
logger.info('saved image to %s/%s', bucket, output)
|
2023-01-28 20:56:06 +00:00
|
|
|
except Exception as err:
|
2023-01-28 23:09:19 +00:00
|
|
|
logger.error('error saving image to S3: %s', err)
|
2023-01-28 18:42:22 +00:00
|
|
|
|
|
|
|
return source_image
|