Apple प्लैटफ़ॉर्म पर Cloud Storage का इस्तेमाल करके फ़ाइलें अपलोड करना

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

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

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

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

Swift

// Create a root reference
let storageRef = storage.reference()

// Create a reference to "mountains.jpg"
let mountainsRef = storageRef.child("mountains.jpg")

// Create a reference to 'images/mountains.jpg'
let mountainImagesRef = storageRef.child("images/mountains.jpg")

// While the file names are the same, the references point to different files
mountainsRef.name == mountainImagesRef.name            // true
mountainsRef.fullPath == mountainImagesRef.fullPath    // false
    

Objective-C

// Create a root reference
FIRStorageReference *storageRef = [storage reference];

// Create a reference to "mountains.jpg"
FIRStorageReference *mountainsRef = [storageRef child:@"mountains.jpg"];

// Create a reference to 'images/mountains.jpg'
FIRStorageReference *mountainImagesRef = [storageRef child:@"images/mountains.jpg"];

// While the file names are the same, the references point to different files
[mountainsRef.name isEqualToString:mountainImagesRef.name];         // true
[mountainsRef.fullPath isEqualToString:mountainImagesRef.fullPath]; // false
  

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

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

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

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

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

putData:metadata:completion: तरीका, Cloud Storage में किसी फ़ाइल को अपलोड करने का सबसे आसान तरीका है. putData:metadata:completion: एक NSData ऑब्जेक्ट लेता है और FIRStorageUploadTask दिखाता है. इसका इस्तेमाल, अपलोड किए गए डेटा को मैनेज करने और उसकी स्थिति को मॉनिटर करने के लिए किया जा सकता है.

Swift

// Data in memory
let data = Data()

// Create a reference to the file you want to upload
let riversRef = storageRef.child("images/rivers.jpg")

// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putData(data, metadata: nil) { (metadata, error) in
  guard let metadata = metadata else {
    // Uh-oh, an error occurred!
    return
  }
  // Metadata contains file metadata such as size, content-type.
  let size = metadata.size
  // You can also access to download URL after upload.
  riversRef.downloadURL { (url, error) in
    guard let downloadURL = url else {
      // Uh-oh, an error occurred!
      return
    }
  }
}
    

Objective-C

// Data in memory
NSData *data = [NSData dataWithContentsOfFile:@"rivers.jpg"];

// Create a reference to the file you want to upload
FIRStorageReference *riversRef = [storageRef child:@"images/rivers.jpg"];

// Upload the file to the path "images/rivers.jpg"
FIRStorageUploadTask *uploadTask = [riversRef putData:data
                                             metadata:nil
                                           completion:^(FIRStorageMetadata *metadata,
                                                        NSError *error) {
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    int size = metadata.size;
    // You can also access to download URL after upload.
    [riversRef downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
      if (error != nil) {
        // Uh-oh, an error occurred!
      } else {
        NSURL *downloadURL = URL;
      }
    }];
  }
}];
  

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

putFile:metadata:completion: तरीके का इस्तेमाल करके, डिवाइसों पर लोकल फ़ाइलें अपलोड की जा सकती हैं. जैसे, कैमरे से ली गई फ़ोटो और वीडियो. putFile:metadata:completion:, NSURL का डेटा इकट्ठा करता है और FIRStorageUploadTask दिखाता है. इसका इस्तेमाल, अपलोड को मैनेज करने और उसकी स्थिति को मॉनिटर करने के लिए किया जा सकता है.

Swift

// File located on disk
let localFile = URL(string: "path/to/image")!

// Create a reference to the file you want to upload
let riversRef = storageRef.child("images/rivers.jpg")

// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putFile(from: localFile, metadata: nil) { metadata, error in
  guard let metadata = metadata else {
    // Uh-oh, an error occurred!
    return
  }
  // Metadata contains file metadata such as size, content-type.
  let size = metadata.size
  // You can also access to download URL after upload.
  riversRef.downloadURL { (url, error) in
    guard let downloadURL = url else {
      // Uh-oh, an error occurred!
      return
    }
  }
}
    

Objective-C

// File located on disk
NSURL *localFile = [NSURL URLWithString:@"path/to/image"];

// Create a reference to the file you want to upload
FIRStorageReference *riversRef = [storageRef child:@"images/rivers.jpg"];

// Upload the file to the path "images/rivers.jpg"
FIRStorageUploadTask *uploadTask = [riversRef putFile:localFile metadata:nil completion:^(FIRStorageMetadata *metadata, NSError *error) {
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    int size = metadata.size;
    // You can also access to download URL after upload.
    [riversRef downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
      if (error != nil) {
        // Uh-oh, an error occurred!
      } else {
        NSURL *downloadURL = URL;
      }
    }];
  }
}];
  

अगर आपको अपलोड किए गए डेटा को सही तरीके से मैनेज करना है, तो पूरा करने वाले हैंडलर के बजाय putData: या putFile: तरीके का इस्तेमाल करें और अपलोड किए गए टास्क पर नज़र रखें. ज़्यादा जानकारी के लिए, अपलोड मैनेज करें देखें.

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

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

Swift

// Create storage reference
let mountainsRef = storageRef.child("images/mountains.jpg")

// Create file metadata including the content type
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"

// Upload data and metadata
mountainsRef.putData(data, metadata: metadata)

// Upload file and metadata
mountainsRef.putFile(from: localFile, metadata: metadata)
    

Objective-C

// Create storage reference
FIRStorageReference *mountainsRef = [storageRef child:@"images/mountains.jpg"];

// Create file metadata including the content type
FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
metadata.contentType = @"image/jpeg";

// Upload data and metadata
FIRStorageUploadTask *uploadTask = [mountainsRef putData:data metadata:metadata];

// Upload file and metadata
uploadTask = [mountainsRef putFile:localFile metadata:metadata];
  

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

अपलोड शुरू करने के अलावा, आपके पास pause, resume, और cancel तरीके इस्तेमाल करके, अपलोड को रोकने, फिर से शुरू करने, और रद्द करने का विकल्प होता है. इन तरीकों से pause, resume, और cancel इवेंट को बढ़ावा मिलता है. अपलोड को रद्द करने से, अपलोड रद्द हो जाता है और आपको गड़बड़ी का यह मैसेज दिखता है.

Swift

// Start uploading a file
let uploadTask = storageRef.putFile(from: localFile)

// Pause the upload
uploadTask.pause()

// Resume the upload
uploadTask.resume()

// Cancel the upload
uploadTask.cancel()
    

Objective-C

// Start uploading a file
FIRStorageUploadTask *uploadTask = [storageRef putFile:localFile];

// Pause the upload
[uploadTask pause];

// Resume the upload
[uploadTask resume];

// Cancel the upload
[uploadTask cancel];
  

अपलोड की स्थिति पर नज़र रखें

अपलोड की प्रोग्रेस को मॉनिटर करने के लिए, FIRStorageUploadTask में ऑब्ज़र्वर अटैच किया जा सकता है. ऑब्ज़र्वर जोड़ने से एक FIRStorageHandle मिलता है, जिसका इस्तेमाल ऑब्ज़र्वर को हटाने के लिए किया जा सकता है.

Swift

// Add a progress observer to an upload task
let observer = uploadTask.observe(.progress) { snapshot in
  // A progress event occured
}
    

Objective-C

// Add a progress observer to an upload task
FIRStorageHandle observer = [uploadTask observeStatus:FIRStorageTaskStatusProgress
                                              handler:^(FIRStorageTaskSnapshot *snapshot) {
                                                // A progress event occurred
                                              }];
  

इन ऑब्ज़र्वर को किसी FIRStorageTaskStatus इवेंट में जोड़ा जा सकता है:

FIRStorageTaskStatus इवेंट आम तौर पर इस्तेमाल किए जाने वाले तरीके
FIRStorageTaskStatusResume यह इवेंट, टास्क के अपलोड होने पर शुरू होता है या फिर से अपलोड होने पर ट्रिगर होता है. आम तौर पर, इसका इस्तेमाल FIRStorageTaskStatusPause इवेंट के साथ किया जाता है.
FIRStorageTaskStatusProgress जब भी डेटा, Cloud Storage में अपलोड होता है, यह इवेंट फ़ायर होता है. साथ ही, इसका इस्तेमाल अपलोड की स्थिति दिखाने वाले इंडिकेटर को पॉप्युलेट करने के लिए किया जा सकता है.
FIRStorageTaskStatusPause अपलोड को किसी भी समय रोकने पर, यह इवेंट फ़ायर हो जाता है. साथ ही, आम तौर पर FIRStorageTaskStatusResume इवेंट के साथ इसका इस्तेमाल किया जाता है.
FIRStorageTaskStatusSuccess अपलोड होने के बाद ही यह इवेंट ट्रिगर हो जाता है.
FIRStorageTaskStatusFailure अपलोड न हो पाने पर, यह इवेंट ट्रिगर होता है. पुष्टि न हो पाने की वजह जानने के लिए, गड़बड़ी की जांच करें.

कोई इवेंट होने पर, FIRStorageTaskSnapshot ऑब्जेक्ट को वापस पास किया जाता है. यह स्नैपशॉट, इवेंट के समय टास्क का ऐसा व्यू होता है जिसमें बदलाव नहीं किया जा सकता. इस ऑब्जेक्ट में ये प्रॉपर्टी शामिल हैं:

प्रॉपर्टी टाइप जानकारी
progress NSProgress एक NSProgress ऑब्जेक्ट, जिसमें अपलोड की प्रोग्रेस की जानकारी होती है.
error NSError अगर अपलोड करते समय कोई गड़बड़ी हुई हो.
metadata FIRStorageMetadata अपलोड के दौरान, मेटाडेटा अपलोड किया जा रहा है. FIRTaskStatusSuccess इवेंट के बाद, इसमें अपलोड की गई फ़ाइल का मेटाडेटा शामिल होता है.
task FIRStorageUploadTask यह टास्क का एक स्नैपशॉट है. इसका इस्तेमाल, टास्क (pause, resume, cancel) को मैनेज करने के लिए किया जा सकता है.
reference FIRStorageReference यह टास्क, किस रेफ़रंस से मिला है.

ऑब्ज़र्वर को अलग-अलग, स्टेटस के हिसाब से या सभी को हटाकर भी हटाया जा सकता है.

Swift

// Create a task listener handle
let observer = uploadTask.observe(.progress) { snapshot in
  // A progress event occurred
}

// Remove an individual observer
uploadTask.removeObserver(withHandle: observer)

// Remove all observers of a particular status
uploadTask.removeAllObservers(for: .progress)

// Remove all observers
uploadTask.removeAllObservers()
    

Objective-C

// Create a task listener handle
FIRStorageHandle observer = [uploadTask observeStatus:FIRStorageTaskStatusProgress
                                              handler:^(FIRStorageTaskSnapshot *snapshot) {
                                                // A progress event occurred
                                              }];

// Remove an individual observer
[uploadTask removeObserverWithHandle:observer];

// Remove all observers of a particular status
[uploadTask removeAllObserversForStatus:FIRStorageTaskStatusProgress];

// Remove all observers
[uploadTask removeAllObservers];
  

मेमोरी लीक होने से रोकने के लिए, FIRStorageTaskStatusSuccess या FIRStorageTaskStatusFailure होने के बाद, सभी ऑब्ज़र्वर हटा दिए जाते हैं.

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

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

पूरा उदाहरण

प्रोग्रेस मॉनिटर करने और गड़बड़ियों को ठीक करने के तरीके के साथ अपलोड का पूरा उदाहरण नीचे दिया गया है:

Swift

// Local file you want to upload
let localFile = URL(string: "path/to/image")!

// Create the file metadata
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"

// Upload file and metadata to the object 'images/mountains.jpg'
let uploadTask = storageRef.putFile(from: localFile, metadata: metadata)

// Listen for state changes, errors, and completion of the upload.
uploadTask.observe(.resume) { snapshot in
  // Upload resumed, also fires when the upload starts
}

uploadTask.observe(.pause) { snapshot in
  // Upload paused
}

uploadTask.observe(.progress) { snapshot in
  // Upload reported progress
  let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
    / Double(snapshot.progress!.totalUnitCount)
}

uploadTask.observe(.success) { snapshot in
  // Upload completed successfully
}

uploadTask.observe(.failure) { snapshot in
  if let error = snapshot.error as? NSError {
    switch (StorageErrorCode(rawValue: error.code)!) {
    case .objectNotFound:
      // File doesn't exist
      break
    case .unauthorized:
      // User doesn't have permission to access file
      break
    case .cancelled:
      // User canceled the upload
      break

    /* ... */

    case .unknown:
      // Unknown error occurred, inspect the server response
      break
    default:
      // A separate error occurred. This is a good place to retry the upload.
      break
    }
  }
}
    

Objective-C

// Local file you want to upload
NSURL *localFile = [NSURL URLWithString:@"path/to/image"];

// Create the file metadata
FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
metadata.contentType = @"image/jpeg";

// Upload file and metadata to the object 'images/mountains.jpg'
FIRStorageUploadTask *uploadTask = [storageRef putFile:localFile metadata:metadata];

// Listen for state changes, errors, and completion of the upload.
[uploadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Upload resumed, also fires when the upload starts
}];

[uploadTask observeStatus:FIRStorageTaskStatusPause handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Upload paused
}];

[uploadTask observeStatus:FIRStorageTaskStatusProgress handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Upload reported progress
  double percentComplete = 100.0 * (snapshot.progress.completedUnitCount) / (snapshot.progress.totalUnitCount);
}];

[uploadTask observeStatus:FIRStorageTaskStatusSuccess handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Upload completed successfully
}];

// Errors only occur in the "Failure" case
[uploadTask observeStatus:FIRStorageTaskStatusFailure handler:^(FIRStorageTaskSnapshot *snapshot) {
  if (snapshot.error != nil) {
    switch (snapshot.error.code) {
      case FIRStorageErrorCodeObjectNotFound:
        // File doesn't exist
        break;

      case FIRStorageErrorCodeUnauthorized:
        // User doesn't have permission to access file
        break;

      case FIRStorageErrorCodeCancelled:
        // User canceled the upload
        break;

      /* ... */

      case FIRStorageErrorCodeUnknown:
        // Unknown error occurred, inspect the server response
        break;
    }
  }
}];
  

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