ดาวน์โหลดไฟล์ด้วย 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 future = island_ref.GetBytes(byte_buffer, kMaxAllowedSize);

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

// 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 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 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 ได้ด้วย