python - Python 的 google-cloud-storage 中是否有与 `refFromUrl` 等价的东西?
问题描述
Firebase API 提供了一个方便的函数Storage::refFromUrl ( source ),它将 aURL
转换为存储引用。
从源码(location.ts)看,它看起来是一个简单的正则表达式。
是否有等效的 Python 方法可与google-cloud-storage
API 一起使用以获取存储桶和路径?
解决方案
这是一个简单的正则表达式。以下是我根据参考 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
可以直接创建存储引用。
推荐阅读
- reactjs - ReactJs 中的代码可重用性(继承)
- python - 为什么'user_input'总是一个字符串?
- firebase - Firebase displayName 仅在重新登录后显示,而不是在第一次登录时显示
- powershell - Powershell 变量丢失
- html - 这个css结构叫什么?
- c# - 从没有正则表达式的字符串中获取一组数字
- session - 想要将会话存储变量传递给 scss 文件
- adobe-illustrator - 从 illustrator 上的彩色矢量图像中去除白色
- reactjs - api不使用调度调用
- python - Kivy 打印变化的变量