Cloud Storage for Firebase ช่วยให้คุณดาวน์โหลดไฟล์จากที่เก็บข้อมูล Cloud Storage ที่ Firebase จัดหาและจัดการได้อย่างรวดเร็วและง่ายดาย
สร้างการอ้างอิง
หากต้องการดาวน์โหลดไฟล์ ขั้นตอนแรกให้สร้างCloud Storageการอ้างอิง ไปยังไฟล์ที่ต้องการดาวน์โหลด
คุณสร้างการอ้างอิงได้โดยการต่อเส้นทางย่อยเข้ากับรูทของCloud Storage บัคเก็ต หรือจะสร้างการอ้างอิงจาก gs://
หรือ https://
URL ที่มีอยู่ซึ่งอ้างอิงออบเจ็กต์ใน 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 ได้ด้วย