Unity için Cloud Storage ile Cloud Storage referansı oluşturun

Dosyalarınız bir Cloud Storage paketinde depolanır. Bu paketteki 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 ya da dosyayı silmek için kullanılabilir. Bir referans, belirli bir dosyaya veya hiyerarşideki daha yüksek düzeydeki bir düğüme işaret edebilir.

Firebase Gerçek Zamanlı Veritabanını kullandıysanız bu yollar size çok tanıdık gelecektir. Ancak dosya verileriniz Gerçek Zamanlı Veritabanında değil Bulut Depolamada depolanır.

Referans Oluştur

Bir dosyayı yüklemek, indirmek veya silmek ya da meta verilerini almak veya güncellemek için bir referans oluşturun. Referans, buluttaki bir dosyanın işaretçisi olarak düşünülebilir. Referanslar hafiftir, dolayısıyla ihtiyaç duyduğunuz kadar referans oluşturabilirsiniz. Ayrıca birden fazla işlem için yeniden kullanılabilirler.

Referanslar, Firebase uygulamanızdaki Firebase.Storage.FirebaseStorage hizmetinden 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 referans üzerinde child yöntemi kullanarak, ağacın daha altındaki bir konuma, örneğin 'images/space.jpg' bir 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");

Dosya hiyerarşimizde yukarı doğru gezinmek için Parent ve Root yöntemlerini de kullanabilirsiniz. Parent bir seviye yukarı çıkarken, Root en tepeye 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. Bunun istisnası, geçersiz bir StorageReference olan Root öğesinin Parent öğesidir.

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

Referanslarla İlgili Sınırlamalar

Referans 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:

  1. reference.Path toplam uzunluğu. UTF-8 kodlandığında yol 1 ile 1024 bayt arasında olmalıdır.
  2. Satırbaşı veya Satır Besleme karakterleri yok.
  3. # , [ , ] , * veya ? kullanmaktan kaçının. çü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

Şimdi dosyaları Cloud Storage'a nasıl yükleyeceğimizi öğrenelim.