查找已上传文件的URL,Firebase存储+Python

8
我想知道如何从我上传到Firebase存储的文件中获取URL?以下是我的代码:
    import firebase_admin
from firebase_admin import credentials, firestore, storage

cred=credentials.Certificate('/serviceAccountKey.json')
firebase_admin.initialize_app(cred, {
    'storageBucket': <my_bucket_name>
})
db = firestore.client()

bucket = storage.bucket()
blob = bucket.blob('image.jpg')
blob.upload_from_filename('/image.jpg')

#here I'd like to have url of file I uploaded
print(blob.<url>)

提前感谢您。


3
您需要使用 generate_signed_url。详细信息可以在参考文档中找到。 - Jen Person
目前还没有以您要求的特定格式获取链接的方法,但它应该能够正常工作。 - Jen Person
是的,谢谢,它确实起作用了。 - dolphin
4个回答

10
你应该使用以下代码:
blob.upload_from_filename(BLOB_PATH)
blob.make_public()
print(blob.public_url)

1
我们是否需要指定 Blob 的可见性,如果存储已经有规则的话,这样做有什么原因吗?感谢您的回答! - DsCpp

6

0

更改安全规则

  • 进入firebase存储中的Firebase安全规则
  • 更改设置,以便您将文件上传到公开可见的文件夹中
  • 路径的其余部分应具有auth限制

示例安全规则

这里除了NewFolder文件夹/路径外,所有其他文件夹/路径都将具有auth限制。

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
     // Explicitly define rules for the 'NewFolder' pattern
    match /NewFolder/{allPaths}{
      allow  write: if request.auth != null; //Only auth users can write
      allow  read: if request.auth == null; //Publically readable
    }
    // This will be defined for everything else
    match /{allPaths=**} {
      allow  write: if request.auth != null; //Only auth users can write
      allow  read: if request.auth != null; //Only auth users can read
    }
  }
}

0
image1 = face_recognition.load_image_file('99.jpg')
imageBlob_1 = bucket.blob('99.jpg')
imageBlob_1.upload_from_filename('99.jpg',content_type='image/jpeg')
URL_1 = imageBlob_1.public_url
response1 = requests.get(URL_1)

1
请更具体地说明您想要实现什么。 - user12148919

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接