question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a method to convert ndb Key's urlsafe format to/from datastore.Key

See original GitHub issue

In GAE standard, ndb has the concept of the urlsafe string version of a Key. e.g. url_string = sandy_key.urlsafe() produces a result like agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM.

However, I cannot find a way to use this format with the google.cloud.datastore library for code running outside GAE standard. Please add a method to the Key class to create urlsafe strings and a method to generate Keys from urlsafe strings. Alternatively, provide some documentation on how to decode/encode them with the existing library.

NB, the console support page says “The encoding method is available in any Cloud Datastore client library.”

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:35 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
dhermescommented, Jun 8, 2017

So I did some more digging and it turns out the Reference protobuf in App Engine is proto1 (i.e. first generation of protobuf), so the modern proto2/proto3 tooling can’t convert it. I got very close to converting using standard tools, but some small differences in the protocols prevent it.

You can definitely include the ProtocolBuffer module in your application (in any location):

google-cloud-sdk/platform/google_appengine/google/net/proto/ProtocolBuffer.py

and then just define your own local Reference and Path classes from

google-cloud-sdk/platform/google_appengine/google/appengine/datastore/entity_pb.py

I.e. you can use these files outside of App Engine. I have done so in the gist I linked to and it works just fine.

2reactions
michaelenglocommented, Jun 7, 2017

Go to the source and you will find this:

def _DecodeUrlSafe(urlsafe):
  """Decode a url-safe base64-encoded string.
  This returns the decoded string.
  """
  if not isinstance(urlsafe, basestring):
    raise TypeError('urlsafe must be a string; received %r' % urlsafe)
  if isinstance(urlsafe, unicode):
    urlsafe = urlsafe.encode('utf8')
  mod = len(urlsafe) % 4
  if mod:
    urlsafe += '=' * (4 - mod)
  #This is 3-4x faster than urlsafe_b64decode()
  return base64.b64decode(urlsafe.replace('-', '+').replace('_', '/'))

I just copied and pasted this snippet in my code and it works now. Hope this helps.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NDB Key Class | App Engine standard environment for Python 2
An instance of the Key class represents an immutable Datastore key. This page has API reference documentation. For an overview, see NDB Entities...
Read more >
convert NDB urlsafe key strings to googledatastore.Key
First, let ndb deserialize the key. ndbKey = ndb.Key(urlsafe=urlsafeKey) # Rebuild the Cloud Datastore key using the path from ndb.
Read more >
Key — ndb documentation - Google Cloud
A key encapsulates the following pieces of information, which together uniquely designate a (possible) entity in Google Cloud Datastore:.
Read more >
datastore - Go Packages
WritePropertyMapDeterministic allows tests to make Serializer.PropertyMap deterministic. This should be set once at the top of your tests in an init() function.
Read more >
Software Packages in "jammy", Subsection python - Ubuntu
... geomet (0.2.1.post1-2) [universe]: convert GeoJSON to/from WKT/WKB ... [universe]: Example data for Variant Call Format (VCF) parser for Python ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found