ดาวน์โหลดไฟล์ด้วย Cloud Storage สำหรับ C++

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 วิธีดังนี้

  1. ดาวน์โหลดลงในบัฟเฟอร์ในหน่วยความจำ
  2. ดาวน์โหลดไปยังเส้นทางที่เฉพาะเจาะจงในอุปกรณ์
  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ได้ด้วย