storage package

Cloud Storage for Firebase

Functions

FunctionDescription
function(app, ...)
getStorage(app, bucketUrl)Gets aFirebaseStorage instance for the given Firebase app.
function(storage, ...)
connectStorageEmulator(storage, host, port, options)Modify thisFirebaseStorage instance to communicate with the Cloud Storage emulator.
ref(storage, url)Returns aStorageReference for the given url.
function(ref, ...)
deleteObject(ref)Deletes the object at this location.
getBlob(ref, maxDownloadSizeBytes)Downloads the data at the object's location. Returns an error if the object is not found.To use this functionality, you have to whitelist your app's origin in your Cloud Storage bucket. See also https://cloud.google.com/storage/docs/configuring-corsThis API is not available in Node.
getBytes(ref, maxDownloadSizeBytes)Downloads the data at the object's location. Returns an error if the object is not found.To use this functionality, you have to whitelist your app's origin in your Cloud Storage bucket. See also https://cloud.google.com/storage/docs/configuring-cors
getDownloadURL(ref)Returns the download URL for the givenStorageReference.
getMetadata(ref)APromise that resolves with the metadata for this object. If this object doesn't exist or metadata cannot be retrieved, the promise is rejected.
getStream(ref, maxDownloadSizeBytes)Downloads the data at the object's location. Raises an error event if the object is not found.This API is only available in Node.
list(ref, options)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.
listAll(ref)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.
updateMetadata(ref, metadata)Updates the metadata for this object.
uploadBytes(ref, data, metadata)Uploads data to this object's location. The upload is not resumable.
uploadBytesResumable(ref, data, metadata)Uploads data to this object's location. The upload can be paused and resumed, and exposes progress updates.
uploadString(ref, value, format, metadata)Uploads a string to this object's location. The upload is not resumable.
function(storageOrRef, ...)
ref(storageOrRef, path)Returns aStorageReference for the given path in the default bucket.

Classes

ClassDescription
StorageErrorAn error returned by the Firebase Storage SDK.

Enumerations

EnumerationDescription
StorageErrorCodeError codes that can be attached toStorageError objects.

Interfaces

InterfaceDescription
FirebaseStorageA Firebase Storage instance.
FullMetadataThe full set of object metadata, including read-only properties.
ListOptionsThe optionslist() accepts.
ListResultResult returned by list().
SettableMetadataObject metadata that can be set at any time.
StorageObserverA stream observer for Firebase Storage.
StorageReferenceRepresents a reference to a Google Cloud Storage object. Developers can upload, download, and delete objects, as well as get/set object metadata.
UploadMetadataObject metadata that can be set at upload.
UploadResultResult returned from a non-resumable upload.
UploadTaskRepresents the process of uploading an object. Allows you to monitor and manage the upload.
UploadTaskSnapshotHolds data about the current state of the upload task.

Variables

VariableDescription
StringFormatAn enumeration of the possible string formats for upload.

Type Aliases

Type AliasDescription
StringFormatAn enumeration of the possible string formats for upload.
TaskEventAn event that is triggered on a task.
TaskStateRepresents the current state of a running upload.

function(app, ...)

getStorage(app, bucketUrl)

Gets aFirebaseStorage instance for the given Firebase app.

Signature:

exportdeclarefunctiongetStorage(app?:FirebaseApp,bucketUrl?:string):FirebaseStorage;

Parameters

ParameterTypeDescription
appFirebaseAppFirebase app to getFirebaseStorage instance for.
bucketUrlstringThe gs:// url to your Firebase Storage Bucket. If not passed, uses the app's default Storage Bucket.

Returns:

FirebaseStorage

AFirebaseStorage instance.

function(storage, ...)

connectStorageEmulator(storage, host, port, options)

Modify thisFirebaseStorage instance to communicate with the Cloud Storage emulator.

Signature:

exportdeclarefunctionconnectStorageEmulator(storage:FirebaseStorage,host:string,port:number,options?:{mockUserToken?:EmulatorMockTokenOptions|string;}):void;

Parameters

ParameterTypeDescription
storageFirebaseStorageTheFirebaseStorage instance
hoststringThe emulator host (ex: localhost)
portnumberThe emulator port (ex: 5001)
options{ mockUserToken?:EmulatorMockTokenOptions | string; }Emulator options.options.mockUserToken is the mock auth token to use for unit testing Security Rules.

Returns:

void

ref(storage, url)

Returns aStorageReference for the given url.

Signature:

exportdeclarefunctionref(storage:FirebaseStorage,url?:string):StorageReference;

Parameters

ParameterTypeDescription
storageFirebaseStorageFirebaseStorage instance.
urlstringURL. If empty, returns root reference.

Returns:

StorageReference

function(ref, ...)

deleteObject(ref)

Deletes the object at this location.

Signature:

exportdeclarefunctiondeleteObject(ref:StorageReference):Promise<void>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference for object to delete.

Returns:

Promise<void>

APromise that resolves if the deletion succeeds.

getBlob(ref, maxDownloadSizeBytes)

Downloads the data at the object's location. Returns an error if the object is not found.

To use this functionality, you have to whitelist your app's origin in your Cloud Storage bucket. See also https://cloud.google.com/storage/docs/configuring-cors

This API is not available in Node.

Signature:

exportdeclarefunctiongetBlob(ref:StorageReference,maxDownloadSizeBytes?:number):Promise<Blob>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference where data should be downloaded.
maxDownloadSizeBytesnumberIf set, the maximum allowed size in bytes to retrieve.

Returns:

Promise<Blob>

A Promise that resolves with a Blob containing the object's bytes

getBytes(ref, maxDownloadSizeBytes)

Downloads the data at the object's location. Returns an error if the object is not found.

To use this functionality, you have to whitelist your app's origin in your Cloud Storage bucket. See also https://cloud.google.com/storage/docs/configuring-cors

Signature:

exportdeclarefunctiongetBytes(ref:StorageReference,maxDownloadSizeBytes?:number):Promise<ArrayBuffer>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference where data should be downloaded.
maxDownloadSizeBytesnumberIf set, the maximum allowed size in bytes to retrieve.

Returns:

Promise<ArrayBuffer>

A Promise containing the object's bytes

getDownloadURL(ref)

Returns the download URL for the givenStorageReference.

Signature:

exportdeclarefunctiongetDownloadURL(ref:StorageReference):Promise<string>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to get the download URL for.

Returns:

Promise<string>

APromise that resolves with the download URL for this object.

getMetadata(ref)

APromise that resolves with the metadata for this object. If this object doesn't exist or metadata cannot be retrieved, the promise is rejected.

Signature:

exportdeclarefunctiongetMetadata(ref:StorageReference):Promise<FullMetadata>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to get metadata from.

Returns:

Promise<FullMetadata>

getStream(ref, maxDownloadSizeBytes)

Downloads the data at the object's location. Raises an error event if the object is not found.

This API is only available in Node.

Signature:

exportdeclarefunctiongetStream(ref:StorageReference,maxDownloadSizeBytes?:number):ReadableStream;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference where data should be downloaded.
maxDownloadSizeBytesnumberIf set, the maximum allowed size in bytes to retrieve.

Returns:

ReadableStream

A stream with the object's data as bytes

list(ref, options)

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.

Signature:

exportdeclarefunctionlist(ref:StorageReference,options?:ListOptions):Promise<ListResult>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to get list from.
optionsListOptionsSeeListOptions for details.

Returns:

Promise<ListResult>

APromise that resolves with the items and prefixes.prefixes contains references to sub-folders anditems contains references to objects in this folder.nextPageToken can be used to get the rest of the results.

listAll(ref)

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.

Signature:

exportdeclarefunctionlistAll(ref:StorageReference):Promise<ListResult>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to get list from.

Returns:

Promise<ListResult>

APromise that resolves with all the items and prefixes under the current storage reference.prefixes contains references to sub-directories anditems contains references to objects in this folder.nextPageToken is never returned.

updateMetadata(ref, metadata)

Updates the metadata for this object.

Signature:

exportdeclarefunctionupdateMetadata(ref:StorageReference,metadata:SettableMetadata):Promise<FullMetadata>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to update metadata for.
metadataSettableMetadataThe new metadata for the object. Only values that have been explicitly set will be changed. Explicitly setting a value to null will remove the metadata.

Returns:

Promise<FullMetadata>

APromise that resolves with the new metadata for this object.

uploadBytes(ref, data, metadata)

Uploads data to this object's location. The upload is not resumable.

Signature:

exportdeclarefunctionuploadBytes(ref:StorageReference,data:Blob|Uint8Array|ArrayBuffer,metadata?:UploadMetadata):Promise<UploadResult>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference where data should be uploaded.
dataBlob | Uint8Array | ArrayBufferThe data to upload.
metadataUploadMetadataMetadata for the data to upload.

Returns:

Promise<UploadResult>

A Promise containing an UploadResult

uploadBytesResumable(ref, data, metadata)

Uploads data to this object's location. The upload can be paused and resumed, and exposes progress updates.

Signature:

exportdeclarefunctionuploadBytesResumable(ref:StorageReference,data:Blob|Uint8Array|ArrayBuffer,metadata?:UploadMetadata):UploadTask;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference where data should be uploaded.
dataBlob | Uint8Array | ArrayBufferThe data to upload.
metadataUploadMetadataMetadata for the data to upload.

Returns:

UploadTask

An UploadTask

uploadString(ref, value, format, metadata)

Uploads a string to this object's location. The upload is not resumable.

Signature:

exportdeclarefunctionuploadString(ref:StorageReference,value:string,format?:StringFormat,metadata?:UploadMetadata):Promise<UploadResult>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference where string should be uploaded.
valuestringThe string to upload.
formatStringFormatThe format of the string to upload.
metadataUploadMetadataMetadata for the string to upload.

Returns:

Promise<UploadResult>

A Promise containing an UploadResult

function(storageOrRef, ...)

ref(storageOrRef, path)

Returns aStorageReference for the given path in the default bucket.

Signature:

exportdeclarefunctionref(storageOrRef:FirebaseStorage|StorageReference,path?:string):StorageReference;

Parameters

ParameterTypeDescription
storageOrRefFirebaseStorage |StorageReferenceFirebaseStorage orStorageReference.
pathstring

Returns:

StorageReference

StringFormat

An enumeration of the possible string formats for upload.

Signature:

StringFormat:{readonlyRAW:"raw";readonlyBASE64:"base64";readonlyBASE64URL:"base64url";readonlyDATA_URL:"data_url";}

StringFormat

An enumeration of the possible string formats for upload.

Signature:

exporttypeStringFormat=(typeofStringFormat)[keyoftypeofStringFormat];

TaskEvent

An event that is triggered on a task.

Signature:

exporttypeTaskEvent='state_changed';

TaskState

Represents the current state of a running upload.

Signature:

exporttypeTaskState='running'|'paused'|'success'|'canceled'|'error';

StorageErrorCode

Error codes that can be attached toStorageError objects.

Signature:

exportdeclareenumStorageErrorCode

Enumeration Members

MemberValueDescription
APP_DELETED"app-deleted"
BUCKET_NOT_FOUND"bucket-not-found"
CANCELED"canceled"
CANNOT_SLICE_BLOB"cannot-slice-blob"
INTERNAL_ERROR"internal-error"
INVALID_ARGUMENT"invalid-argument"
INVALID_ARGUMENT_COUNT"invalid-argument-count"
INVALID_CHECKSUM"invalid-checksum"
INVALID_DEFAULT_BUCKET"invalid-default-bucket"
INVALID_EVENT_NAME"invalid-event-name"
INVALID_FORMAT"invalid-format"
INVALID_ROOT_OPERATION"invalid-root-operation"
INVALID_URL"invalid-url"
NO_DEFAULT_BUCKET"no-default-bucket"
NO_DOWNLOAD_URL"no-download-url"
OBJECT_NOT_FOUND"object-not-found"
PROJECT_NOT_FOUND"project-not-found"
QUOTA_EXCEEDED"quota-exceeded"
RETRY_LIMIT_EXCEEDED"retry-limit-exceeded"
SERVER_FILE_WRONG_SIZE"server-file-wrong-size"
UNAUTHENTICATED"unauthenticated"
UNAUTHORIZED"unauthorized"
UNAUTHORIZED_APP"unauthorized-app"
UNKNOWN"unknown"
UNSUPPORTED_ENVIRONMENT"unsupported-environment"

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-11-14 UTC.