สร้างการอ้างอิง Cloud Storage ใน Flutter

ระบบจะจัดเก็บไฟล์ไว้ใน Bucket ของ Cloud Storage ไฟล์ในที่เก็บข้อมูลนี้จะแสดงในโครงสร้างแบบลำดับชั้น เช่นเดียวกับ ระบบไฟล์ในฮาร์ดดิสก์ในเครื่อง หรือข้อมูลในฐานข้อมูลเรียลไทม์ของ Firebase การสร้างการอ้างอิงถึงไฟล์จะทำให้แอปของคุณมีสิทธิ์เข้าถึงไฟล์นั้น จากนั้นจะใช้ข้อมูลอ้างอิงเหล่านี้เพื่ออัปโหลดหรือดาวน์โหลดข้อมูล รับหรืออัปเดตข้อมูลเมตา หรือลบไฟล์ได้ การอ้างอิงอาจชี้ไปยังไฟล์ที่เฉพาะเจาะจงหรือไปยังโหนดระดับที่สูงกว่า ในลำดับชั้น

หากคุณเคยใช้ ฐานข้อมูลเรียลไทม์ของ Firebase คุณจะคุ้นเคยกับเส้นทางเหล่านี้เป็นอย่างดี อย่างไรก็ตาม ข้อมูลไฟล์จะจัดเก็บไว้ใน Cloud Storage ไม่ใช่ใน Realtime Database

สร้างการอ้างอิง

สร้างการอ้างอิงเพื่ออัปโหลด ดาวน์โหลด หรือลบไฟล์ หรือเพื่อรับหรืออัปเดตข้อมูลเมตาของไฟล์ การอ้างอิง อาจถือได้ว่าเป็นตัวชี้ไปยังไฟล์ในระบบคลาวด์ การอ้างอิงมีขนาดเล็ก คุณจึงสร้างได้มากเท่าที่ต้องการ และยังใช้ซ้ำได้สำหรับการดำเนินการหลายอย่าง

สร้างการอ้างอิงโดยใช้FirebaseStorageอินสแตนซ์ Singleton และ เรียกใช้เมธอด ref()

final storageRef = FirebaseStorage.instance.ref();

จากนั้นคุณจะสร้างการอ้างอิงไปยังตำแหน่งที่อยู่ลึกลงไปในโครงสร้างได้ เช่น "images/space.jpg" โดยใช้วิธี child() กับการอ้างอิงที่มีอยู่

// Create a child reference
// imagesRef now points to "images"
final imagesRef = storageRef.child("images");

// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
final spaceRef = storageRef.child("images/space.jpg");

นอกจากนี้ คุณยังใช้พร็อพเพอร์ตี้ parent และ root เพื่อไปยังระดับที่สูงขึ้นในลำดับชั้นของไฟล์ได้ด้วย parent จะเลื่อนขึ้นไป 1 ระดับ ส่วน root จะเลื่อนขึ้นไปจนถึงระดับบนสุด

// parent allows us to move our reference to a parent node
// imagesRef2 now points to 'images'
final imagesRef2 = spaceRef.parent;

// root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
final rootRef = spaceRef.root;

child(), parent และ root สามารถเชื่อมโยงกันได้หลายครั้ง เนื่องจากแต่ละรายการเป็นข้อมูลอ้างอิง แต่การเข้าถึง root.parent จะทำให้เกิด null

// References can be chained together multiple times
// earthRef points to 'images/earth.jpg'
final earthRef = spaceRef.parent?.child("earth.jpg");

// nullRef is null, since the parent of root is null
final nullRef = spaceRef.root.parent;

พร็อพเพอร์ตี้อ้างอิง

คุณสามารถตรวจสอบการอ้างอิงเพื่อทำความเข้าใจไฟล์ที่อ้างอิงได้ดียิ่งขึ้น โดยใช้พร็อพเพอร์ตี้ fullPath, name และ bucket พร็อพเพอร์ตี้เหล่านี้ จะรับเส้นทางแบบเต็ม ชื่อ และที่เก็บข้อมูลของไฟล์

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
spaceRef.fullPath;

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

// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.bucket;

ข้อจำกัดเกี่ยวกับข้อมูลอ้างอิง

เส้นทางและชื่ออ้างอิงอาจมีลำดับของอักขระ Unicode ที่ถูกต้อง แต่มีข้อจำกัดบางอย่าง ได้แก่

  1. ความยาวทั้งหมดของ reference.fullPath ต้องอยู่ระหว่าง 1 ถึง 1024 ไบต์เมื่อเข้ารหัส UTF-8
  2. ไม่มีอักขระขึ้นบรรทัดใหม่หรืออักขระตัดบรรทัด
  3. หลีกเลี่ยงการใช้ #, [, ], * หรือ ? เนื่องจากไม่ทำงานร่วมกับ เครื่องมืออื่นๆ เช่น ฐานข้อมูลเรียลไทม์ของ Firebase หรือ gsutil ได้ดี

ตัวอย่างแบบเต็ม

// Points to the root reference
final storageRef = FirebaseStorage.instance.ref();

// Points to "images"
Reference? imagesRef = storageRef.child("images");

// Points to "images/space.jpg"
// Note that you can use variables to create child values
final fileName = "space.jpg";
final spaceRef = imagesRef.child(fileName);

// File path is "images/space.jpg"
final path = spaceRef.fullPath;

// File name is "space.jpg"
final name = spaceRef.name;

// Points to "images"
imagesRef = spaceRef.parent;

ถัดไป เรามาดูวิธีอัปโหลดไฟล์ไปยัง Cloud Storage กัน