Dosyalarınız bir Cloud Storage paketinde depolanır. Bu paketteki dosyalar, yerel sabit diskinizdeki dosya sistemi veya Firebase Realtime Database'deki veriler gibi hiyerarşik bir yapıda sunulur. Bir dosyaya referans oluşturduğunuzda uygulamanız ilgili dosyaya erişim kazanır. Bu referanslar daha sonra veri yüklemek veya indirmek, meta verileri almak ya da güncellemek ya da dosyayı silmek için kullanılabilir. Referans, belirli bir dosyaya veya hiyerarşide daha üst düzey bir düğüme işaret edebilir.
Firebase Realtime Database'i kullandıysanız bu yollar size çok tanıdık gelmelidir. Ancak dosya verileriniz Realtime Database'de değil, Cloud Storage'da depolanır.
Referans Oluşturma
Dosya yüklemek, indirmek veya silmek ya da meta verilerini almak veya güncellemek için referans oluşturun. Referans, buluttaki bir dosyaya işaret eden bir işaret olarak düşünülebilir. Referanslar hafif olduğundan istediğiniz kadar referans oluşturabilirsiniz. Ayrıca birden fazla işlem için yeniden kullanılabilir.
Referanslar, GetReferenceFromUrl()
yöntemini çağırıp gs://<your-cloud-storage-bucket>
biçiminde bir URL ileterek Firebase uygulamanızdaki Firebase.Storage.FirebaseStorage
hizmetinden oluşturulur. Bu URL'yi Firebase konsolunun Depolama alanı 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öntemini kullanarak ağaçta daha aşağıda bir konuma (ör. 'images/space.jpg'
) başvuru 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 Gezinme
Dosya hiyerarşimizde yukarı gitmek için Parent
ve Root
yöntemlerini de kullanabilirsiniz. Parent
bir seviye yukarı giderken Root
en üst düzeye ulaşır.
// 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üğünden birden çok kez zincirlenebilir. İstisna, geçersiz bir StorageReference
olan Root
Parent
değeridir.
// 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 referansların yönlendirdiği 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;
Referanslarla ilgili Sınırlamalar
Referans yolları ve adlar, herhangi bir geçerli Unicode karakter dizisini içerebilir ancak aşağıdakileri içeren belirli kısıtlamalar uygulanır:
- UTF-8 olarak kodlandığında
reference.Path
toplam uzunluğu 1 ile 1.024 bayt arasında olmalıdır. - Satır Başı veya Satır Feed'i karakterleri yok.
- Firebase Realtime Database veya gsutil gibi diğer araçlarla çalışmadığından
#
,[
,]
,*
ya da?
kullanmaktan kaçının.
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
Şimdi de Cloud Storage'a dosya yüklemeyi öğrenelim.