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