Read, download files
general considerations
- The client SDK is subject to access-rules. Some functions allow the user, after access control, to save a bearer URL which is not subject to security rules (one-off access control).
- Download workflows are influenced by the browser restrictions.
get a HTTP URL on the client
We request a read URL. Access control is performed when requesting the URL.
The returned URL is a bearer URL, which is not subject to access-control. We consume it outside the realm of the Storage SDK, as a regular URL.
Note: the URL remains valid unless manually revoked at the file level in the Firebase Console.
getDownloadURL(fileRef).then(url => ...)
consume a cross-origin HTTP URL on the client.
The URL is cross-origin. The challenges and patterns to consume a cross-origin URL are not specific to Firebase.
The way we consume the URL determines if CORS headers are necessary.
- The browser allows cross-origin URLs in media elements' src attribute (hot linking), with no CORS headers required.
- The browser allows navigating to cross-origin URLs (basic browser behavior). For example, we navigate to an image in a new tab.
- The browser doesn't allow background fetch of cross-origin resources unless explicit CORS headers are present on the server. This applies to fetch() and functions that rely on it.
Buckets do not have permissive CORS headers by default, but we can add them on demand. CORS headers whitelist one, several or all domains. We use gsutil or gcloud to whitelist our domain (see the dedicated chapter).
download a Blob with the client SDK
A blob is an opaque object that we fetch and transform to a local URL for easier saving. When using getBlob():
- access rules are enforced
- CORS headers are required (it uses fetch() under the hood)
We create a local (same-origin) URL out of the blob, to avoid the browser restrictions against cross-origin_ URLs. It restores the ability to download content through a single click, without navigating to a different URL (see below).
getBlob(fileRef).then((blob) => {
// create a local URL and trigger download imperatively
})
add the download attribute to an anchor tag, guard against cross origin URLs
The download attribute on anchor tags (<a href="" download>) offers one-click downloads for same-origin URLs or local URLs.
For cross-origin URLs, clicking the anchor tag triggers standard browser navigation instead: the browser navigates to the resource and shows its full URL.
create a local URL out of a blob (browser specific)
This example also triggers download programmatically, and revokes the local URL for clean up. We set download to the file name.
// 3. Create a local URL out of the blob
const objectURL = URL.createObjectURL(blob)
// 4. Use the local URL to trigger the download
const link = document.createElement("a")
link.href = objectURL
link.download = img.id + ".png"
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
// 5. Clean up by revoking the local URL
URL.revokeObjectURL(objectURL)