'Expo file system Firebase Storage can't retrieve file (403 error)

I am trying to retrieve a file from Firebase Storage with the URL from getDownloadURL, using the Expo FileSystem. However, I keep getting a 403 response. My rules are completely public, I have even taken off requiring auth to see if that fixed anything, but it did not. I have verified that the downloadURL is valid (I have copied it into my browser, and it works).

Code

retrieveFileFromStorage = async (fileName, senderUid, downloadURL) => {
    let result = await FileSystem.downloadAsync(
        downloadURL,
        FileSystem.documentDirectory + fileName,
        { headers: { 'Content-Type': 'image/png' } }
    )

    console.log(result);
    return result.uri;
}

Response

Object {
  "headers": Object {
    "Cache-Control": "private, max-age=0",
    "Content-Length": "378",
    "Content-Type": "application/xml; charset=UTF-8",
    "Date": "Tue, 31 Mar 2020 02:12:00 GMT",
    "Expires": "Tue, 31 Mar 2020 02:12:00 GMT",
    "Server": "UploadServer",
    "alt-svc": "quic=\":443\"; ma=2592000; v=\"46,43\",h3-Q050=\":443\"; ma=2592000,h3-Q049=\":443\"; ma=2592000,h3-Q048=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,h3-T050=\":443\"; ma=2592000",
    "x-guploader-uploadid": "<removed>",
  },
  "status": 403,
  "uri": "file:///var/mobile/Containers/Data/Application/<...>/image.png",
}

Note: Since I can copy downloadURL from the function into my browser, and the image shows up just fine, does this mean the error is within retrieving the file with FileSystem?

Another note: Removing Content-Type all together causes an [Unhandled promise rejection: Error: Could not download from <URL_HERE>] error. But the URL is valid, so why can't it download from the URL?

This is occurring on iOS 13, both device and simulator.



Solution 1:[1]

The error is within the call being made to Firebase, not in the file system.

It works in your browser because your browser is including headers that are not included in the call when made from the app. For example, I encountered this when working with OpenMaps because it was expecting a User-Agent header. What exactly is missing in this case for Firebase would need further investigation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Moeller