ระบบจะจัดเก็บไฟล์ไว้ในบัคเก็ต Cloud Storage โดยไฟล์ในบัคเก็ตนี้จะแสดงในโครงสร้างแบบมีลำดับชั้น เช่นเดียวกับระบบไฟล์ในฮาร์ดดิสก์ในเครื่องหรือข้อมูลใน Firebase Realtime Database การสร้างข้อมูลอ้างอิงถึงไฟล์จะทำให้แอปมีสิทธิ์เข้าถึงไฟล์ดังกล่าว จากนั้นคุณจะใช้ข้อมูลอ้างอิงเหล่านี้เพื่ออัปโหลดหรือดาวน์โหลดข้อมูล รับหรืออัปเดตข้อมูลเมตา หรือลบไฟล์ได้ ข้อมูลอ้างอิงอาจชี้ไปยังไฟล์ที่เฉพาะเจาะจงหรือโหนดระดับสูงกว่าในลำดับชั้นก็ได้
หากคุณเคยใช้ Firebase Realtime Database มาก่อน คุณจะคุ้นเคยกับเส้นทางเหล่านี้เป็นอย่างดี อย่างไรก็ตาม ระบบจะจัดเก็บข้อมูลไฟล์ไว้ใน Cloud Storage, ไม่ใช่ ใน Realtime Database
สร้างการอ้างอิง
สร้างข้อมูลอ้างอิงเพื่ออัปโหลด ดาวน์โหลด หรือลบไฟล์ หรือเพื่อรับหรืออัปเดตข้อมูลเมตาของไฟล์ คุณสามารถนึกภาพข้อมูลอ้างอิงเป็นตัวชี้ไปยังไฟล์ในระบบคลาวด์ ข้อมูลอ้างอิงมีขนาดเล็ก คุณจึงสร้างได้มากเท่าที่ต้องการ นอกจากนี้ คุณยังใช้ข้อมูลอ้างอิงซ้ำได้สำหรับการดำเนินการหลายอย่าง
คุณสร้างข้อมูลอ้างอิงจากบริการ Firebase.Storage.FirebaseStorage ในแอป Firebase
ได้โดยเรียกใช้เมธอด GetReferenceFromUrl() และส่ง URL ในรูปแบบ gs://<your-cloud-storage-bucket> คุณดู URL นี้ได้ใน
ส่วนพื้นที่เก็บข้อมูลของFirebaseคอนโซล
// 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>");
คุณสร้างข้อมูลอ้างอิงไปยังตำแหน่งที่ต่ำกว่าในโครงสร้างแบบต้นไม้ได้ เช่น
'images/space.jpg' โดยใช้เมธอด child ในข้อมูลอ้างอิงที่มีอยู่
// 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");
ไปยังส่วนต่างๆ ด้วยข้อมูลอ้างอิง
นอกจากนี้ คุณยังใช้เมธอด Parent และ Root เพื่อไปยังส่วนต่างๆ ในลำดับชั้นของไฟล์ได้ Parent จะไปยังส่วนต่างๆ ในระดับที่สูงขึ้น 1 ระดับ ส่วน Root จะไปยังส่วนต่างๆ ที่ระดับบนสุด
// 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 และ Root เข้าด้วยกันหลายครั้งได้ เนื่องจากแต่ละรายการจะแสดงข้อมูลอ้างอิง ข้อยกเว้นคือ Parent ของ 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;
เมธอดข้อมูลอ้างอิง
คุณสามารถตรวจสอบข้อมูลอ้างอิงเพื่อทำความเข้าใจไฟล์ที่ข้อมูลอ้างอิงชี้ได้ดียิ่งขึ้นโดยใช้พร็อพเพอร์ตี้ Path, Name และ Bucket พร็อพเพอร์ตี้เหล่านี้จะแสดงเส้นทางแบบเต็ม ชื่อ และบัคเก็ตของไฟล์
// 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;
ข้อจำกัดเกี่ยวกับข้อมูลอ้างอิง
เส้นทางและชื่อข้อมูลอ้างอิงอาจมีการเรียงลำดับอักขระ Unicode ที่ถูกต้อง แต่จะมีข้อจำกัดบางอย่าง ได้แก่
- ความยาวทั้งหมดของ
reference.Pathต้องอยู่ระหว่าง 1 ถึง 1024 ไบต์เมื่อเข้ารหัส UTF-8 - ไม่มีอักขระขึ้นบรรทัดใหม่หรือป้อนบรรทัด
- หลีกเลี่ยงการใช้
#,[,],*หรือ?เนื่องจากอักขระเหล่านี้ทำงานได้ไม่ดีกับ เครื่องมืออื่นๆ เช่น Firebase Realtime Database หรือ gsutil
ตัวอย่างแบบเต็ม
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;
ขั้นตอนถัดไป
จากนั้นเราจะมาดูวิธี อัปโหลดไฟล์ไปยัง Cloud Storageกัน