import requests
import logging


def upload(filename, url, token=None):
    """
    Upload a file to a URL
    """
    headers = {}
    if token:
        headers['X-Auth-Token'] = token

    try:
        with open(filename, 'rb') as file_obj:
            response = requests.put(url, data=file_obj, timeout=120, headers=headers, verify=False)
    except requests.exceptions.RequestException as err:
        logging.warning('RequestException when trying to upload file %s: %s', filename, err)
        return None
    except IOError as err:
        logging.warning('IOError when trying to upload file %s: %s', filename, err)
        return None

    if response.status_code == 200 or response.status_code == 201:
        return True
    return None