您的文件存储在Cloud Storage 存储桶中。此存储桶中的文件以层次结构呈现,就像本地硬盘上的文件系统或 Firebase 实时数据库中的数据一样。通过创建对文件的引用,您的应用程序可以访问它。这些引用随后可用于上传或下载数据、获取或更新元数据或删除文件。引用可以指向特定文件或层次结构中的更高级别节点。
如果您使用过Firebase Realtime Database ,这些路径对您来说应该非常熟悉。但是,您的文件数据存储在 Cloud Storage 中,而不是实时数据库中。
创建参考
创建引用以上传、下载或删除文件,或者获取或更新其元数据。可以将引用视为指向云中文件的指针。引用是轻量级的,因此您可以根据需要创建任意数量的引用。它们也可重复用于多种操作。
通过调用GetReferenceFromUrl()
方法并传入gs://<your-cloud-storage-bucket>
形式的 URL,从 Firebase 应用上的storage
服务创建引用。您可以在Firebase 控制台的存储部分找到此 URL。
// Get a reference to the storage service, using the default Firebase App Storage* storage = Storage::GetInstance(app); // Create a Cloud Storage reference from our storage service StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");
您可以通过对现有引用使用child
方法来创建对树中较低位置的引用,例如'images/space.jpg'
。
// Create a child reference // images_ref now points to "images" StorageReference images_ref = storage_ref.Child("images"); // Child references can also take paths delimited by '/' // space_ref now points to "images/space.jpg" // images_ref still points to "images" StorageReference space_ref = storage_ref.Child("images/space.jpg"); // This is equivalent to creating the full reference 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' 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 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" StorageReference earth_ref = space_ref.Parent().Child("earth.jpg"); // null_ref is null, since the parent of root is an invalid StorageReference StorageReference null_ref = space_ref.Root().Parent();
参考方法
您可以使用full_path
、 name
和bucket
方法检查引用以更好地理解它们指向的文件。这些方法获取文件的完整路径、名称和存储桶。
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk space_ref.full_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 Cloud Storage bucket where files are stored space_ref.bucket();
参考文献的限制
引用路径和名称可以包含任何有效的 Unicode 字符序列,但有一定的限制,包括:
- 使用 UTF-8 编码时,reference.fullPath 的总长度必须在 1 到 1024 字节之间。
- 没有回车符或换行符。
- 避免使用
#
、[
、]
、*
或?
,因为这些不能很好地与其他工具(例如Firebase 实时数据库或gsutil )一起使用。
完整示例
Storage* storage = Storage::GetInstance(app); // Points to the root reference StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-bucket-name>"); // Points to "images" StorageReference images_ref = storage_ref.Child("images"); // Points to "images/space.jpg" // Note that you can use variables to create child values std::string filename = "space.jpg"; StorageReference space_ref = images_ref.Child(filename); // File path is "images/space.jpg" std::string path = space_ref.full_path() // File name is "space.jpg" std::string name = space_ref.name() // Points to "images" StorageReference images_ref = space_ref.Parent();
下一步
接下来,让我们学习如何将文件上传到云存储。