def METHOD_NAME(bucket, key):
    size_limit = bucket.size_limit
    if size_limit and int(content_length or 0) > size_limit:
        desc = 'File size limit exceeded.' \
            if isinstance(size_limit, int) else size_limit.reason
        raise FileSizeError(description=desc)
    with db.session.begin_nested():
        obj = ObjectVersion.create(bucket, key)
        obj.set_contents(
            stream, size=content_length, size_limit=size_limit)
    db.session.commit()
    return obj