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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
dolphin
  • 199
  • 2
  • 9
  • 3
    You'll need to use `generate_signed_url`. Details can be found in the [reference doc](https://googlecloudplatform.github.io/google-cloud-python/latest/storage/blobs.html). – Jen Person Sep 21 '18 at 22:26
  • I tried to use that, but it gives me link for example: https://storage.googleapis.com/project.appspot.com/image.jpg?GoogleAccessId=erviceaccount.com&Expires=86400&Signature=Yj2WtyANJO5nYCpr5vIaVdoVv0BqLreBYN6rSaCdXeSLq1mTv6m3QQ%2Fp31By6vf9RfYv2rWlg98acPAjr5zb73RfuDvhN7PcFQtbb23rpAYJClY6YJ0GAuc9%2Fvtispl%2BBvVcv7TdQSiwZM3ZhzMag%2FzqLFN0CgaFmPqDRwnm6FVXPhO%2FmHePiSD0SOu%2FGtIK4h7TUcf9wfsw4i75dP and what I want is link like this: https://firebasestorage.googleapis.com/v0/b/project.appspot.com/o/image.jpg?alt=media&token=d5809ce7-92a0-4e25-b3cf-fe684aac20ca – dolphin Sep 22 '18 at 07:51
  • 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. – Jen Person Sep 24 '18 at 17:29
  • Yes, thanks, it does work the same. – dolphin Sep 24 '18 at 20:55

4 Answers4

9

You should use the following code:

blob.upload_from_filename(BLOB_PATH)
blob.make_public()
print(blob.public_url)
Yan
  • 347
  • 3
  • 3
  • Is there a reason why we need to specify the visibility of the blob if the storage already has rules? Thanks for that! – DsCpp Jun 02 '21 at 18:36
5

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

Joonsoo
  • 788
  • 1
  • 13
  • 15
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)
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
    }
  }
}
Dr.House
  • 784
  • 5
  • 11