C++ के लिए, Cloud Storage वाली फ़ाइलें अपलोड करें

'Firebase के लिए Cloud Storage' की मदद से, फ़ाइलों को तेज़ी और आसानी से Cloud Storage बकेट में अपलोड किया जा सकता है. यह बकेट, Firebase से मैनेज की जाती है और दी जाती है.

रेफ़रंस बनाना

फ़ाइल अपलोड करने के लिए, पहले Cloud Storage में उस जगह के लिए Cloud Storage रेफ़रंस बनाएं जहां आपको फ़ाइल अपलोड करनी है.

अपने Cloud Storage बकेट के रूट में चाइल्ड पाथ जोड़कर, रेफ़रंस बनाया जा सकता है:

// Create a root reference
StorageReference storage_ref = storage->GetReference();

// Create a reference to "mountains.jpg"
StorageReference mountains_ref = storage_ref.Child("mountains.jpg");

// Create a reference to 'images/mountains.jpg'
StorageReference mountain_images_ref = storage_ref.Child("images/mountains.jpg");

// While the file names are the same, the references point to different files
mountains_ref.name() == mountain_images_ref.name();           // true
mountains_ref.full_path() == mountain_images_ref.full_path(); // false

अपने Cloud Storage बकेट के रूट के रेफ़रंस के साथ, डेटा अपलोड नहीं किया जा सकता. आपका संदर्भ किसी चाइल्ड यूआरएल पर ले जाना चाहिए.

फ़ाइलें अपलोड करें

रेफ़रंस फ़ाइल मिलने के बाद, Cloud Storage में दो तरीकों से फ़ाइलें अपलोड की जा सकती हैं:

  1. मेमोरी में बाइट बफ़र से अपलोड करें
  2. डिवाइस पर मौजूद फ़ाइल दिखाने वाले फ़ाइल पाथ से अपलोड करें

मेमोरी में मौजूद डेटा से अपलोड करें

PutData() तरीका, किसी फ़ाइल को Cloud Storage में अपलोड करने का सबसे आसान तरीका है. PutData() एक बाइट बफ़र लेता है और Future<Metadata> रिटर्न करता है, जिसमें फ़्यूचर के पूरा होने पर फ़ाइल के बारे में जानकारी होगी. अपलोड को मैनेज करने और उसकी स्थिति को मॉनिटर करने के लिए, Controller का इस्तेमाल किया जा सकता है.

// Data in memory
const size_t kByteBufferSize = ...
uint8_t byte_buffer[kByteBufferSize] = { ... };

// Create a reference to the file you want to upload
StorageReference rivers_ref = storage_ref.Child("images/rivers.jpg");

// Upload the file to the path "images/rivers.jpg"
Future future = rivers_ref.PutBytes(byte_buffer, kByteBufferSize);

इस दौरान अनुरोध किया गया था, लेकिन हमें फ़ाइल अपलोड होने से पहले आने वाले समय के पूरा होने का इंतज़ार करना होगा. आम तौर पर, गेम लूप में चलते हैं और दूसरे ऐप्लिकेशन के मुकाबले कम कॉलबैक होते हैं. इसलिए, आम तौर पर गेम पूरा होने के लिए पोल किया जाता है.

if (future.status() != firebase::kFutureStatusPending) {
  if (future.status() != firebase::kFutureStatusComplete) {
    LogMessage("ERROR: GetData() returned an invalid future.");
    // Handle the error...
  } else if (future.Error() != firebase::storage::kErrorNone) {
    LogMessage("ERROR: GetData() returned error %d: %s", future.Error(),
               future.error_message());
    // Handle the error...
    }
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    Metadata* metadata = future.Result();
    std::string download_url = metadata->download_url();
  }
}

किसी डिवाइस पर मौजूद फ़ाइल से अपलोड करें

PutFile() तरीके का इस्तेमाल करके, डिवाइसों पर लोकल फ़ाइलें अपलोड की जा सकती हैं. जैसे, कैमरे से ली गई फ़ोटो और वीडियो. PutFile(), फ़ाइल के पाथ को दिखाने के लिए, std::string लेता है और एक Future<Metadata> दिखाता है. इसमें, फ़्यूचर के पूरा होने पर फ़ाइल की जानकारी होगी. अपलोड को मैनेज करने और उसकी स्थिति को मॉनिटर करने के लिए, Controller का इस्तेमाल किया जा सकता है.

// File located on disk
std::string local_file = ...

// Create a reference to the file you want to upload
StorageReference rivers_ref = storage_ref.Child("images/rivers.jpg");

// Upload the file to the path "images/rivers.jpg"
Future future = rivers_ref.PutFile(localFile);

// Wait for Future to complete...

if (future.Error() != firebase::storage::kErrorNone) {
  // Uh-oh, an error occurred!
} else {
  // Metadata contains file metadata such as size, content-type, and download URL.
  Metadata* metadata = future.Result();
  std::string download_url = metadata->download_url();
}

अगर आपको अपलोड को सही तरीके से मैनेज करना है, तो PutFile() या PutBytes() तरीकों में Controller दें. इससे आपको अपलोड की जारी कार्रवाई पर नज़र रखने के लिए कंट्रोलर का इस्तेमाल करने की अनुमति मिलती है. ज़्यादा जानकारी के लिए, अपलोड मैनेज करें देखें.

फ़ाइल मेटाडेटा जोड़ें

फ़ाइलें अपलोड करते समय भी मेटाडेटा को शामिल किया जा सकता है. इस मेटाडेटा में सामान्य फ़ाइल मेटाडेटा प्रॉपर्टी होती हैं, जैसे कि name, size, और content_type (इसे आम तौर पर MIME टाइप कहा जाता है). PutFile() तरीका, फ़ाइल नाम के एक्सटेंशन से कॉन्टेंट टाइप का अपने-आप पता लगाता है. हालांकि, मेटाडेटा में content_type बताकर, अपने-आप पहचाने गए टाइप को बदला जा सकता है. अगर आपने content_type नहीं दिया है और Cloud Storage को फ़ाइल एक्सटेंशन से डिफ़ॉल्ट तौर पर अनुमान नहीं लगाया जा सकता है, तो Cloud Storage, application/octet-stream का इस्तेमाल करता है. फ़ाइल के मेटाडेटा के बारे में ज़्यादा जानने के लिए, फ़ाइल मेटाडेटा का इस्तेमाल करें सेक्शन देखें.

// Create storage reference
StorageReference mountains_ref = storage_ref.Child("images/mountains.jpg");

// Create file metadata including the content type
StorageMetadata metadata;
metadata.set_content_type("image/jpeg");

// Upload data and metadata
mountains_ref.PutBytes(data, metadata);

// Upload file and metadata
mountains_ref.PutFile(local_file, metadata);

अपलोड प्रबंधित करें

अपलोड शुरू करने के अलावा, Controller पर Pause(), Resume(), और Cancel() तरीकों का इस्तेमाल करके, अपलोड को रोका, फिर से शुरू, और रद्द किया जा सकता है. हालांकि, PutBytes() या PutFile() तरीकों का इस्तेमाल किया जा सकता है.

// Start uploading a file
firebase::storage::Controller controller;
storage_ref.Child("images/mountains.jpg").PutFile(local_file, nullptr, &controller);

// Pause the upload
controller.Pause();

// Resume the upload
controller.Resume();

// Cancel the upload
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").PutFile(local_file, my_listener);
}

गड़बड़ी ठीक करना

अपलोड करते समय गड़बड़ियां होने की कई वजहें हो सकती हैं. जैसे, डिवाइस पर मौजूद फ़ाइल का मौजूद न होना या उपयोगकर्ता को अपनी पसंद की फ़ाइल अपलोड करने की अनुमति न होना. आपको दस्तावेज़ों के गड़बड़ियां मैनेज करना सेक्शन में, गड़बड़ियों के बारे में ज़्यादा जानकारी मिल सकती है.

अगले चरण

अब जब आपने फ़ाइलें अपलोड कर ली हैं, तो आइए अब Cloud Storage से उन्हें डाउनलोड करने का तरीका जानें.