swiftly/cli/tempurl.py
def cli_tempurl(context, method, path, seconds=None, use_container=False):
    """
    Generates a TempURL and sends that to the context.io_manager's
    stdout.

    See :py:mod:`swiftly.cli.tempurl` for context usage information.

    See :py:class:`CLITempURL` for more information.

    :param context: The :py:class:`swiftly.cli.context.CLIContext` to
        use.
    :param method: The method for the TempURL (GET, PUT, etc.)
    :param path: The path the TempURL should direct to.
    :param seconds: The number of seconds the TempURL should be good
        for. Default: 3600
    :param use_container: If True, will create a container level TempURL
        useing X-Container-Meta-Temp-Url-Key instead of
        X-Account-Meta-Temp-Url-Key.
    """
    with contextlib.nested(
            context.io_manager.with_stdout(),
            context.client_manager.with_client()) as (fp, client):
        method = method.upper()
        path = path.lstrip('/')
        seconds = seconds if seconds is not None else 3600
        if '/' not in path:
            raise ReturnCode(
                'invalid tempurl path %r; should have a / within it' % path)
        if use_container:
            key_type = 'container'
            container = path.split('/', 1)[0]
            status, reason, headers, contents = \
                client.head_container(container)
        else:
            key_type = 'account'
            status, reason, headers, contents = \
                client.head_account()
        if status // 100 != 2:
            raise ReturnCode(
                'obtaining X-%s-Meta-Temp-Url-Key: %s %s' %
                (key_type.title(), status, reason))
        key = headers.get('x-%s-meta-temp-url-key' % key_type)
        if not key:
            raise ReturnCode(
                'there is no X-%s-Meta-Temp-Url-Key set for this %s' %
                (key_type.title(), key_type))
        url = client.storage_url + '/' + path
        fp.write(generate_temp_url(method, url, seconds, key))
        fp.write('\n')
        fp.flush()
def cli_tempurl(context, method, path, seconds=None, use_container=False):
    """
    Generates a TempURL and sends that to the context.io_manager's
    stdout.

    See :py:mod:`swiftly.cli.tempurl` for context usage information.

    See :py:class:`CLITempURL` for more information.

    :param context: The :py:class:`swiftly.cli.context.CLIContext` to
        use.
    :param method: The method for the TempURL (GET, PUT, etc.)
    :param path: The path the TempURL should direct to.
    :param seconds: The number of seconds the TempURL should be good
        for. Default: 3600
    :param use_container: If True, will create a container level TempURL
        useing X-Container-Meta-Temp-Url-Key instead of
        X-Account-Meta-Temp-Url-Key.
    """
    with contextlib.nested(
            context.io_manager.with_stdout(),
            context.client_manager.with_client()) as (fp, client):
        method = method.upper()
        path = path.lstrip('/')
        seconds = seconds if seconds is not None else 3600
        if '/' not in path:
            raise ReturnCode(
                'invalid tempurl path %r; should have a / within it' % path)
        if use_container:
            key_type = 'container'
            container = path.split('/', 1)[0]
            status, reason, headers, contents = \
                client.head_container(container)
        else:
            key_type = 'account'
            status, reason, headers, contents = \
                client.head_account()
        if status // 100 != 2:
            raise ReturnCode(
                'obtaining X-%s-Meta-Temp-Url-Key: %s %s' %
                (key_type.title(), status, reason))
        key = headers.get('x-%s-meta-temp-url-key' % key_type)
        if not key:
            raise ReturnCode(
                'there is no X-%s-Meta-Temp-Url-Key set for this %s' %
                (key_type.title(), key_type))
        url = client.storage_url + '/' + path
        fp.write(generate_temp_url(method, url, seconds, key))
        fp.write('\n')
        fp.flush()
Generates a TempURL and sends that to the context.io_manager's
    stdout.

    See :py:mod:`swiftly.cli.tempurl` for context usage information.

    See :py:class:`CLITempURL` for more information.

    :param context: The :py:class:`swiftly.cli.context.CLIContext` to
        use.
    :param method: The method for the TempURL (GET, PUT, etc.)
    :param path: The path the TempURL should direct to.
    :param seconds: The number of seconds the TempURL should be good
        for. Default: 3600
    :param use_container: If True, will create a container level TempURL
        useing X-Container-Meta-Temp-Url-Key instead of
        X-Account-Meta-Temp-Url-Key.