Firebase API提供了一个方便的函数Storage::refFromUrl (source),用于将URL转换为存储引用。
从the source (location.ts)上看,它像是一个简单的正则表达式。
是否有等效的google-cloud-storage方法可以与Python API一起使用来获取存储桶和路径?
发布于 2018-08-30 18:04:18
这是一个简单的正则表达式。以下是我在几分钟内根据参考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)我已经要求将它添加到Firebase功能列表中,如果它出现了,我希望它会在firebase_admin.storage中显示出来
使用bucket和path可以直接创建存储引用。
https://stackoverflow.com/questions/52064868
复制相似问题