Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class FirebaseStorage
- Object
- 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.UploadProgress | Receives the progress of an upload. |
Fields
public static final int DEFAULT_CHUNK_SIZE = 1048576 | The chunk size of resumable uploads. |
Constructors
public FirebaseStorage(String bucket) | Creates a client for a bucket. |
Methods
Inherited methods
Field details
DEFAULT_CHUNK_SIZE
public static final int DEFAULT_CHUNK_SIZE = 1048576The 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
bucketString- the bucket name,
my-project.appspot.comormy-project.firebasestorage.app
Method details
setTokenProvider
public void setTokenProvider(TokenProvider tokenProvider)Sets the source of ID tokens. Without one requests are anonymous.
Parameters
tokenProviderTokenProvider- the token provider
setTransport
public void setTransport(CloudHttpTransport transport)Replaces the HTTP transport, for tests or a custom stack.
Parameters
transportCloudHttpTransport- the transport
setHost
public void setHost(String host)Points the client at another host, such as the Storage emulator.
Parameters
hostString- the scheme and host,
http://10.0.2.2:9199for 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
chunkSizeint- 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
pathString- the object name
databyte[]- the content
contentTypeString- 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
pathString- the object name
databyte[]- the content
contentTypeString- the MIME type
progressFirebaseStorage.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
pathString- the object name
Returns
the bytes
delete
public AsyncResource<Boolean> delete(String path)Deletes an object.
Parameters
pathString- 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
pathString- 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
pathString- the object name
fieldsMap<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
pathString- the object name
Returns
the URL; fails when the object has no download token