import typing


def group_s3_objects_no_larger_than(key_and_size_list: typing.List[typing.Tuple[str, int]],
                                    max_size: int) -> typing.List[typing.List[str]]:
    """
    Given lots of s3 objects key and their size in bytes, with a max size
    limitation, group them into different group that the total file size of
    each group is as large as possible but not pass max_size settings.

    Return a list of s3 key group, each s3 key group is just a list of string
    (list of s3 key)
    """
    group_list: typing.List[typing.List[str]] = list()
    s3_object_group: typing.List[str] = [key_and_size_list[0][0], ]
    cumulated_size = key_and_size_list[0][1]
    for key, size in key_and_size_list[1:]:
        # if add this one will surpass the max_size
        if cumulated_size + size < max_size:
            s3_object_group.append(key)
            cumulated_size += size
        else:
            group_list.append(s3_object_group)
            s3_object_group = [key, ]
            cumulated_size = size

    if len(s3_object_group):
        group_list.append(s3_object_group)

    return group_list