Membuat referensi Cloud Storage di Android

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 getReference()-nya.

Kotlin+KTX

// Create a storage reference from our app
var storageRef = storage.reference

Java

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

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.

Kotlin+KTX

// Create a child reference
// imagesRef now points to "images"
var imagesRef: StorageReference? = storageRef.child("images")

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

Java

// Create a child reference
// imagesRef now points to "images"
StorageReference imagesRef = storageRef.child("images");

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

Anda juga dapat menggunakan metode getParent() dan getRoot() untuk naik ke atas dalam hierarki file. getParent() naik satu level ke atas, sedangkan getRoot() naik ke level paling atas.

Kotlin+KTX

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

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

Java

// getParent allows us to move our reference to a parent node
// imagesRef now points to 'images'
imagesRef = spaceRef.getParent();

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

child(), getParent(), dan getRoot() bisa dirantai bersama beberapa kali, karena masing-masing menampilkan referensi. Namun, null akan ditampilkan jika getRoot().getParent() dipanggil.

Kotlin+KTX

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

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

Java

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

// nullRef is null, since the parent of root is null
StorageReference nullRef = spaceRef.getRoot().getParent();

Properti Referensi

Anda dapat memeriksa referensi untuk lebih memahami file yang ditunjuk dengan menggunakan metode getPath(), getName(), dan getBucket(). Metode ini akan mendapatkan nama, bucket, dan jalur lengkap dari file tersebut.

Kotlin+KTX

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
spaceRef.path

// 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

Java

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
spaceRef.getPath();

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
spaceRef.getName();

// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.getBucket();

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

Kotlin+KTX

// Points to the root reference
storageRef = storage.reference

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

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

// File path is "images/space.jpg"
val path = spaceRef.path

// File name is "space.jpg"
val name = spaceRef.name

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

Java

// Points to the root reference
storageRef = storage.getReference();

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

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

// File path is "images/space.jpg"
String path = spaceRef.getPath();

// File name is "space.jpg"
String name = spaceRef.getName();

// Points to "images"
imagesRef = spaceRef.getParent();

Selanjutnya, mari pelajari cara mengupload file ke Cloud Storage.