Class FirebaseStorage

java.lang.Object
com.codename1.cloud.firebase.FirebaseStorage

public class FirebaseStorage extends Object

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"));
    }
});
  • Field Details

    • DEFAULT_CHUNK_SIZE

      public static final int DEFAULT_CHUNK_SIZE
      The chunk size of resumable uploads. Google requires a multiple of 256 KiB.
      See Also:
  • Constructor Details

    • FirebaseStorage

      public FirebaseStorage(String bucket)

      Creates a client for a bucket.

      Parameters
      • bucket: 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: the token provider
    • setTransport

      public void setTransport(CloudHttpTransport transport)

      Replaces the HTTP transport, for tests or a custom stack.

      Parameters
      • transport: the transport
    • setHost

      public void setHost(String host)

      Points the client at another host, such as the Storage emulator.

      Parameters
      • host: 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: 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: the object name

      • data: the content

      • contentType: 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: the object name

      • data: the content

      • contentType: the MIME type

      • progress: 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: the object name
      Returns

      the bytes

    • delete

      public AsyncResource<Boolean> delete(String path)

      Deletes an object.

      Parameters
      • path: 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: 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: the object name

      • fields: 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: the object name
      Returns

      the URL; fails when the object has no download token