Dosyalarınız bir Cloud Storage paketinde depolanır. Bu kovadaki dosyalar, tıpkı yerel sabit diskinizdeki dosya sistemi veya Firebase Gerçek Zamanlı Veritabanındaki veriler gibi hiyerarşik bir yapıda sunulur. Bir dosyaya referans oluşturarak uygulamanız ona erişim kazanır. Bu referanslar daha sonra verileri yüklemek veya indirmek, meta verileri almak veya güncellemek veya dosyayı silmek için kullanılabilir. Bir başvuru, belirli bir dosyaya veya hiyerarşide daha yüksek bir düzey düğüme işaret edebilir.
Firebase Realtime Database kullandıysanız, bu yollar size çok tanıdık gelecektir. Ancak dosya verileriniz Realtime Database'de değil , Cloud Storage'da depolanır.
Referans Oluştur
Bir dosyayı karşıya yüklemek, indirmek veya silmek ya da meta verilerini almak veya güncellemek için bir referans oluşturun. Referans, buluttaki bir dosyaya işaretçi olarak düşünülebilir. Referanslar hafiftir, böylece ihtiyaç duyduğunuz kadarını oluşturabilirsiniz. Ayrıca birden fazla işlem için yeniden kullanılabilirler.
Referanslar, Firebase uygulamanızdaki Firebase.Storage.FirebaseStorage
GetReferenceFromUrl()
yöntemi çağrılarak ve gs://<your-cloud-storage-bucket>
biçiminde bir URL iletilerek oluşturulur. Bu URL'yi Firebase konsolunun Depolama bölümünde bulabilirsiniz.
// Get a reference to the storage service, using the default Firebase App FirebaseStorage storage = FirebaseStorage.DefaultInstance; // Create a storage reference from our storage service StorageReference storageRef = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");
Mevcut bir referansta child
yöntemi kullanarak, örneğin 'images/space.jpg'
gibi ağaçta daha düşük bir konuma referans oluşturabilirsiniz.
// Create a child reference // imagesRef now points to "images" StorageReference imagesRef = storageRef.Child("images"); // Child references can also take paths delimited by '/' such as: // "images/space.jpg". StorageReference spaceRef = imagesRef.Child("space.jpg"); // spaceRef now points to "images/space.jpg" // imagesRef still points to "images" // This is equivalent to creating the full referenced StorageReference spaceRefFull = storage.GetReferenceFromUrl( "gs://<your-cloud-storage-bucket>/images/space.jpg");
Referanslarla Gezin
Dosya hiyerarşimizde yukarı gitmek için Parent
ve Root
yöntemlerini de kullanabilirsiniz. Parent
bir seviye yukarı gider, Root
ise en üste kadar gider.
// Parent allows us to move to the parent of a reference // imagesRef now points to 'images' StorageReference imagesRef = spaceRef.Parent; // Root allows us to move all the way back to the top of our bucket // rootRef now points to the root StorageReference rootRef = spaceRef.Root;
Child
, Parent
ve Root
her biri bir referans döndürdüğü için birden çok kez birbirine zincirlenebilir. İstisna, geçersiz bir Parent
olan Root
StorageReference
.
// References can be chained together multiple times // earthRef points to "images/earth.jpg" StorageReference earthRef = spaceRef.Parent.Child("earth.jpg"); // nullRef is null since the parent of root is an invalid StorageReference StorageReference nullRef = spaceRef.Root.Parent;
Referans Yöntemleri
Path
, Name
ve Bucket
özelliklerini kullanarak işaret ettikleri dosyaları daha iyi anlamak için referansları inceleyebilirsiniz. Bu özellikler dosyanın tam yolunu, adını ve paketini alır.
// Reference's path is: "images/space.jpg" // This is analogous to a file path on disk string path = spaceRef.Path; // Reference's name is the last segment of the full path: "space.jpg" // This is analogous to the file name string name = spaceRef.Name; // Reference's bucket is the name of the storage bucket where files are stored string bucket = spaceRef.Bucket;
Referanslara İlişkin Sınırlamalar
Başvuru yolları ve adlar, herhangi bir geçerli Unicode karakter dizisini içerebilir, ancak aşağıdakiler de dahil olmak üzere belirli kısıtlamalar uygulanır:
- Toplam
reference.Path
uzunluğu. UTF-8 kodlandığında, yol 1 ile 1024 bayt arasında olmalıdır. - Satır Başı veya Satır Besleme karakterleri yok.
-
#
,[
,]
,*
, veya?
, çünkü bunlar Firebase Realtime Database veya gsutil gibi diğer araçlarla iyi çalışmaz.
Tam Örnek
FirebaseStorage storage = FirebaseStorage.DefaultInstance; // Points to the root reference StorageReference storageRef = storage.GetReferenceFromUrl("gs://<your-bucket-name>"); // Points to "images" StorageReference imagesRef = storageRef.Child("images"); // Points to "images/space.jpg" // Note that you can use variables to create child values string filename = "space.jpg"; StorageReference spaceRef = imagesRef.Child(filename); // File path is "images/space.jpg" string path = spaceRef.Path; // File name is "space.jpg" string name = spaceRef.Name; // Points to "images" StorageReference imagesRef = spaceRef.Parent;
Sonraki adımlar
Ardından, dosyaları Cloud Storage'a nasıl yükleyeceğinizi öğrenelim.