0
votes

I'm using Firebase Storage to store images in FB, I'm saving download URL in Realtime DB and to retrieve some photos from the storage, I'm using this code:

   let storage = FIRStorage.storage()
    imageRefInDB.observeEventType(.Value, withBlock: { (snapshot) in
        // Get download URL from snapshot
        let downloadURL = snapshot.value as! String
        // Create a storage reference from the URL
         let storageRef = storage.referenceForURL(downloadURL)
        storageRef.dataWithMaxSize(1 * 1024 * 1024) { (data, error) -> Void in
            if (error != nil) {
                print(error.debugDescription)
            } else {
                let downloadedImage = UIImage(data: data!)
            }
        }
    })

My problem is I wanna use some of my photo that are inside another Firebase project. For example, the download URLs will be: https://firebasestorage.googleapis.com/v0/b/PROJECT1.appspot.com/o/someMedia&token

When in my current project, the bucket is different like : https://firebasestorage.googleapis.com/v0/b/PROJECT2.appspot.com/o/someMedia&Token

When I try to using the PROJECT1 files in my current PROJECT2, I'm getting the following error:

reason: 'Provided bucket: PROJECT1.appspot.com does not match bucket specified in FIRApp configuration: PROJECT2.appspot.com

Is there a way to enable downloading theses files from other projects like a setting in Firebase without retrieving them with regular URL downloads? Thanks for your help!

1

1 Answers

2
votes

I tried that once and it lead to an unhappy time. In my case I had two different buckets for my images, so I had to find an alternate solution.

If you have only a single bucket, you can try configuring that bucket in your FIRApp:

let firOptions = FIROptions(googleAppID: googleAppID, bundleID: bundleID, GCMSenderID: GCMSenderID, APIKey: nil, clientID: nil, trackingID: nil, androidClientID: nil, databaseURL: databaseURL, storageBucket: storageBucket, deepLinkURLScheme: nil)        
FIRApp.configureWithName("anotherClient", options: options)
let app = FIRApp(named: "anotherClient")

let storageRef = FIRStorage.storage(app: app!).reference()

Taken from this answer, so you may need to modify it to suit your needs

With that app, you can then use the Firebase Storage API as you're already doing to download the image data.

If you have multiple buckets (as I had), you either have to configure multiple apps or (as I did) handle downloading the image from the downloadURL yourself (without using the Firebase Storage API):

dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
    let data = NSData(contentsOfURL: downloadURL) //make sure your image in this url does exist, otherwise unwrap in a if let check
    dispatch_async(dispatch_get_main_queue(), {
        let downloadedImage = UIImage(data: data!)
    });
}