ファイルはCloud Storageバケットに保存されます。このバケット内のファイルは、ローカル ハードディスクのファイル システムや Firebase Realtime Database のデータと同様に、階層構造で表示されます。ファイルへの参照を作成することで、アプリはそのファイルにアクセスできるようになります。これらの参照は、データのアップロードまたはダウンロード、メタデータの取得または更新、またはファイルの削除に使用できます。参照は、特定のファイルまたは階層内の上位レベルのノードのいずれかを指すことができます。
Firebase Realtime Databaseを使用したことがある場合、これらのパスは非常によく知られているはずです。ただし、ファイル データは Realtime Database ではなくCloud Storage に保存されます。
参照を作成する
ファイルをアップロード、ダウンロード、または削除するため、またはそのメタデータを取得または更新するための参照を作成します。参照は、クラウド内のファイルへのポインターと考えることができます。参照は軽量なので、必要な数だけ作成できます。また、複数の操作で再利用できます。
FirebaseStorage
シングルトン インスタンスを使用して参照を作成し、そのgetReference()
メソッドを呼び出します。
Kotlin+KTX
// Create a storage reference from our app var storageRef = storage.reference
Java
// Create a storage reference from our app StorageReference storageRef = storage.getReference();
次に、既存の参照に対してchild()
メソッドを使用して、ツリーの下位の場所への参照 ( "images/space.jpg"
) を作成できます。
Kotlin+KTX
// Create a child reference // imagesRef now points to "images" var imagesRef: StorageReference? = storageRef.child("images") // Child references can also take paths // spaceRef now points to "images/space.jpg // imagesRef still points to "images" var spaceRef = storageRef.child("images/space.jpg")
Java
// Create a child reference // imagesRef now points to "images" StorageReference imagesRef = storageRef.child("images"); // Child references can also take paths // spaceRef now points to "images/space.jpg // imagesRef still points to "images" StorageReference spaceRef = storageRef.child("images/space.jpg");
参照を使用して移動する
getParent()
およびgetRoot()
メソッドを使用して、ファイル階層を上に移動することもできます。 getParent()
は 1 レベル上に移動しますが、 getRoot()
は一番上まで移動します。
Kotlin+KTX
// parent allows us to move our reference to a parent node // imagesRef now points to 'images' imagesRef = spaceRef.parent // root allows us to move all the way back to the top of our bucket // rootRef now points to the root val rootRef = spaceRef.root
Java
// getParent allows us to move our reference to a parent node // imagesRef now points to 'images' imagesRef = spaceRef.getParent(); // getRoot allows us to move all the way back to the top of our bucket // rootRef now points to the root StorageReference rootRef = spaceRef.getRoot();
child()
、 getParent()
、およびgetRoot()
は、それぞれが参照を返すため、複数回連結できます。ただし、 getRoot().getParent()
を呼び出すとnull
が返されます。
Kotlin+KTX
// References can be chained together multiple times // earthRef points to 'images/earth.jpg' val earthRef = spaceRef.parent?.child("earth.jpg") // nullRef is null, since the parent of root is null val nullRef = spaceRef.root.parent
Java
// References can be chained together multiple times // earthRef points to 'images/earth.jpg' StorageReference earthRef = spaceRef.getParent().child("earth.jpg"); // nullRef is null, since the parent of root is null StorageReference nullRef = spaceRef.getRoot().getParent();
参照プロパティ
getPath()
、 getName()
、およびgetBucket()
メソッドを使用して参照を検査し、それらが指すファイルをよりよく理解することができます。これらのメソッドは、ファイルのフル パス、名前、およびバケットを取得します。
Kotlin+KTX
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk spaceRef.path // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name spaceRef.name // Reference's bucket is the name of the storage bucket that the files are stored in spaceRef.bucket
Java
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk spaceRef.getPath(); // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name spaceRef.getName(); // Reference's bucket is the name of the storage bucket that the files are stored in spaceRef.getBucket();
参照に関する制限
参照パスと名前には、有効な Unicode 文字の任意のシーケンスを含めることができますが、次のような特定の制限が課されます。
- UTF-8 エンコードの場合、reference.fullPath の合計の長さは 1 ~ 1024 バイトである必要があります。
- キャリッジ リターンまたはライン フィード文字はありません。
-
#
、[
、]
、*
、または?
の使用は避けてください。これらは、 Firebase Realtime Databaseやgsutilなどの他のツールではうまく機能しないためです。
完全な例
Kotlin+KTX
// Points to the root reference storageRef = storage.reference // Points to "images" imagesRef = storageRef.child("images") // Points to "images/space.jpg" // Note that you can use variables to create child values val fileName = "space.jpg" spaceRef = imagesRef.child(fileName) // File path is "images/space.jpg" val path = spaceRef.path // File name is "space.jpg" val name = spaceRef.name // Points to "images" imagesRef = spaceRef.parent
Java
// Points to the root reference storageRef = storage.getReference(); // Points to "images" imagesRef = storageRef.child("images"); // Points to "images/space.jpg" // Note that you can use variables to create child values String fileName = "space.jpg"; spaceRef = imagesRef.child(fileName); // File path is "images/space.jpg" String path = spaceRef.getPath(); // File name is "space.jpg" String name = spaceRef.getName(); // Points to "images" imagesRef = spaceRef.getParent();
次に、Cloud Storage にファイルをアップロードする方法を学びましょう。