获取已上载的blob/文件的Firebase存储令牌

2024-04-19 03:02:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python应用程序,它要求我将一些文件转储到Firebase存储桶中,更新HTML文件的img链接,以便它们指向之前上载的files+Token,然后将HTML文件本身保存到bucket中以备以后使用。我遇到的问题是我似乎无法在Python中找到获取blob/文件的唯一标记的方法。作为参考,我一直在Javascript中使用getDownloadURL来实现这一点,它为我提供了一个如下所示的URL: https://firebasestorage.googleapis.com/v0/b/[project].appspot.com/o/[bucket]%2Ffile.png?alt=media&token=[token]

当我使用google cloud python blob方法'path'时,它会给我一个不带标记的URL,我已经可以自己构建: /b/[项目]。appspot.com/o/[桶]%2文件.png在

有谁能帮我指出我可以用什么方法来检索blob/文件的令牌,这样我就可以构造完整的URL了吗?在

from firebase_admin import credentials
from firebase_admin import storage

try:
    cred = credentials.Certificate("serviceAccountKey.json")
    firebase_admin.initialize_app(cred, {
        'databaseURL': 'https://[project].firebaseio.com'
    })
except Exception as e:
    print str(e.message)

bucket = storage.bucket('[project].appspot.com')
blob1 = bucket.blob(userId + '/' + file)
with open(os.path.join(tmp_dir, file), 'rb') as my_file:
    blob1.upload_from_file(my_file)

#Now I need the token but this only prints the path I already know
print blob1.path

Tags: 文件path方法fromprojectcomtokenurl
1条回答
网友
1楼 · 发布于 2024-04-19 03:02:19
bucket = storage.bucket('[project].appspot.com')
blob1 = bucket.blob(userId + '/' + file)
with open(os.path.join(tmp_dir, file), 'rb') as my_file:
    blob1.upload_from_file(my_file)

# token here
metadata = bucket.get_blob('path_to_new_blob').metadata

相关问题 更多 >