[英]Is there an equivalent to `refFromUrl` in google-cloud-storage for Python?
The Firebase API provides a convenience function Storage::refFromUrl ( source ) that converts a URL
into a storage reference. Firebase API提供了便捷功能Storage :: refFromUrl ( source ),可将
URL
转换为存储引用。
From the source (location.ts) It looks like it's a straightforward Regular expression. 从源(location.ts)看来,这是一个简单的正则表达式。
Is there an equivalent Python method that works with the google-cloud-storage
API to get the bucket and path? 是否存在与
google-cloud-storage
API一起使用的等效Python方法来获取存储区和路径?
It's a straightforward regex. 这是一个简单的正则表达式。 Here's what I put together in a few minutes, based on the reference Javascript implementation:
这是我在几分钟后根据参考Javascript实现汇总的内容:
def _urlToBucketPath (url):
"""Convert a Firebase HTTP URL to a (bucket, path) tuple,
Firebase's `refFromURL`.
"""
bucket_domain = '([A-Za-z0-9.\\-]+)'
is_http = not url.startswith('gs://')
if is_http:
path = '(/([^?#]*).*)?$'
version = 'v[A-Za-z0-9_]+'
rex = (
'^https?://firebasestorage\\.googleapis\\.com/' +
version + '/b/' + bucket_domain + '/o' + path)
else:
gs_path = '(/(.*))?$'
rex = '^gs://' + bucket_domain + gs_path
matches = re.match(rex, url, re.I)
if not matches:
raise Exception('URL does not match a bucket: %s' % url)
bucket, _, path = matches.groups()
if is_http:
path = urllib.parse.unquote(path)
return (bucket, path)
I've asked that it be added to the Firebase features list, and if it shows up I expect it'd be exposed in firebase_admin.storage 我已要求将其添加到Firebase功能列表中,如果它显示出来,我希望它会在firebase_admin.storage中公开
With the bucket
and path
it's straightforward to create a storage reference. 使用
bucket
和path
可以轻松创建存储引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.