I’m writing an application using the Google Cloud Python Client Libraries. Every so often, I encounter an object that references another via a URL. For example, the Zone
object has a region
field that contains a value like "https://www.googleapis.com/compute/v1/projects/myproject/regions/someregion"
.
If I have a Zone
object and want to get the corresponding Region
object, what do I do?
I see a few options:
- Parse the URL to extract the relevant data then use the RegionClient.get method with that data.
- Directly request the given URL.
At one point, I found a post suggesting that these URLs are not “real” (Sorry, can’t find that post now). If true, option 2 is a non-starter. I haven’t actually tried that option yet because I haven’t taken the time to figure out authentication on a direct call like that.
I don’t think I am the only one to notice this issue. For example, this blog post says the following in reference to storage buckets:
Strangely – given how common it is for objects to referenced by their URI – the GCS Python client library does not provide any convenience methods to automatically extract bucket and file name information from raw URIs.
Therefore, it is up to you to implement your own method to extract bucket and file names from raw URIs if you want to load objects using the Python SDK.
Parsing the URL feels a bit hacky to me, though I do see places in the SDK where paths are parsed.
Is parsing the URL the right thing to do?
I have so far been parsing the URLs. It seems to work, but as I say, it feels hacky. So I’m just looking to verify that is the right option.
jwrw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.