import uuid


def export_table(bigquery, cloud_storage_path,
                 project_id, dataset_id, table_id,
                 export_format="CSV",
                 num_retries=5,
                 compression="NONE"):
    """
    Starts an export job

    Args:
        bigquery: initialized and authorized bigquery
            google-api-client object.
        cloud_storage_path: fully qualified
            path to a Google Cloud Storage location.
            e.g. gs://mybucket/myfolder/
        export_format: format to export in;
            "CSV", "NEWLINE_DELIMITED_JSON", or "AVRO".
        compression: format to compress results with,
            "NONE" (default) or "GZIP".

    Returns: an extract job resource representing the
        job, see https://cloud.google.com/bigquery/docs/reference/v2/jobs
    """
    # Generate a unique job ID so retries
    # don't accidentally duplicate export
    job_data = {
        'jobReference': {
            'projectId': project_id,
            'jobId': str(uuid.uuid4())
        },
        'configuration': {
            'extract': {
                'sourceTable': {
                    'projectId': project_id,
                    'datasetId': dataset_id,
                    'tableId': table_id,
                },
                'destinationUris': [cloud_storage_path],
                'destinationFormat': export_format,
                'compression': compression
            }
        }
    }
    return bigquery.jobs().insert(
        projectId=project_id,
        body=job_data).execute(num_retries=num_retries)