简体   繁体   中英

How do I get url with token of uploaded file to firebase storage?

I am trying to get the URL of a file I'm uploading to Firebase Storage. I want the URL that includes the token at the end, the one that looks like this: https://firebasestorage.googleapis.com/v0/b/myapp.appspot.com/o/folder%myfile?alt=media&token=mytoken

So far this is my code:

from firebase_admin import credentials, initialize_app

cred = credentials.Certificate("serviceAccountKey.json")
initialize_app(cred, {'storageBucket': 'myapp.appspot.com'})

bucket = storage.bucket()
path = "path/to/myfile"
blob = self.bucket.blob(path)
blob.upload_from_filename("temp.mp3")

# I only know how to get this URL but it's not the one that I want
blob.make_public()
url = blob.public_url

I also don't want the signed URL that expires.

I've seen people mention the function getDownloadURL but I don't know how I can use it with firebase-admin in Python.

I've checked https://googleapis.dev/python/storage/latest/blobs.html but all I could find about URLs was either signedURL or publicURL

Change the security rule to that specific folder

  • Make sure you upload publically visible images to that specific folder
  • It doesn't require any access token to access such images
  • Capturing media token can only be done by the Client SDK and not available in the Admin SDK for firebase-admin python
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
     // Explicitly define rules for the 'path/to/myfile/' pattern
    match /path/to/myfile/{allPaths}{
      allow  write: if request.auth != null; // Only Auth person can read
      allow  read: if request.auth == null; // Anyone can read
    }
    // This will be defined for everything else
    match /{allPaths=**} {
      allow  write: if request.auth != null; // Only auth person can write
      allow  read: if request.auth != null; // Only auth person can read
    }
  }
}

Sample python code

storageBucket='myapp.appspot.com'
bucket_path="path/to/myfile/temp.mp3"
firebase_storageURL = 'https://firebasestorage.googleapis.com/v0/b/{}/o/{}?alt=media'.format(storageBucket, bucket_path)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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