ดาวน์โหลดไฟล์ด้วย 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