Convert to a base64 encode urlsafe string for App Engine.

        This is intended to work with the "legacy" representation of a
        datastore "Key" used within Google App Engine (a so-called
        "Reference"). The returned string can be used as the ``urlsafe``
        argument to ``ndb.Key(urlsafe=...)``. The base64 encoded values
        will have padding removed.

        .. note::

            The string returned by ``to_legacy_urlsafe`` is equivalent, but
            not identical, to the string returned by ``ndb``. The location
            prefix may need to be specified to obtain identical urlsafe
            keys.

        :type location_prefix: str
        :param location_prefix: The location prefix of an App Engine project
                                ID. Often this value is 's~', but may also be
                                'e~', or other location prefixes currently
                                unknown.

        :rtype: bytes
        :returns: A bytestring containing the key encoded as URL-safe base64.

def to_legacy_urlsafe(self, location_prefix=None):
        """Convert to a base64 encode urlsafe string for App Engine.

        This is intended to work with the "legacy" representation of a
        datastore "Key" used within Google App Engine (a so-called
        "Reference"). The returned string can be used as the ``urlsafe``
        argument to ``ndb.Key(urlsafe=...)``. The base64 encoded values
        will have padding removed.

        .. note::

            The string returned by ``to_legacy_urlsafe`` is equivalent, but
            not identical, to the string returned by ``ndb``. The location
            prefix may need to be specified to obtain identical urlsafe
            keys.

        :type location_prefix: str
        :param location_prefix: The location prefix of an App Engine project
                                ID. Often this value is 's~', but may also be
                                'e~', or other location prefixes currently
                                unknown.

        :rtype: bytes
        :returns: A bytestring containing the key encoded as URL-safe base64.
        """
        if location_prefix is None:
            project_id = self.project
        else:
            project_id = location_prefix + self.project

        reference = _app_engine_key_pb2.Reference(
            app=project_id,
            path=_to_legacy_path(self._path),  # Avoid the copy.
            name_space=self.namespace,
        )
        raw_bytes = reference.SerializeToString()
        return base64.urlsafe_b64encode(raw_bytes).strip(b"=")