# Buggy code
def expand_multipart_filename(data, headers):
    """Replace instance of '${filename}' in key with given file name.

    Data is given as multipart form submission bytes, and file name is
    replace according to Amazon S3 documentation for Post uploads:
    http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html
    """
    _, params = cgi.parse_header(headers.get("Content-Type"))

    if "boundary" not in params:
        return data

    boundary = params["boundary"].encode("ascii")
    data_bytes = to_bytes(data)

    filename = None

    for disposition, _ in _iter_multipart_parts(data_bytes, boundary):
        if disposition.get("name") == "file" and "filename" in disposition:
            filename = disposition["filename"]
            break

    if filename is None:
        # Found nothing, return unaltered
        return data

    for disposition, part in _iter_multipart_parts(data_bytes, boundary):
        if disposition.get("name") == "key" and b"${filename}" in part:
            search = boundary + part
            replace = boundary + part.replace(b"${filename}", filename.encode("utf8"))

            if search in data_bytes:
                return data_bytes.replace(search, replace)

    return data


# Traceback (TypeError)
2017-10-10T10:58:34:ERROR:localstack.services.generic_proxy: Error forwarding request: string indices must be integers Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/localstack/services/generic_proxy.py", line 194, in forward
path=path, data=data, headers=forward_headers, response=response)
File "/usr/local/lib/python3.6/site-packages/localstack/services/s3/s3_listener.py", line 436, in return_response
send_notifications(method, bucket_name, object_path)
File "/usr/local/lib/python3.6/site-packages/localstack/services/s3/s3_listener.py", line 122, in send_notifications
filter_rules_match(config.get('Filter'), object_path)):
File "/usr/local/lib/python3.6/site-packages/localstack/services/s3/s3_listener.py", line 50, in filter_rules_match
if rule['Name'] == 'prefix':
TypeError: string indices must be integers

# Fixed code
def expand_multipart_filename(data, headers):
    """Replace instance of '${filename}' in key with given file name.

    Data is given as multipart form submission bytes, and file name is
    replace according to Amazon S3 documentation for Post uploads:
    http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPOST.html
    """
    _, params = cgi.parse_header(headers.get("Content-Type", ""))

    if "boundary" not in params:
        return data

    boundary = params["boundary"].encode("ascii")
    data_bytes = to_bytes(data)

    filename = None

    for disposition, _ in _iter_multipart_parts(data_bytes, boundary):
        if disposition.get("name") == "file" and "filename" in disposition:
            filename = disposition["filename"]
            break

    if filename is None:
        # Found nothing, return unaltered
        return data

    for disposition, part in _iter_multipart_parts(data_bytes, boundary):
        if disposition.get("name") == "key" and b"${filename}" in part:
            search = boundary + part
            replace = boundary + part.replace(b"${filename}", filename.encode("utf8"))

            if search in data_bytes:
                return data_bytes.replace(search, replace)

    return data