Find the url of uploaded file, firebase storage + python
Asked Answered
I

4

8

I was wondering how could I get the url from the file I am uploading to the firebase storage? 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.

Incombustible answered 21/9, 2018 at 21:23 Comment(4)
You'll need to use generate_signed_url. Details can be found in the reference doc.Celestaceleste
I tried to use that, but it gives me link for example: storage.googleapis.com/project.appspot.com/… and what I want is link like this: firebasestorage.googleapis.com/v0/b/project.appspot.com/o/…Incombustible
At this time, there is no way to get the link in the particular format that you're looking for, but it should work just the same.Celestaceleste
Yes, thanks, it does work the same.Incombustible
S
11

You should use the following code:

blob.upload_from_filename(BLOB_PATH)
blob.make_public()
print(blob.public_url)
Shreve answered 7/8, 2020 at 12:4 Comment(1)
Is there a reason why we need to specify the visibility of the blob if the storage already has rules? Thanks for that!Yehudi
R
6

You can get the public url with 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

Rouault answered 18/2, 2019 at 14:50 Comment(0)
N
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)
Nisi answered 21/5, 2020 at 6:23 Comment(1)
Please be more specific and explain what you want to achieve.Enthuse
C
0

Change the Security RULE

  • Go to Firebase Security RULES inside the firebase storage
  • Change so that you upload files to the publically visible folder
  • Rest of the path should have auth restrictions

Sample Security Rule

Here all folders/ path expect NewFolder will have auth restriction.

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
    }
  }
}
Cutwork answered 18/8, 2022 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.