Membuat referensi Cloud Storage dengan Cloud Storage untuk C++

File Anda disimpan di bucket Cloud Storage. File di bucket ini disajikan secara hierarkis, seperti sistem file pada hard disk lokal atau data dalam Firebase Realtime Database. Dengan membuat referensi ke sebuah file, aplikasi Anda akan mendapatkan akses ke file tersebut. Selanjutnya, referensi ini bisa digunakan untuk mengupload atau mendownload data, memperoleh atau mengupdate metadata, atau menghapus file. Referensi bisa menunjuk ke file tertentu atau ke node bertingkat lebih tinggi dalam hierarki.

Jika Anda telah menggunakan Firebase Realtime Database, jalur ini tentu sudah tidak asing bagi Anda. Namun, data file Anda tersimpan di Cloud Storage, bukan di Realtime Database.

Membuat Referensi

Buat referensi untuk mengupload, mendownload, atau menghapus file, atau untuk memperoleh atau memperbarui metadatanya. Referensi bisa dianggap sebagai penunjuk ke file di cloud. Referensi berukuran kecil, sehingga Anda bisa membuat sebanyak yang dibutuhkan. Referensi juga bisa 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 bisa menemukan URL ini di bagian Storage pada 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 pada hierarki, misalnya 'images/space.jpg', dengan menggunakan metode child pada referensi yang sudah 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");

Anda juga dapat menggunakan metode Parent dan Root untuk naik ke atas dalam hierarki file. Parent berarti naik satu level ke atas, sedangkan Root naik ke level paling 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 bisa digabungkan bersama beberapa kali, karena masing-masing menampilkan referensi. Pengecualiannya adalah Parent dari Root, yang merupakan StorageReference yang 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 bisa memeriksa referensi agar lebih dapat memahami file yang dituju oleh referensi tersebut menggunakan metode full_path, name, dan bucket. Metode ini akan mengambil jalur lengkap, nama, dan bucket file tersebut.

// 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();

Pembatasan Referensi

Jalur dan nama referensi bisa memuat urutan karakter Unicode yang valid, tetapi ada pembatasan tertentu yang diberlakukan, antara lain:

  1. Panjang total reference.fullPath harus antara 1 dan 1.024 byte jika berenkode UTF-8.
  2. Tidak mengandung karakter Enter atau Line Feed.
  3. Hindari penggunaan #, [, ], *, atau ?, karena karakter tersebut tidak kompatibel 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 Berikutnya

Selanjutnya, mari pelajari cara mengupload file ke Cloud Storage.