Class 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 Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceReceives the progress of an upload. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe chunk size of resumable uploads. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionDeletes an object.AsyncResource<byte[]> Downloads the content of an object.getDownloadUrl(String path) Builds a long-lived download URL from the object's download token.getMetadata(String path) Reads the metadata of an object:name,size,contentType,updated,metadataand the download tokens.voidsetChunkSize(int chunkSize) Sets the chunk size of resumable uploads.voidPoints the client at another host, such as the Storage emulator.voidsetTokenProvider(TokenProvider tokenProvider) Sets the source of ID tokens.voidsetTransport(CloudHttpTransport transport) Replaces the HTTP transport, for tests or a custom stack.updateMetadata(String path, Map<String, Object> fields) Changes metadata fields of an object,contentTypeormetadatafor example.Uploads an object in a single request.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.
-
Field Details
-
DEFAULT_CHUNK_SIZE
public static final int DEFAULT_CHUNK_SIZEThe chunk size of resumable uploads. Google requires a multiple of 256 KiB.- See Also:
-
-
Constructor Details
-
FirebaseStorage
Creates a client for a bucket.
Parameters
bucket: the bucket name,my-project.appspot.comormy-project.firebasestorage.app
-
-
Method Details
-
setTokenProvider
Sets the source of ID tokens. Without one requests are anonymous.
Parameters
tokenProvider: the token provider
-
setTransport
Replaces the HTTP transport, for tests or a custom stack.
Parameters
transport: the transport
-
setHost
Points the client at another host, such as the Storage emulator.
Parameters
host: 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
chunkSize: the chunk size in bytes
-
upload
-
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
Downloads the content of an object.
Parameters
path: the object name
Returns
the bytes
-
delete
Deletes an object.
Parameters
path: the object name
Returns
completes with true when the object is gone
-
getMetadata
Reads the metadata of an object:
name,size,contentType,updated,metadataand the download tokens.Parameters
path: the object name
Returns
the metadata
-
updateMetadata
-
getDownloadUrl
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
-