def get_bucket_and_path_from(full_path): """Get the bucket and object path. Args: full_path (str): The full GCS path. Must be in the format gs://bucket-name/path/to/object Returns: tuple: The bucket name and object path. Ex. (bucket-name, path/to/object) Raises: InvalidBucketPathError: Raised if the full path cannot be parsed or does not look like a GCS bucket URL. """ try: parsed = urlparse.urlparse(full_path) except AttributeError as e: LOGGER.warn('Could not parse path %s: %s', full_path, e) parsed = None if not parsed or parsed.scheme != GCS_SCHEME: raise api_errors.InvalidBucketPathError( 'Invalid bucket path: {}'.format(full_path)) bucket_name = parsed.netloc object_name = parsed.path[1:] # Skip leading / in path. return bucket_name, object_name
def get_bucket_and_path_from(full_path): """Get the bucket and object path. Args: full_path: The full GCS path. Return: The bucket name and object path. """ if not full_path or not full_path.startswith('gs://'): raise api_errors.InvalidBucketPathError( 'Invalid bucket path: {}'.format(full_path)) bucket_name = full_path[5:].split('/')[0] bucket_prefix = 5 + len(bucket_name) + 1 object_path = full_path[bucket_prefix:] return bucket_name, object_path