Back to Firebase Js Sdk

storage package

docs-devsite/storage.md

12.12.123.5 KB
Original Source

Project: /docs/reference/js/_project.yaml Book: /docs/reference/_book.yaml page_type: reference

{% comment %} DO NOT EDIT THIS FILE! This is generated by the JS SDK team, and any local changes will be overwritten. Changes should be made in the source code at https://github.com/firebase/firebase-js-sdk {% endcomment %}

storage package

Cloud Storage for Firebase

Functions

FunctionDescription
<b>function(app, ...)</b>
getStorage(app, bucketUrl)Gets a FirebaseStorage instance for the given Firebase app.
<b>function(storage, ...)</b>
connectStorageEmulator(storage, host, port, options)Modify this FirebaseStorage instance to communicate with the Cloud Storage emulator.
ref(storage, url)Returns a StorageReference for the given url.
<b>function(ref, ...)</b>
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-cors<!-- -->This 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 given StorageReference<!-- -->.
getMetadata(ref)A <code>Promise</code> 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: <code>listAll</code> 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.
<b>function(storageOrRef, ...)</b>
ref(storageOrRef, path)Returns a StorageReference 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 to <code>StorageError</code> objects.

Interfaces

InterfaceDescription
FirebaseStorageA Firebase Storage instance.
FullMetadataThe full set of object metadata, including read-only properties.
ListOptionsThe options <code>list()</code> 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) {:#getstorage_25f3a57}

Gets a FirebaseStorage instance for the given Firebase app.

<b>Signature:</b>

typescript
export declare function getStorage(app?: FirebaseApp, bucketUrl?: string): FirebaseStorage;

Parameters

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

<b>Returns:</b>

FirebaseStorage

A FirebaseStorage instance.

function(storage, ...)

connectStorageEmulator(storage, host, port, options) {:#connectstorageemulator_e9039de}

Modify this FirebaseStorage instance to communicate with the Cloud Storage emulator.

<b>Signature:</b>

typescript
export declare function connectStorageEmulator(storage: FirebaseStorage, host: string, port: number, options?: {
    mockUserToken?: EmulatorMockTokenOptions | string;
}): void;

Parameters

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

<b>Returns:</b>

void

ref(storage, url) {:#ref_5672fc1}

Returns a StorageReference for the given url.

<b>Signature:</b>

typescript
export declare function ref(storage: FirebaseStorage, url?: string): StorageReference;

Parameters

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

<b>Returns:</b>

StorageReference

function(ref, ...)

deleteObject(ref) {:#deleteobject_30df0b2}

Deletes the object at this location.

<b>Signature:</b>

typescript
export declare function deleteObject(ref: StorageReference): Promise<void>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference for object to delete.

<b>Returns:</b>

Promise<void>

A Promise that resolves if the deletion succeeds.

getBlob(ref, maxDownloadSizeBytes) {:#getblob_1c7a935}

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.

<b>Signature:</b>

typescript
export declare function getBlob(ref: StorageReference, maxDownloadSizeBytes?: number): Promise<Blob>;

Parameters

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

<b>Returns:</b>

Promise<Blob>

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

getBytes(ref, maxDownloadSizeBytes) {:#getbytes_1c7a935}

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

<b>Signature:</b>

typescript
export declare function getBytes(ref: StorageReference, maxDownloadSizeBytes?: number): Promise<ArrayBuffer>;

Parameters

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

<b>Returns:</b>

Promise<ArrayBuffer>

A Promise containing the object's bytes

getDownloadURL(ref) {:#getdownloadurl_30df0b2}

Returns the download URL for the given StorageReference<!-- -->.

<b>Signature:</b>

typescript
export declare function getDownloadURL(ref: StorageReference): Promise<string>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to get the download URL for.

<b>Returns:</b>

Promise<string>

A Promise that resolves with the download URL for this object.

getMetadata(ref) {:#getmetadata_30df0b2}

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

<b>Signature:</b>

typescript
export declare function getMetadata(ref: StorageReference): Promise<FullMetadata>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to get metadata from.

<b>Returns:</b>

Promise<FullMetadata<!-- -->>

getStream(ref, maxDownloadSizeBytes) {:#getstream_1c7a935}

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.

<b>Signature:</b>

typescript
export declare function getStream(ref: StorageReference, maxDownloadSizeBytes?: number): ReadableStream;

Parameters

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

<b>Returns:</b>

ReadableStream

A stream with the object's data as bytes

list(ref, options) {:#list_36af757}

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.

<b>Signature:</b>

typescript
export declare function list(ref: StorageReference, options?: ListOptions): Promise<ListResult>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to get list from.
optionsListOptionsSee ListOptions for details.

<b>Returns:</b>

Promise<ListResult<!-- -->>

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

listAll(ref) {:#listall_30df0b2}

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.

<b>Signature:</b>

typescript
export declare function listAll(ref: StorageReference): Promise<ListResult>;

Parameters

ParameterTypeDescription
refStorageReferenceStorageReference to get list from.

<b>Returns:</b>

Promise<ListResult<!-- -->>

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

updateMetadata(ref, metadata) {:#updatemetadata_a634608}

Updates the metadata for this object.

<b>Signature:</b>

typescript
export declare function updateMetadata(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.

<b>Returns:</b>

Promise<FullMetadata<!-- -->>

A Promise that resolves with the new metadata for this object.

uploadBytes(ref, data, metadata) {:#uploadbytes_02686b1}

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

<b>Signature:</b>

typescript
export declare function uploadBytes(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.

<b>Returns:</b>

Promise<UploadResult<!-- -->>

A Promise containing an UploadResult

uploadBytesResumable(ref, data, metadata) {:#uploadbytesresumable_02686b1}

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

<b>Signature:</b>

typescript
export declare function uploadBytesResumable(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.

<b>Returns:</b>

UploadTask

An UploadTask

uploadString(ref, value, format, metadata) {:#uploadstring_277829d}

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

<b>Signature:</b>

typescript
export declare function uploadString(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.

<b>Returns:</b>

Promise<UploadResult<!-- -->>

A Promise containing an UploadResult

function(storageOrRef, ...)

ref(storageOrRef, path) {:#ref_41be95d}

Returns a StorageReference for the given path in the default bucket.

<b>Signature:</b>

typescript
export declare function ref(storageOrRef: FirebaseStorage | StorageReference, path?: string): StorageReference;

Parameters

ParameterTypeDescription
storageOrRefFirebaseStorage | StorageReferenceFirebaseStorage or StorageReference<!-- -->.
pathstring

<b>Returns:</b>

StorageReference

StringFormat

An enumeration of the possible string formats for upload.

<b>Signature:</b>

typescript
StringFormat: {
    readonly RAW: "raw";
    readonly BASE64: "base64";
    readonly BASE64URL: "base64url";
    readonly DATA_URL: "data_url";
}

StringFormat

An enumeration of the possible string formats for upload.

<b>Signature:</b>

typescript
export type StringFormat = (typeof StringFormat)[keyof typeof StringFormat];

TaskEvent

An event that is triggered on a task.

<b>Signature:</b>

typescript
export type TaskEvent = 'state_changed';

TaskState

Represents the current state of a running upload.

<b>Signature:</b>

typescript
export type TaskState = 'running' | 'paused' | 'success' | 'canceled' | 'error';

StorageErrorCode

Error codes that can be attached to StorageError objects.

<b>Signature:</b>

typescript
export declare enum StorageErrorCode 

Enumeration Members

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