message  Send Feedback

Reference

Represents a reference to a Google Cloud Storage object in React Native Firebase.

A reference can be used to upload and download storage objects, get/set storage object metadata, retrieve storage object download urls and delete storage objects.

Example 1

Get a reference to a specific storage path.

const ref = firebase.storage().ref('invertase/logo.png');

Example 2

Get a reference to a specific storage path on another bucket in the same firebase project.

const ref = firebase.storage().refFromURL('gs://other-bucket/invertase/logo.png');

Properties

Methods


Properties

bucket

bucket: string;

The name of the bucket containing this reference's object.


fullPath

fullPath: string;

The full path of this object.


name

name: string;

The short name of this object, which is the last component of the full path. For example, if fullPath is 'full/path/image.png', name is 'image.png'.


parent

parent: Reference | null;

A reference pointing to the parent location of this reference, or null if this reference is the root.


root

root: Reference;

A reference to the root of this reference's bucket.


storage

storage: Module;

The storage service associated with this reference.


Methods

child

child(
  path: string
): Reference;

Returns a reference to a relative path from this reference.

Example

const parent = firebase.storage().ref('invertase');
const ref = parent.child('logo.png');

Parameters

  • path: string
    The relative path from this reference. Leading, trailing, and consecutive slashes are removed.

delete

delete(): Promise<void>;

Deletes the object at this reference's location.

Example

const ref = firebase.storage().ref('invertase/logo.png');
await ref.delete();

getDownloadURL

getDownloadURL(): Promise<string>;

Fetches a long lived download URL for this object.

Example

const ref = firebase.storage().ref('invertase/logo.png');
const url = await ref.getDownloadURL();

getMetadata

getMetadata(): Promise<FullMetadata>;

Fetches metadata for the object at this location, if one exists.

Example

const ref = firebase.storage().ref('invertase/logo.png');
const metadata = await ref.getMetadata();
console.log('Cache control: ', metadata.cacheControl);

list

list(
  options?: ListOptions
): Promise<ListResult>;

List items (files) and prefixes (folders) under this storage reference.

List API is only available for Firebase Rules Version 2.

GCS is a key-blob store. Firebase Storage imposes the semantic of '/' delimited folder structure. Refer to GCS's List API if you want to learn more.

To adhere to Firebase Rules's Semantics, Firebase Storage does not support objects whose paths end with "/" or contain two consecutive "/"s. Firebase Storage List API will filter these unsupported objects. list() may fail if there are too many unsupported objects in the bucket.

Example

const ref = firebase.storage().ref('/');
const results = await ref.list({
maxResults: 30,
});

Parameters

  • options: (optional) ListOptions
    An optional ListOptions interface.

listAll

listAll(): Promise<ListResult>;

List all items (files) and prefixes (folders) under this storage reference.

This is a helper method for calling list() repeatedly until there are no more results. The default pagination size is 1000.

Note: The results may not be consistent if objects are changed while this operation is running.

Warning: listAll may potentially consume too many resources if there are too many results.

Example

const ref = firebase.storage().ref('/');
const results = await ref.listAll();

put

put(
  data: Blob | Uint8Array | ArrayBuffer,
  metadata?: SettableMetadata
): Task;

Puts data onto the storage bucket.

Example

const ref = firebase.storage().ref('invertase/new-logo.png');
const task = ref.put(BLOB, {
cacheControl: 'no-store', // disable caching
});

Parameters

  • data: Blob | Uint8Array | ArrayBuffer
    The data to upload to the storage bucket at the reference location.
  • metadata: (optional) SettableMetadata

putFile

putFile(
  localFilePath: string,
  metadata?: SettableMetadata
): Task;

Puts a file from local disk onto the storage bucket.

Example

const ref = firebase.storage().ref('invertase/new-logo.png');
const path = `${firebase.utils.FilePath.DOCUMENT_DIRECTORY}/new-logo.png`;
const task = ref.putFile(path, {
cacheControl: 'no-store', // disable caching
});

Parameters

  • localFilePath: string
    The local file path to upload to the bucket at the reference location.
  • metadata: (optional) SettableMetadata
    Any additional `SettableMetadata` for this task.

putString

putString(
  data: string,
  format?: 'raw' | 'base64' | 'base64url' | 'data_url',
  metadata?: SettableMetadata
): Task;

Puts a string on the storage bucket. Depending on the string type, set a storage.StringFormat type.

Example

const ref = firebase.storage().ref('invertase/new-logo.png');
const task = ref.putString('PEZvbyBCYXI+', firebase.storage.StringFormat.BASE64, {
cacheControl: 'no-store', // disable caching
});

Parameters

  • data: string
    The string data, must match the format provided.
  • format: (optional) 'raw' | 'base64' | 'base64url' | 'data_url'
    The format type of the string, e.g. a Base64 format string.
  • metadata: (optional) SettableMetadata
    Any additional `SettableMetadata` for this task.

toString

: string;

Returns a gs:// URL for this object in the form gs://///.

Example

const ref = firebase.storage().ref('invertase/logo.png');
console.log('Full path: ', ref.toString()); // gs://invertase.io/invertase/logo.png

updateMetadata

updateMetadata(
  metadata: SettableMetadata
): Promise<FullMetadata>;

Updates the metadata for this reference object on the storage bucket.

Example

const ref = firebase.storage().ref('invertase/nsfw-logo.png');
const updatedMetadata = await ref.updateMetadata({
customMetadata: {
'nsfw': 'true',
}
});

Parameters


writeToFile

writeToFile(
  localFilePath: string
): Task;

Downloads a file to the specified local file path on the device.

Example

Get a Download Storage task to download a file:

const downloadTo = `${firebase.utils.FilePath.DOCUMENT_DIRECTORY}/foobar.json`;
const task = firebase.storage().ref('/foo/bar.json').writeToFile(downloadTo);

Parameters

  • localFilePath: string

React Native Market

Looking for pre-built React Native apps to kick start your next project? Check out the React Native Market by Invertase.

Your purchases help support our open-source projects such as React Native Firebase. All items are currently 50% off to celebrate the release of React Native Firebase version 6.
shopping_cartVisit React Native Market