def put_file(
    state, hostname, file_io, remote_file,
    sudo=False, sudo_user=None, print_output=False
):
    '''
    Upload file-ios to the specified host using SFTP. Supports uploading files with sudo
    by uploading to a temporary directory then moving & chowning.
    '''

    print_prefix = '[{0}] '.format(colored(hostname, attrs=['bold']))

    if not sudo:
        _put_file(state, hostname, file_io, remote_file)

    else:
        # sudo is a little more complicated, as you can only sftp with the SSH user
        # connected,  so upload to tmp and copy/chown w/sudo

        # Get temp file location
        temp_file = state.get_temp_filename(remote_file)
        _put_file(state, hostname, file_io, temp_file)

        # Execute run_shell_command w/sudo to mv/chown it
        command = 'mv {0} {1}'.format(temp_file, remote_file)
        if sudo_user:
            command = '{0} && chown {1} {2}'.format(command, sudo_user, remote_file)

        channel, _, stderr = run_shell_command(
            state, hostname, command,
            sudo=sudo, sudo_user=sudo_user,
            print_output=print_output
        )

        if channel.exit_status > 0:
            logger.error('File error: {0}'.format('\n'.join(stderr)))
            return False

    if print_output:
        print('{0}file uploaded: {1}'.format(print_prefix, remote_file))
dataset/ETHPy150Open Fizzadar/pyinfra/pyinfra/api/ssh.py/put_file/original