C++ için Cloud Storage ile Cloud Storage referansı oluşturma

Dosyalarınız bir Bulut Depolama 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 dosyaya erişim kazanır. Bu referanslar daha sonra veri 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 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 dosyaya işaretçi olarak düşünülebilir. Referanslar hafiftir, böylece ihtiyacınız olduğu kadar referans oluşturabilirsiniz. Ayrıca birden çok işlem için yeniden kullanılabilirler.

Referanslar, GetReferenceFromUrl() yöntemi çağrılarak ve gs://<your-cloud-storage-bucket> biçiminde bir URL geçirilerek Firebase uygulamanızdaki storage hizmetinden 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
Storage* storage = Storage::GetInstance(app);

// Create a Cloud Storage reference from our storage service
StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-cloud-storage-bucket>");

Varolan bir başvuruda child yöntemi kullanarak ağacın alt kısmındaki bir konuma, 'images/space.jpg' gibi bir başvuru oluşturabilirsiniz.

// Create a child reference
// images_ref now points to "images"
StorageReference images_ref = storage_ref.Child("images");

// Child references can also take paths delimited by '/'
// space_ref now points to "images/space.jpg"
// images_ref still points to "images"
StorageReference space_ref = storage_ref.Child("images/space.jpg");

// This is equivalent to creating the full reference
StorageReference space_ref = storage.GetReferenceFromUrl("gs://<your-cloud-storage-bucket>/images/space.jpg");

Dosya hiyerarşimizde yukarı gitmek için Parent ve Root yöntemlerini de kullanabilirsiniz. Parent bir düzey yukarı çıkarken, Root en üste kadar gider.

// Parent allows us to move to the parent of a reference
// images_ref now points to 'images'
StorageReference images_ref = space_ref.Parent();

// Root allows us to move all the way back to the top of our bucket
// root_ref now points to the root
StorageReference root_ref = space_ref.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 .

// References can be chained together multiple times
// earth_ref points to "images/earth.jpg"
StorageReference earth_ref = space_ref.Parent().Child("earth.jpg");

// null_ref is null, since the parent of root is an invalid StorageReference
StorageReference null_ref = space_ref.Root().Parent();

Referans Yöntemleri

full_path , name ve bucket yöntemlerini kullanarak işaret ettikleri dosyaları daha iyi anlamak için referansları inceleyebilirsiniz. Bu yöntemler dosyanın tam yolunu, adını ve klasörünü alır.

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

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

// Reference's bucket is the name of the Cloud Storage bucket where files are stored
space_ref.bucket();

Referanslarla İlgili Sınırlamalar

Başvuru yolları ve adlar, herhangi bir geçerli Unicode karakter dizisini içerebilir, ancak aşağıdakiler dahil olmak üzere belirli kısıtlamalar uygulanır:

  1. UTF-8 olarak kodlandığında, reference.fullPath öğesinin toplam uzunluğu 1 ile 1024 bayt arasında olmalıdır.
  2. Satır Başı veya Satır Besleme karakterleri yok.
  3. # , [ , ] , * veya ? kullanmaktan kaçının , çünkü bunlar Firebase Gerçek Zamanlı Veritabanı veya gsutil gibi diğer araçlarla iyi çalışmaz.

Tam Örnek

Storage* storage = Storage::GetInstance(app);

// Points to the root reference
StorageReference storage_ref = storage->GetReferenceFromUrl("gs://<your-bucket-name>");

// Points to "images"
StorageReference images_ref = storage_ref.Child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
std::string filename = "space.jpg";
StorageReference space_ref = images_ref.Child(filename);

// File path is "images/space.jpg"
std::string path = space_ref.full_path()

// File name is "space.jpg"
std::string name = space_ref.name()

// Points to "images"
StorageReference images_ref = space_ref.Parent();

Sonraki adımlar

Ardından, dosyaların Cloud Storage'a nasıl yükleneceğini öğrenelim.