5

I have a python application which requires me to dump a few files into a Firebase Storage bucket, update an HTML file's img links so they point to the previously uploaded files + Token, and then save off the HTML file itself into the bucket for later use. The problem I'm having is that I can't seem to find a way in Python to fetch the unique token of the blob/file. For reference, I have been accomplishing this in Javascript using getDownloadURL which provides me with a URL that looks like this: https://firebasestorage.googleapis.com/v0/b/[project].appspot.com/o/[bucket]%2Ffile.png?alt=media&token=[token]

When I use the google-cloud-python blob method 'path' it gives me a URL without the token, which I can already construct on my own: /b/[project].appspot.com/o/[bucket]%2Ffile.png

Can anybody please help point me in the direction of what method I can use to retrieve the blob/file's token so I can construct the full 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
user2108603
  • 105
  • 8

3 Answers3

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

Please read this answer. blob.reload() will reload properties from GCS. Doc

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 14 '22 at 14:42
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/32017479) – Patrick Yoder Jun 18 '22 at 17:10
0

Try changing the security rules to accept public reads

This is not recommended, But if this solves your specific use case, use this security rule.

  • Please don't use this security rules for secured stored data in the firebase
  • Don't use it for all paths
  • Specify the path so that, only that path will be exposed to the public
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
     // Explicitly define rules for the 'path_to_new_blob' pattern
    match /path_to_new_blob/{allPaths}{
      allow  write: if request.auth != null; // auth restriction for writes
      allow  read: if request.auth == null; // Public Readable
    }
    // This will be defined for everything else
    match /{allPaths=**} {
      allow  write: if request.auth != null; //auth restriction
      allow  read: if request.auth != null; //auth restriction
    }
  }
}

Dr.House
  • 784
  • 5
  • 11