您的文件存储在 Google Cloud Storage 存储分区中。此存储分区中的文件以分层结构存储,就像本地硬盘中的文件系统或 Firebase 实时数据库中的数据一样。通过创建对文件的引用,您的应用便可以访问相应文件。然后,借助所创建的这些引用,您可以上传或下载数据、获取或更新元数据,也可以删除文件。引用可以指向特定的文件,也可以指向层次结构中更高层级的节点。
如果您使用过 Firebase 实时数据库,应当很熟悉这些路径。不同的是,您的文件数据是存储在 Google Cloud Storage 中,而不是存储在实时数据库中。
创建引用
如需上传、下载或删除文件,或者需要获取或更新文件的元数据,请创建引用。引用可以看作是指向云端文件的指针。由于引用属于轻型对象,因此您可以根据需要创建任意多个引用。 这些引用可以重复用于多个操作。
引用是通过您的 Firebase 应用上的 Firebase.Storage.FirebaseStorage
服务创建的,具体方法是调用 GetReferenceFromUrl()
方法,并传递 gs://<your-cloud-storage-bucket>
格式的网址。您可以在 Firebase 控制台的“存储”部分找到此网址。
// Get a reference to the storage service, using the default Firebase App Firebase.Storage.FirebaseStorage storage = Firebase.Storage.FirebaseStorage.DefaultInstance; // Create a storage reference from our storage service Firebase.Storage.StorageReference storage_ref = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");
您可以对现有引用使用 child
方法,创建对树中较低层位置(如 'images/space.jpg'
)的引用。
// Create a child reference // images_ref now points to "images" Firebase.Storage.StorageReference images_ref = storage_ref.Child("images"); // Child references can also take paths delimited by '/' such as: // "images/space.jpg". Firebase.Storage.StorageReference space_ref = images_ref.Child("space.jpg"); // space_ref now points to "images/space.jpg" // images_ref still points to "images" // This is equivalent to creating the full reference Firebase.Storage.StorageReference space_ref = storage.GetReferenceFromUrl( "gs://<your-cloud-storage-bucket>/images/space.jpg");
通过引用进行导航
您还可以使用 Parent
和 Root
方法,在我们的文件层次结构中向上导航。Parent
可以向上一层,而 Root
可以一直向上到最顶层。
// Parent allows us to move to the parent of a reference // images_ref now points to 'images' Firebase.Storage.StorageReference images_ref = space_ref.Parent; // Root allows us to move all the way back to the top of our bucket // root_ref now points to the root Firebase.Storage.StorageReference root_ref = space_ref.Root;
Child
、Parent
和 Root
可以多次链接在一起,每次都会返回一个引用。Root
的 Parent
例外,它是无效的 StorageReference。
// References can be chained together multiple times // earth_ref points to "images/earth.jpg" Firebase.Storage.StorageReference earth_ref = space_ref.Parent.Child("earth.jpg"); // null_ref is null since the parent of root is an invalid StorageReference Firebase.Storage.StorageReference null_ref = space_ref.Root.Parent;
引用方法
您可以检查引用的 Path
、Name
和 Bucket
属性,以便更好地了解它们指向的文件。这些属性会获取文件的完整路径、名称和存储分区。
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk space_ref.Path; // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name space_ref.Name; // Reference's bucket is the name of the storage bucket where files are stored space_ref.Bucket;
引用限制
引用路径和名称可以包含有效 Unicode 字符的任意序列,但也必须遵循一些限制,具体包括:
- 使用 UTF-8 编码时,
reference.Path
的总长度必须介于 1 到 1024 个字节之间。 - 不得包含回车符或换行符。
- 避免使用
#
、[
、]
、*
或?
,因为这些字符在 Firebase Realtime Database 或 gsutil 等其他工具中无法正常显示。
完整示例
Firebase.Storage.FirebaseStorage storage = Firebase.Storage.FirebaseStorage.DefaultInstance; // Points to the root reference Firebase.Storage.StorageReference storage_ref = storage.GetReferenceFromUrl("gs://<your-bucket-name>"); // Points to "images" Firebase.Storage.StorageReference images_ref = storage_ref.Child("images"); // Points to "images/space.jpg" // Note that you can use variables to create child values string filename = "space.jpg"; Firebase.Storage.StorageReference space_ref = images_ref.Child(filename); // File path is "images/space.jpg" string path = space_ref.Path; // File name is "space.jpg" string name = space_ref.Name; // Points to "images" Firebase.Storage.StorageReference images_ref = space_ref.Parent;
后续步骤
接下来,我们将了解如何上传文件到 Cloud Storage。