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

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

पहचान फ़ाइल बनाना

कोई फ़ाइल अपलोड करने के लिए, सबसे पहले उस जगह का 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: में फ़ाइल अपलोड करने का सबसे आसान तरीका 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
NSString *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
NSString *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 से उन्हें डाउनलोड कैसे करें.