Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove query string from Firebase Storage download URL

Problem:

I need to be able to remove all link decoration from the download URL that is generated for images in Firebase Storage. However, when all link decoration is stripped away, the resulting link currently would return a JSON document of the image's metadata.

Context:

The flow goes as follows:

An image is uploaded to Firebase from an iOS app. Once that is done the download URL is then sent in a POST request to an external server.

The server that the URL is being sent to doesn't accept link decoration when submitting image URLs.

Goal:

Alter the Firebase Storage download URL such as it is stripped of all link decoration like so:

  • https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg

Notes:

The problem is twofold really, first the link needs to be manipulated to remove all the link decoration. Then the behavior of the link needs to changed, since in order to return an image, you need ?alt=media following the file extension, in this case .jpg. Currently, without link decoration, using the link with my desired structure would return a JSON document of the metadata.

The current link structure is as follows:

  • https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg?alt=media&token=[TOKEN]

Desired link structure:

  • https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg

The token is necessary for accessing the image depending security rules in place, but can be ignored with the proper read permissions. I can adjust the rules as needed, but I still need to be able to remove the ?alt=media and still return an image.

like image 648
A Israfil Avatar asked Oct 16 '22 10:10

A Israfil


2 Answers

Building up on Frank's answer, if you access to your associated Google Cloud Platform project, find the bucket in the Storage tab and make this bucket public, you will be able to get the image from here with the format you wish. That is, you will not be accessing through Firebase

https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg

but through Google Cloud Storage, with a link like

https://storage.googleapis.com/[bucket_name]/[path_to_image]

Once in your GCP project Console, access the Storage bucket with the same name as the one you have in your Firebase project. They are the same bucket. Then make the bucket public by following these steps. After that, you will be able to construct your links as mentioned above and they will be accessible with no token and no alt=media param. If you do not want to make the public to everyone, you will be able to play around with the permissions there as you wish.

like image 157
Pablo Almécija Rodríguez Avatar answered Oct 19 '22 01:10

Pablo Almécija Rodríguez


You could split the url string into two halves by using String.componentsSeparatedByString(_ separator:)

Storage.storage().reference().child(filePath).downloadURL(completion: { (url, error) in
    let urlString = url.absoluteString
    let urlStringWithoutQueryString = urlString.componentsSeparatedByString("?").first!
})

Calling .downloadURL on a StorageReference will return you that URL, but this method can be used to remove the query string from any URL. String.componentsSeparatedByString(_ separator:) breaks a String into an array of Strings, splitting the string by any occurrence of a given separator, in this case ?.

NOTE this method assumes that ? occurs only once within the url string, which I believe is the case for all Firebase Storage urls.

like image 42
David Chopin Avatar answered Oct 19 '22 00:10

David Chopin