File Anda disimpan di keranjang Cloud Storage . File dalam keranjang ini ditampilkan dalam struktur hierarki, seperti sistem file di hard disk lokal Anda, atau data di Firebase Realtime Database. Dengan membuat referensi ke file, aplikasi Anda mendapatkan akses ke sana. Referensi ini kemudian dapat digunakan untuk mengunggah atau mengunduh data, mendapatkan atau memperbarui metadata, atau menghapus file. Referensi dapat menunjuk ke file tertentu atau ke node tingkat yang lebih tinggi dalam hierarki.
Jika Anda telah menggunakan Firebase Realtime Database , jalur ini akan sangat Anda kenal. Namun, data file Anda disimpan di Cloud Storage, bukan di Realtime Database.
Buat Referensi
Buat referensi untuk mengunggah, mengunduh, atau menghapus file, atau untuk mendapatkan atau memperbarui metadatanya. Referensi dapat dianggap sebagai penunjuk ke file di cloud. Referensi ringan, sehingga Anda dapat membuat sebanyak yang Anda butuhkan. Mereka juga dapat digunakan kembali untuk beberapa operasi.
Referensi dibuat dari layanan storage
di aplikasi Firebase Anda dengan memanggil metode GetReferenceFromUrl()
dan meneruskan URL dengan format gs://<your-cloud-storage-bucket>
. Anda dapat menemukan URL ini di bagian Storage di Firebase console .
// 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>");
Anda dapat membuat referensi ke lokasi yang lebih rendah di pohon, misalnya 'images/space.jpg'
, dengan menggunakan metode child
pada referensi yang ada.
// 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");
Menavigasi dengan Referensi
Anda juga dapat menggunakan metode Parent
dan Root
untuk menavigasi ke atas dalam hierarki file kami. Parent
menavigasi satu tingkat, sementara Root
menavigasi sampai ke atas.
// 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
, dan Root
dapat dirangkai beberapa kali, karena masing-masing mengembalikan referensi. Pengecualian adalah Parent
of Root
, yang merupakan StorageReference
tidak valid.
// 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();
Metode Referensi
Anda dapat memeriksa referensi untuk lebih memahami file yang mereka tunjuk menggunakan metode full_path
, name
, dan bucket
. Metode ini mendapatkan path lengkap, nama, dan bucket file.
// 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();
Keterbatasan Referensi
Jalur dan nama referensi dapat berisi urutan karakter Unicode yang valid, tetapi batasan tertentu diberlakukan termasuk:
- Panjang total reference.fullPath harus antara 1 dan 1024 byte saat dikodekan UTF-8.
- Tidak ada karakter Carriage Return atau Line Feed.
- Hindari menggunakan
#
,[
,]
,*
, atau?
, karena ini tidak bekerja dengan baik dengan alat lain seperti Firebase Realtime Database atau gsutil .
Contoh Lengkap
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();
Langkah selanjutnya
Selanjutnya, mari pelajari cara mengupload file ke Cloud Storage.