Cloud Storage for Firebase ช่วยให้คุณดาวน์โหลดไฟล์จากบักเก็ต Cloud Storage ที่ Firebase มีให้และจัดการได้อย่างรวดเร็วและง่ายดาย
สร้างการอ้างอิง
หากต้องการดาวน์โหลดไฟล์ ก่อนอื่นให้สร้างการอ้างอิง Cloud Storage ไปยังไฟล์ที่ต้องการดาวน์โหลด
คุณสร้างข้อมูลอ้างอิงได้โดยเพิ่มเส้นทางย่อยต่อท้ายรูทของที่เก็บข้อมูล Cloud Storage หรือจะสร้างข้อมูลอ้างอิงจาก URL ของ gs:// หรือ https:// ที่มีอยู่ซึ่งอ้างอิงออบเจ็กต์ใน Cloud Storage ก็ได้
// Create a reference with an initial file path and name StorageReference path_reference = storage->GetReference("images/stars.jpg"); // Create a reference from a Cloud Storage URI StorageReference gs_reference = storage->GetReferenceFromUrl("gs://bucket/images/stars.jpg"); // Create a reference from an HTTPS URL // Note that in the URL, characters are URL escaped! StorageReference https_reference = storage->GetReferenceFromUrl("https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg");
ดาวน์โหลดไฟล์
เมื่อมีข้อมูลอ้างอิงแล้ว คุณจะดาวน์โหลดไฟล์จาก Cloud Storage ได้ 3 วิธีดังนี้
- ดาวน์โหลดไปยังบัฟเฟอร์ในหน่วยความจำ
- ดาวน์โหลดไปยังเส้นทางที่เฉพาะเจาะจงในอุปกรณ์
- สร้าง URL สตริงที่แสดงไฟล์ออนไลน์
ดาวน์โหลดในหน่วยความจำ
ดาวน์โหลดไฟล์ไปยังบัฟเฟอร์ไบต์ในหน่วยความจำโดยใช้เมธอด GetBytes() วิธีนี้เป็นวิธีที่ง่ายที่สุดในการดาวน์โหลดไฟล์อย่างรวดเร็ว แต่ต้องโหลดเนื้อหาทั้งหมดของไฟล์ลงในหน่วยความจำ หากคุณขอไฟล์ที่มีขนาดใหญ่กว่าหน่วยความจำที่แอปมีอยู่ แอปจะขัดข้อง เพื่อป้องกันปัญหาเกี่ยวกับหน่วยความจำ โปรดตรวจสอบว่าได้ตั้งค่าขนาดสูงสุดเป็นขนาดที่คุณทราบว่าแอปสามารถจัดการได้ หรือใช้วิธีการดาวน์โหลดอื่น
// Create a reference to the file you want to download StorageReference island_ref = storage_ref.Child("images/island.jpg"); // Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes) const size_t kMaxAllowedSize = 1 * 1024 * 1024 int8_t byte_buffer[kMaxAllowedSize]; firebase::Future<size_t> future = island_ref.GetBytes(byte_buffer, kMaxAllowedSize);
ณ จุดนี้ ระบบได้ส่งคำขอแล้ว แต่เราต้องรอให้ Future ดำเนินการเสร็จสมบูรณ์ก่อนจึงจะอ่านไฟล์ได้ เนื่องจากเกมมักจะทำงานแบบวนซ้ำและมีการเรียกกลับน้อยกว่าแอปพลิเคชันอื่นๆ โดยทั่วไปคุณจึงต้องทำการสำรวจเพื่อดูว่าการดำเนินการเสร็จสมบูรณ์หรือไม่
// In the game loop that polls for the result... if (future.status() != firebase::kFutureStatusPending) { if (future.status() != firebase::kFutureStatusComplete) { LogMessage("ERROR: GetBytes() returned an invalid future."); // Handle the error... } else if (future.Error() != firebase::storage::kErrorNone) { LogMessage("ERROR: GetBytes() returned error %d: %s", future.Error(), future.error_message()); // Handle the error... } else { // byte_buffer is now populated with data for "images/island.jpg" } }
ดาวน์โหลดเป็นไฟล์ในเครื่อง
เมธอด GetFile() จะดาวน์โหลดไฟล์ไปยังอุปกรณ์ในเครื่องโดยตรง ใช้ตัวเลือกนี้หากผู้ใช้ต้องการเข้าถึงไฟล์ขณะออฟไลน์หรือแชร์ในแอปอื่น
// Create a reference to the file you want to download StorageReference islandRef = storage_ref.Child("images/island.jpg"]; // Create local filesystem URL const char* local_url = "file:///local/images/island.jpg"; // Download to the local filesystem Future<size_t> future = islandRef.GetFile(local_url); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // The file has been downloaded to local file URL "images/island.jpg" }
GetFile() ใช้อาร์กิวเมนต์ Controller ที่ไม่บังคับซึ่งคุณสามารถใช้เพื่อจัดการการดาวน์โหลดได้ ดูข้อมูลเพิ่มเติมได้ที่จัดการการดาวน์โหลด
สร้าง URL สำหรับดาวน์โหลด
หากคุณมีโครงสร้างพื้นฐานการดาวน์โหลดที่อิงตาม URL อยู่แล้ว หรือเพียงต้องการ URL เพื่อแชร์ คุณสามารถรับ URL การดาวน์โหลดสำหรับไฟล์ได้โดยเรียกใช้เมธอด GetDownloadUrl() ในการอ้างอิง Cloud Storage
// Create a reference to the file you want to download StorageReference stars_ref = storage_ref.Child("images/stars.jpg"); // Fetch the download URL firebase::Future<std::string> future = stars_ref.GetDownloadUrl(); // Wait for Future to complete... if (future.Error() != firebase::storage::kErrorNone) { // Uh-oh, an error occurred! } else { // Get the download URL for 'images/stars.jpg' std::string download_url = future.Result(); }
จัดการการดาวน์โหลด
นอกจากการเริ่มดาวน์โหลดแล้ว คุณยังหยุดชั่วคราว ดาวน์โหลดต่อ และยกเลิกการดาวน์โหลดได้โดยใช้เมธอด Pause(), Resume() และ Cancel() ใน Controller ซึ่งคุณอาจเลือกส่งไปยังเมธอด GetBytes() หรือ GetFile() ก็ได้
// Start downloading a file Controller controller; storage_ref.Child("images/mountains.jpg").GetFile(local_file, nullptr, &controller); // Pause the download controller.Pause(); // Resume the download controller.Resume(); // Cancel the download controller.Cancel();
ตรวจสอบความคืบหน้าในการดาวน์โหลด
คุณสามารถแนบ Listener กับการดาวน์โหลดเพื่อตรวจสอบความคืบหน้าของการดาวน์โหลดได้
class MyListener : public firebase::storage::Listener { public: virtual void OnProgress(firebase::storage::Controller* controller) { // A progress event occurred } }; { // Start uploading a file MyEventListener my_listener; storage_ref.Child("images/mountains.jpg").GetFile(local_file, my_listener); }
จัดการข้อผิดพลาด
ข้อผิดพลาดอาจเกิดขึ้นได้จากหลายสาเหตุเมื่อดาวน์โหลด เช่น ไม่มีไฟล์ หรือผู้ใช้ไม่มีสิทธิ์เข้าถึงไฟล์ที่ต้องการ ดูข้อมูลเพิ่มเติมเกี่ยวกับข้อผิดพลาดได้ในส่วนจัดการข้อผิดพลาดของเอกสาร
ขั้นตอนถัดไป
นอกจากนี้ คุณยังรับและอัปเดตข้อมูลเมตาสำหรับไฟล์ที่จัดเก็บไว้ใน Cloud Storage ได้ด้วย