Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class FirebaseStorage

  1. Object
  2. FirebaseStorage

Cloud Storage for Firebase over its REST API, in pure Java.

Paths are object names inside the bucket, avatars/42.png for example. Requests carry the ID token of the TokenProvider, so your storage security rules apply. A 401 refreshes the token once and retries.

FirebaseStorage storage = new FirebaseStorage("my-project.appspot.com");
storage.setTokenProvider(new FirebaseAuthTokenProvider());
byte[] data = "hello".getBytes();
storage.upload("notes/hello.txt", data, "text/plain").ready(new SuccessCallback<Map<String, Object>>() {
    public void onSucess(Map<String, Object> meta) {
        Log.p("Uploaded " + meta.get("name"));
    }
});

Nested types

interface FirebaseStorage.UploadProgressReceives the progress of an upload.

Fields

public static final int DEFAULT_CHUNK_SIZE = 1048576The chunk size of resumable uploads.

Constructors

public FirebaseStorage(String bucket)Creates a client for a bucket.

Methods

public void setTokenProvider(TokenProvider tokenProvider)Sets the source of ID tokens.
public void setTransport(CloudHttpTransport transport)Replaces the HTTP transport, for tests or a custom stack.
public void setHost(String host)Points the client at another host, such as the Storage emulator.
public void setChunkSize(int chunkSize)Sets the chunk size of resumable uploads.
public AsyncResource<Map<String, Object>> upload(String path, byte[] data, String contentType)Uploads an object in a single request.
public AsyncResource<Map<String, Object>> uploadResumable(String path, byte[] data, String contentType, FirebaseStorage.UploadProgress progress)Uploads an object with the resumable protocol, one chunk per request, and reports progress after each chunk.
public AsyncResource<byte[]> download(String path)Downloads the content of an object.
public AsyncResource<Boolean> delete(String path)Deletes an object.
public AsyncResource<Map<String, Object>> getMetadata(String path)Reads the metadata of an object: name, size, contentType, updated, metadata and the download tokens.
public AsyncResource<Map<String, Object>> updateMetadata(String path, Map<String, Object> fields)Changes metadata fields of an object, contentType or metadata for example.
public AsyncResource<String> getDownloadUrl(String path)Builds a long-lived download URL from the object’s download token.

Inherited methods

Field details

DEFAULT_CHUNK_SIZE

public static final int DEFAULT_CHUNK_SIZE = 1048576
The chunk size of resumable uploads. Google requires a multiple of 256 KiB.

Constructor details

FirebaseStorage

public FirebaseStorage(String bucket)
Creates a client for a bucket.

Parameters

bucket String
the bucket name, my-project.appspot.com or my-project.firebasestorage.app

Method details

setTokenProvider

public void setTokenProvider(TokenProvider tokenProvider)
Sets the source of ID tokens. Without one requests are anonymous.

Parameters

tokenProvider TokenProvider
the token provider

setTransport

public void setTransport(CloudHttpTransport transport)
Replaces the HTTP transport, for tests or a custom stack.

Parameters

transport CloudHttpTransport
the transport

setHost

public void setHost(String host)
Points the client at another host, such as the Storage emulator.

Parameters

host String
the scheme and host, http://10.0.2.2:9199 for example

setChunkSize

public void setChunkSize(int chunkSize)
Sets the chunk size of resumable uploads. It is rounded down to a multiple of 256 KiB.

Parameters

chunkSize int
the chunk size in bytes

upload

public AsyncResource<Map<String, Object>> upload(String path, byte[] data, String contentType)
Uploads an object in a single request. Use uploadResumable for large data.

Parameters

path String
the object name
data byte[]
the content
contentType String
the MIME type

Returns

the object metadata the server returns

uploadResumable

public AsyncResource<Map<String, Object>> uploadResumable(String path, byte[] data, String contentType, FirebaseStorage.UploadProgress progress)

Uploads an object with the resumable protocol, one chunk per request, and reports progress after each chunk.

A chunk that fails with a network error or a 5xx status is resumed: the client asks the server how much it has received (X-Goog-Upload-Command: query) and continues from there, up to four times per chunk with a doubling delay. An upload the server reports as cancelled fails.

Parameters

path String
the object name
data byte[]
the content
contentType String
the MIME type
progress FirebaseStorage.UploadProgress
receives progress, may be null

Returns

the object metadata the server returns after the last chunk

download

public AsyncResource<byte[]> download(String path)
Downloads the content of an object.

Parameters

path String
the object name

Returns

the bytes

delete

public AsyncResource<Boolean> delete(String path)
Deletes an object.

Parameters

path String
the object name

Returns

completes with true when the object is gone

getMetadata

public AsyncResource<Map<String, Object>> getMetadata(String path)
Reads the metadata of an object: name, size, contentType, updated, metadata and the download tokens.

Parameters

path String
the object name

Returns

the metadata

updateMetadata

public AsyncResource<Map<String, Object>> updateMetadata(String path, Map<String, Object> fields)
Changes metadata fields of an object, contentType or metadata for example.

Parameters

path String
the object name
fields Map<String, Object>
the fields to change

Returns

the updated metadata

getDownloadUrl

public AsyncResource<String> getDownloadUrl(String path)
Builds a long-lived download URL from the object’s download token. Anyone with the URL can read the object.

Parameters

path String
the object name

Returns

the URL; fails when the object has no download token