简体   繁体   English

找到上传文件的url,firebase存储+python

[英]Find the url of uploaded file, firebase storage + python

I was wondering how could I get the url from the file I am uploading to the firebase storage?我想知道如何从我上传到 firebase 存储的文件中获取 url? Here is my code:这是我的代码:

    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>)

Thanks in advance.提前致谢。

You should use the following code:您应该使用以下代码:

blob.upload_from_filename(BLOB_PATH)
blob.make_public()
print(blob.public_url)

You can get the public url with blob.public_url 您可以使用blob.public_url获取公共网址

Example code: 示例代码:

bucket = storage.bucket()
blob = bucket.blob(BLOB_PATH)
blob.upload_from_filename(FILE_PATH)
print(blob.public_url)

Documentation: https://googleapis.github.io/google-cloud-python/latest/storage/blobs.html#google.cloud.storage.blob.Blob.public_url 文档: https : //googleapis.github.io/google-cloud-python/latest/storage/blobs.html#google.cloud.storage.blob.Blob.public_url

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)

Change the Security RULE更改安全规则

  • Go to Firebase Security RULES inside the firebase storage firebase firebase storage中的 Go 到Firebase Security RULES
  • Change so that you upload files to the publically visible folder更改以便您将文件上传到publically visible的文件夹
  • Rest of the path should have auth restrictions Rest的路径应该有auth限制

Sample Security Rule示例安全规则

Here all folders/ path expect NewFolder will have auth restriction.此处所有文件夹/路径都期望NewFolder将具有身份验证限制。

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
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM