def replace_s3_invalid_characters(key):
  """Replaces characters invalid for an S3 object key in a string

  Args:
    key: string where to replace characters

  Returns:
    string where any invalid characters were replaced with underscores
  """

  spec_chars = " !-_'.,*()"
  lst = []
  for char in key:
    if char.isalpha() or char.isdigit() or spec_chars.find(char) >= 0:
      lst.append(char)
    else:
      lst.append('_')
  return ''.join(lst)