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

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

  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();

ติดตามความคืบหน้าการดาวน์โหลด

คุณสามารถแนบผู้ฟังเพื่อดาวน์โหลดเพื่อติดตามความคืบหน้าของการดาวน์โหลด

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