def PrefixFromPattern(pattern):
  """Given a path pattern, return the prefix to use in ListKeys.
  If a pattern ends with '*', the prefix is the pattern without the '*', otherwise, we format is as a directory.
  """
  if pattern.endswith('*'):
    return pattern[:-1]
  elif pattern.endswith('/'):
    return '' if pattern == '/' else pattern
  elif pattern == '':
    return pattern
  else:
    return pattern + '/'