def upload_image_to_s3( bucket: Bucket, file_name: str, content_type: Optional[str], user_profile: UserProfile, contents: bytes, ) -> None: key = bucket.Object(file_name) metadata = { "user_profile_id": str(user_profile.id), "realm_id": str(user_profile.realm_id), } content_disposition = "" if content_type is None: content_type = "" if content_type not in INLINE_MIME_TYPES: content_disposition = "attachment" key.put( Body=contents, Metadata=metadata, ContentType=content_type, ContentDisposition=content_disposition, )
def delete_file_from_s3(self, path_id: str, bucket: Bucket) -> bool: key = bucket.Object(path_id) try: key.load() except botocore.exceptions.ClientError: file_name = path_id.split("/")[-1] logging.warning( "%s does not exist. Its entry in the database will be removed.", file_name ) return False key.delete() return True