Membuat referensi Cloud Storage di Flutter

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.

Buat referensi menggunakan instance singleton FirebaseStorage dan dengan memanggil metode ref()-nya.

final storageRef = FirebaseStorage.instance.ref();

Selanjutnya, 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
// imagesRef now points to "images"
final imagesRef = storageRef.child("images");

// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
final spaceRef = storageRef.child("images/space.jpg");

Anda juga dapat menggunakan properti parent dan root untuk naik ke atas dalam hierarki file. parent naik satu level ke atas, sedangkan root naik ke level paling atas.

// parent allows us to move our reference to a parent node
// imagesRef2 now points to 'images'
final imagesRef2 = spaceRef.parent;

// root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
final rootRef = spaceRef.root;

child(), parent, dan root bisa dirantai bersama beberapa kali, karena masing-masing merupakan referensi. Namun, mengakses root.parent menghasilkan null.

// References can be chained together multiple times
// earthRef points to 'images/earth.jpg'
final earthRef = spaceRef.parent?.child("earth.jpg");

// nullRef is null, since the parent of root is null
final nullRef = spaceRef.root.parent;

Properti Referensi

Anda bisa memeriksa referensi agar lebih dapat memahami file yang dituju oleh referensi tersebut menggunakan properti fullPath, name, dan bucket. Properti ini akan mengambil nama, bucket, dan jalur lengkap dari file tersebut.

// 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 that the files are stored in
spaceRef.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

// Points to the root reference
final storageRef = FirebaseStorage.instance.ref();

// Points to "images"
Reference? imagesRef = storageRef.child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
final fileName = "space.jpg";
final spaceRef = imagesRef.child(fileName);

// File path is "images/space.jpg"
final path = spaceRef.fullPath;

// File name is "space.jpg"
final name = spaceRef.name;

// Points to "images"
imagesRef = spaceRef.parent;

Selanjutnya, mari pelajari cara mengupload file ke Cloud Storage.