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

Dosyalarınız bir Cloud Storage for Firebase paketinde depolanır. Bu paketteki dosyalar, yerel sabit diskinizdeki dosya sistemi gibi hiyerarşik bir yapıda sunulur.

Uygulamanız, bir dosyaya referans oluşturarak dosyaya erişim elde eder. Referans, belirli bir dosyayı veya hiyerarşideki daha üst düzey bir düğümü işaret edebilir. Referansı, buluttaki dosyaya veya düğüme işaret eden bir gösterge olarak düşünebilirsiniz. Referans oluşturduktan sonra, referansı kullanarak işlem yapabilirsiniz (ör. veri yükleme veya indirme, meta veri alma veya güncelleme ya da dosya silme).

Bu sayfada, referans oluşturmak ve referanslarla çalışmak için Cloud Storage for Firebase SDK'sının nasıl kullanılacağı açıklanmaktadır.

Referans oluşturma

Bir dosya üzerinde işlem yapmak için önce dosyaya referans oluşturmanız gerekir. Referanslar basit olduğundan, ihtiyacınız olduğu kadar referans oluşturabilirsiniz. Ayrıca birden fazla işlem için tekrar 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 referansta child yöntemini kullanarak ağacın daha alt kısımlarındaki bir konuma (ör. 'images/space.jpg') 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şinizde yukarı gitmek için Parent ve Root yöntemlerini de kullanabilirsiniz. Parent bir seviye yukarı giderken Root en üst seviyeye 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 bağlanabilir. Root için Parent istisnadır. Bu, geçersiz bir StorageReference 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ı inceleyip işaret ettikleri dosyaları daha iyi anlayabilirsiniz. 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ı, geçerli Unicode karakterlerinden oluşan herhangi bir diziyi içerebilir ancak aşağıdakiler de dahil olmak üzere belirli kısıtlamalar uygulanır:

  1. reference.Path dizesinin toplam uzunluğu, UTF-8 kodlamalı olduğunda 1 ile 1.024 bayt arasında olmalıdır.
  2. Satır başı karakteri veya satır sonu karakteri içermemelidir.
  3. #, [, ], * veya ? kullanmaktan kaçının. Bu karakterler, 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

Sonraki adımda, Cloud Storage'e nasıl dosya yükleyeceğinizi öğrenin.