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