StorageReference
public class StorageReference implements Comparable
Represents a reference to a Google Cloud Storage object. Developers can upload and download objects, get/set object metadata, and delete an object at a specified path. (see Google Cloud Storage)
Summary
Public methods |
|
|---|---|
@NonNull StorageReference |
Returns a new instance of |
int |
compareTo(@NonNull StorageReference other) |
@NonNull Task<Void> |
delete()Deletes the object at this |
boolean |
|
@NonNull List<FileDownloadTask> |
|
@NonNull List<UploadTask> |
|
@NonNull String |
Return the Google Cloud Storage bucket that holds this object. |
@NonNull Task<byte[]> |
getBytes(long maxDownloadSizeBytes)Asynchronously downloads the object from this |
@NonNull Task<Uri> |
Asynchronously retrieves a long lived download URL with a revokable token. |
@NonNull FileDownloadTask |
Asynchronously downloads the object at this |
@NonNull FileDownloadTask |
Asynchronously downloads the object at this |
@NonNull Task<StorageMetadata> |
Retrieves metadata associated with an object at this |
@NonNull String |
getName()Returns the short name of this object. |
@Nullable StorageReference |
Returns a new instance of |
@NonNull String |
getPath()Returns the full path to this object, not including the Google Cloud Storage bucket. |
@NonNull StorageReference |
getRoot()Returns a new instance of |
@NonNull FirebaseStorage |
Returns the |
@NonNull StreamDownloadTask |
Asynchronously downloads the object at this |
@NonNull StreamDownloadTask |
getStream(@NonNull StreamDownloadTask.StreamProcessor processor)Asynchronously downloads the object at this |
int |
hashCode() |
@NonNull Task<ListResult> |
list(int maxResults)List up to |
@NonNull Task<ListResult> |
Resumes a previous call to |
@NonNull Task<ListResult> |
listAll()List all items (files) and prefixes (folders) under this StorageReference. |
@NonNull UploadTask |
Asynchronously uploads byte data to this |
@NonNull UploadTask |
putBytes(@NonNull byte[] bytes, @NonNull StorageMetadata metadata)Asynchronously uploads byte data to this |
@NonNull UploadTask |
Asynchronously uploads from a content URI to this |
@NonNull UploadTask |
putFile(@NonNull Uri uri, @NonNull StorageMetadata metadata)Asynchronously uploads from a content URI to this |
@NonNull UploadTask |
Asynchronously uploads from a content URI to this |
@NonNull UploadTask |
putStream(@NonNull InputStream stream)Asynchronously uploads a stream of data to this |
@NonNull UploadTask |
putStream(@NonNull InputStream stream, @NonNull StorageMetadata metadata)Asynchronously uploads a stream of data to this |
String |
toString() |
@NonNull Task<StorageMetadata> |
updateMetadata(@NonNull StorageMetadata metadata)Updates the metadata associated with this |
Public methods
child
public @NonNull StorageReference child(@NonNull String pathString)
Returns a new instance of StorageReference pointing to a child location of the current reference. All leading and trailing slashes will be removed, and consecutive slashes will be compressed to single slashes. For example:
child = /foo/bar path = foo/bar child = foo/bar/ path = foo/bar child = foo///bar path = foo/bar
| Returns | |
|---|---|
@NonNull StorageReference |
the child |
getActiveDownloadTasks
public @NonNull List<FileDownloadTask> getActiveDownloadTasks()
| Returns | |
|---|---|
@NonNull List<FileDownloadTask> |
the set of active download tasks currently in progress or recently completed. |
getActiveUploadTasks
public @NonNull List<UploadTask> getActiveUploadTasks()
| Returns | |
|---|---|
@NonNull List<UploadTask> |
the set of active upload tasks currently in progress or recently completed. |
getBucket
public @NonNull String getBucket()
Return the Google Cloud Storage bucket that holds this object.
getBytes
public @NonNull Task<byte[]> getBytes(long maxDownloadSizeBytes)
Asynchronously downloads the object from this StorageReference A byte array will be allocated large enough to hold the entire file in memory. Therefore, using this method will impact memory usage of your process. If you are downloading many large files, getStream may be a better option.
| Parameters | |
|---|---|
long maxDownloadSizeBytes |
The maximum allowed size in bytes that will be allocated. Set this parameter to prevent out of memory conditions from occurring. If the download exceeds this limit, the task will fail and an |
getDownloadUrl
public @NonNull Task<Uri> getDownloadUrl()
Asynchronously retrieves a long lived download URL with a revokable token. This can be used to share the file with others, but can be revoked by a developer in the Firebase Console if desired.
getFile
public @NonNull FileDownloadTask getFile(@NonNull File destinationFile)
Asynchronously downloads the object at this StorageReference to a specified system filepath.
| Parameters | |
|---|---|
@NonNull File destinationFile |
A |
| Returns | |
|---|---|
@NonNull FileDownloadTask |
A |
getFile
public @NonNull FileDownloadTask getFile(@NonNull Uri destinationUri)
Asynchronously downloads the object at this StorageReference to a specified system filepath.
| Parameters | |
|---|---|
@NonNull Uri destinationUri |
A file system URI representing the path the object should be downloaded to. |
| Returns | |
|---|---|
@NonNull FileDownloadTask |
A |
getMetadata
public @NonNull Task<StorageMetadata> getMetadata()
Retrieves metadata associated with an object at this StorageReference.
| Returns | |
|---|---|
@NonNull Task<StorageMetadata> |
the metadata. |
getParent
public @Nullable StorageReference getParent()
Returns a new instance of StorageReference pointing to the parent location or null if this instance references the root location. For example:
path = foo/bar/baz parent = foo/bar path = foo parent = (root) path = (root) parent = (null)
| Returns | |
|---|---|
@Nullable StorageReference |
the parent |
getPath
public @NonNull String getPath()
Returns the full path to this object, not including the Google Cloud Storage bucket.
getRoot
public @NonNull StorageReference getRoot()
Returns a new instance of StorageReference pointing to the root location.
| Returns | |
|---|---|
@NonNull StorageReference |
the root |
getStorage
public @NonNull FirebaseStorage getStorage()
Returns the FirebaseStorage service which created this reference.
| Returns | |
|---|---|
@NonNull FirebaseStorage |
The |
getStream
public @NonNull StreamDownloadTask getStream()
Asynchronously downloads the object at this StorageReference via a InputStream. The InputStream should be read on an OnSuccessListener registered to run on a background thread via addOnSuccessListener
| Returns | |
|---|---|
@NonNull StreamDownloadTask |
A |
getStream
public @NonNull StreamDownloadTask getStream(@NonNull StreamDownloadTask.StreamProcessor processor)
Asynchronously downloads the object at this StorageReference via a InputStream.
| Parameters | |
|---|---|
@NonNull StreamDownloadTask.StreamProcessor processor |
A |
| Returns | |
|---|---|
@NonNull StreamDownloadTask |
A |
list
public @NonNull Task<ListResult> list(int maxResults)
List up to maxResults items (files) and prefixes (folders) under this StorageReference.
"/" is treated as a path delimiter. Cloud Storage for Firebase does not support object paths that end with "/" or contain two consecutive "/"s. All invalid objects in Google Cloud Storage will be filtered.
list() is only available for projects using Firebase Rules Version 2.
| Parameters | |
|---|---|
int maxResults |
The maximum number of results to return in a single page. Must be greater than 0 and at most 1000. |
| Returns | |
|---|---|
@NonNull Task<ListResult> |
A a |
list
public @NonNull Task<ListResult> list(int maxResults, @NonNull String pageToken)
Resumes a previous call to list, starting after a pagination token. Returns the next set of items (files) and prefixes (folders) under this StorageReference.
"/" is treated as a path delimiter. Cloud Storage for Firebase does not support object paths that end with "/" or contain two consecutive "/"s. All invalid objects in Google Cloud Storage will be filtered.
list() is only available for projects using Firebase Rules Version 2.
| Parameters | |
|---|---|
int maxResults |
The maximum number of results to return in a single page. Must be greater than 0 and at most 1000. |
@NonNull String pageToken |
A page token from a previous call to list. |
| Returns | |
|---|---|
@NonNull Task<ListResult> |
A a |
listAll
public @NonNull Task<ListResult> listAll()
List all items (files) and prefixes (folders) under this StorageReference.
This is a helper method for calling list() repeatedly until there are no more results. Consistency of the result is not guaranteed if objects are inserted or removed while this operation is executing.
listAll() is only available for projects using Firebase Rules Version 2.
| Returns | |
|---|---|
@NonNull Task<ListResult> |
A |
| Throws | |
|---|---|
java.lang.OutOfMemoryError java.lang.OutOfMemoryError |
If there are too many items at this location. |
putBytes
public @NonNull UploadTask putBytes(@NonNull byte[] bytes)
Asynchronously uploads byte data to this StorageReference. This is not recommended for large files. Instead upload a file via putFile or an InputStream via putStream.
| Parameters | |
|---|---|
@NonNull byte[] bytes |
The byte array to upload. |
| Returns | |
|---|---|
@NonNull UploadTask |
An instance of |
putBytes
public @NonNull UploadTask putBytes(@NonNull byte[] bytes, @NonNull StorageMetadata metadata)
Asynchronously uploads byte data to this StorageReference. This is not recommended for large files. Instead upload a file via putFile or a Stream via putStream.
| Parameters | |
|---|---|
@NonNull byte[] bytes |
The byte[] to upload. |
@NonNull StorageMetadata metadata |
|
| Returns | |
|---|---|
@NonNull UploadTask |
An instance of |
putFile
public @NonNull UploadTask putFile(@NonNull Uri uri)
Asynchronously uploads from a content URI to this StorageReference.
| Parameters | |
|---|---|
@NonNull Uri uri |
The source of the upload. This can be a file:// scheme or any content URI. A content resolver will be used to load the data. |
| Returns | |
|---|---|
@NonNull UploadTask |
An instance of |
putFile
public @NonNull UploadTask putFile(@NonNull Uri uri, @NonNull StorageMetadata metadata)
Asynchronously uploads from a content URI to this StorageReference.
| Parameters | |
|---|---|
@NonNull Uri uri |
The source of the upload. This can be a file:// scheme or any content URI. A content resolver will be used to load the data. |
@NonNull StorageMetadata metadata |
|
| Returns | |
|---|---|
@NonNull UploadTask |
An instance of |
putFile
public @NonNull UploadTask putFile(
@NonNull Uri uri,
@Nullable StorageMetadata metadata,
@Nullable Uri existingUploadUri
)
Asynchronously uploads from a content URI to this StorageReference.
| Parameters | |
|---|---|
@NonNull Uri uri |
The source of the upload. This can be a file:// scheme or any content URI. A content resolver will be used to load the data. |
@Nullable StorageMetadata metadata |
|
@Nullable Uri existingUploadUri |
If set, an attempt is made to resume an existing upload session as defined by |
| Returns | |
|---|---|
@NonNull UploadTask |
An instance of |
putStream
public @NonNull UploadTask putStream(@NonNull InputStream stream)
Asynchronously uploads a stream of data to this StorageReference. The stream will remain open at the end of the upload.
| Parameters | |
|---|---|
@NonNull InputStream stream |
The |
| Returns | |
|---|---|
@NonNull UploadTask |
An instance of |
putStream
public @NonNull UploadTask putStream(@NonNull InputStream stream, @NonNull StorageMetadata metadata)
Asynchronously uploads a stream of data to this StorageReference. The stream will remain open at the end of the upload.
| Parameters | |
|---|---|
@NonNull InputStream stream |
The |
@NonNull StorageMetadata metadata |
|
| Returns | |
|---|---|
@NonNull UploadTask |
An instance of |
toString
public String toString()
| Returns | |
|---|---|
String |
This object in URI form, which can then be shared and passed into |
updateMetadata
public @NonNull Task<StorageMetadata> updateMetadata(@NonNull StorageMetadata metadata)
Updates the metadata associated with this StorageReference.
| Parameters | |
|---|---|
@NonNull StorageMetadata metadata |
A |
| Returns | |
|---|---|
@NonNull Task<StorageMetadata> |
a |