Cloud Storage की मदद से, Apple प्लैटफ़ॉर्म पर फ़ाइलें डाउनलोड करना

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

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

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

अपने Cloud Storage बकेट के रूट में चाइल्ड पाथ जोड़कर, रेफ़रंस बनाया जा सकता है. इसके अलावा, Cloud Storage में मौजूद किसी ऑब्जेक्ट के बारे में बताने वाले मौजूदा gs:// या https:// यूआरएल से भी रेफ़रंस बनाया जा सकता है.

Swift

// Create a reference with an initial file path and name
let pathReference = storage.reference(withPath: "images/stars.jpg")

// Create a reference from a Google Cloud Storage URI
let gsReference = storage.reference(forURL: "gs://<your-firebase-storage-bucket>/images/stars.jpg")

// Create a reference from an HTTPS URL
// Note that in the URL, characters are URL escaped!
let httpsReference = storage.reference(forURL: "https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg")

Objective-C

// Create a reference with an initial file path and name
FIRStorageReference *pathReference = [storage referenceWithPath:@"images/stars.jpg"];

// Create a reference from a Google Cloud Storage URI
FIRStorageReference *gsReference = [storage referenceForURL:@"gs://<your-firebase-storage-bucket>/images/stars.jpg"];

// Create a reference from an HTTPS URL
// Note that in the URL, characters are URL escaped!
FIRStorageReference *httpsReference = [storage referenceForURL:@"https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg"];
  

फ़ाइलें डाउनलोड करें

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

  1. 'यादें' में NSData में डाउनलोड करें
  2. NSURL पर डाउनलोड करें, जो डिवाइस पर मौजूद फ़ाइल का प्रतिनिधित्व करता है
  3. फ़ाइल के बारे में ऑनलाइन जानकारी देने वाला NSURL जनरेट करें

मेमोरी में डाउनलोड करें

dataWithMaxSize:completion: तरीके का इस्तेमाल करके, मेमोरी में मौजूद NSData ऑब्जेक्ट में फ़ाइल डाउनलोड करें. यह किसी फ़ाइल को तुरंत डाउनलोड करने का सबसे आसान तरीका है. हालांकि, इसके लिए आपकी फ़ाइल का पूरा कॉन्टेंट, मेमोरी में लोड होना चाहिए. अगर आपने ऐप्लिकेशन के लिए उपलब्ध मेमोरी से बड़ी फ़ाइल का अनुरोध किया है, तो आपका ऐप्लिकेशन क्रैश हो जाएगा. मेमोरी से जुड़ी समस्याओं से बचने के लिए, डाउनलोड करने के किसी दूसरे तरीके का इस्तेमाल करें. ऐसा करने से, आपको पता है कि आपका ऐप्लिकेशन जिन चीज़ों को मैनेज कर सकता है उनके लिए ज़्यादा से ज़्यादा साइज़ सेट करें.

Swift

// Create a reference to the file you want to download
let islandRef = storageRef.child("images/island.jpg")

// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
islandRef.getData(maxSize: 1 * 1024 * 1024) { data, error in
  if let error = error {
    // Uh-oh, an error occurred!
  } else {
    // Data for "images/island.jpg" is returned
    let image = UIImage(data: data!)
  }
}
    

Objective-C

// Create a reference to the file you want to download
FIRStorageReference *islandRef = [storageRef child:@"images/island.jpg"];

// Download in memory with a maximum allowed size of 1MB (1 * 1024 * 1024 bytes)
[islandRef dataWithMaxSize:1 * 1024 * 1024 completion:^(NSData *data, NSError *error){
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Data for "images/island.jpg" is returned
    UIImage *islandImage = [UIImage imageWithData:data];
  }
}];
    

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

writeToFile:completion: तरीका, किसी फ़ाइल को सीधे स्थानीय डिवाइस पर डाउनलोड करता है. अगर आपके उपयोगकर्ता, ऑफ़लाइन रहते हुए या किसी दूसरे ऐप्लिकेशन में फ़ाइल को ऐक्सेस करना चाहते हैं, तो इसका इस्तेमाल करें. writeToFile:completion:, FIRStorageDownloadTask दिखाता है. इसका इस्तेमाल डाउनलोड को मैनेज करने और अपलोड की स्थिति को मॉनिटर करने के लिए किया जा सकता है.

Swift

// Create a reference to the file you want to download
let islandRef = storageRef.child("images/island.jpg")

// Create local filesystem URL
let localURL = URL(string: "path/to/image")!

// Download to the local filesystem
let downloadTask = islandRef.write(toFile: localURL) { url, error in
  if let error = error {
    // Uh-oh, an error occurred!
  } else {
    // Local file URL for "images/island.jpg" is returned
  }
}
    

Objective-C

// Create a reference to the file you want to download
FIRStorageReference *islandRef = [storageRef child:@"images/island.jpg"];

// Create local filesystem URL
NSURL *localURL = [NSURL URLWithString:@"path/to/image"];

// Download to the local filesystem
FIRStorageDownloadTask *downloadTask = [islandRef writeToFile:localURL completion:^(NSURL *URL, NSError *error){
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Local file URL for "images/island.jpg" is returned
  }
}];
    

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

डाउनलोड यूआरएल जनरेट करें

अगर आपके पास पहले से ही यूआरएल से जुड़ा डाउनलोड इन्फ़्रास्ट्रक्चर है या आपको सिर्फ़ यूआरएल शेयर करना है, तो किसी फ़ाइल के डाउनलोड का यूआरएल पाने के लिए, Cloud Storage रेफ़रंस पर downloadURLWithCompletion: तरीके का इस्तेमाल करें.

Swift

// Create a reference to the file you want to download
let starsRef = storageRef.child("images/stars.jpg")

// Fetch the download URL
starsRef.downloadURL { url, error in
  if let error = error {
    // Handle any errors
  } else {
    // Get the download URL for 'images/stars.jpg'
  }
}
    

Objective-C

// Create a reference to the file you want to download
FIRStorageReference *starsRef = [storageRef child:@"images/stars.jpg"];

// Fetch the download URL
[starsRef downloadURLWithCompletion:^(NSURL *URL, NSError *error){
  if (error != nil) {
    // Handle any errors
  } else {
    // Get the download URL for 'images/stars.jpg'
  }
}];
    

FirebaseUI की मदद से इमेज डाउनलोड की जा रही हैं

FirebaseUI, बॉयलरप्लेट कोड को हटाने और Google के सबसे सही तरीकों को बढ़ावा देने के लिए आसान, कस्टमाइज़ की जा सकने वाली, और प्रोडक्शन के लिए तैयार नेटिव मोबाइल बाइंडिंग उपलब्ध कराता है. FirebaseUI का इस्तेमाल करके SDWebImage के साथ हमारे इंटिग्रेशन का इस्तेमाल करके Cloud Storage से इमेज को तेज़ी और आसानी से डाउनलोड, कैश, और दिखाया जा सकता है.

सबसे पहले, अपने Podfile में FirebaseUI जोड़ें:

pod 'FirebaseStorageUI'

इसके बाद, इमेज को सीधे Cloud Storage से, UIImageView में लोड किया जा सकता है:

Swift

// Reference to an image file in Firebase Storage
let reference = storageRef.child("images/stars.jpg")

// UIImageView in your ViewController
let imageView: UIImageView = self.imageView

// Placeholder image
let placeholderImage = UIImage(named: "placeholder.jpg")

// Load the image using SDWebImage
imageView.sd_setImage(with: reference, placeholderImage: placeholderImage)
    

Objective-C

// Reference to an image file in Firebase Storage
FIRStorageReference *reference = [storageRef child:@"images/stars.jpg"];

// UIImageView in your ViewController
UIImageView *imageView = self.imageView;

// Placeholder image
UIImage *placeholderImage;

// Load the image using SDWebImage
[imageView sd_setImageWithStorageReference:reference placeholderImage:placeholderImage];
    

डाउनलोड मैनेज करें

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

Swift

// Start downloading a file
let downloadTask = storageRef.child("images/mountains.jpg").write(toFile: localFile)

// Pause the download
downloadTask.pause()

// Resume the download
downloadTask.resume()

// Cancel the download
downloadTask.cancel()
    

Objective-C

// Start downloading a file
FIRStorageDownloadTask *downloadTask = [[storageRef child:@"images/mountains.jpg"] writeToFile:localFile];

// Pause the download
[downloadTask pause];

// Resume the download
[downloadTask resume];

// Cancel the download
[downloadTask cancel];
    

डाउनलोड की प्रोग्रेस पर नज़र रखें

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

Swift

// Add a progress observer to a download task
let observer = downloadTask.observe(.progress) { snapshot in
  // A progress event occurred
}
    

Objective-C

// Add a progress observer to a download task
FIRStorageHandle observer = [downloadTask 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 डाउनलोड करने पर nil.
task FIRStorageDownloadTask यह टास्क का एक स्नैपशॉट है. इसका इस्तेमाल, टास्क (pause, resume, cancel) को मैनेज करने के लिए किया जा सकता है.
reference FIRStorageReference यह टास्क, किस रेफ़रंस से मिला है.

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

Swift

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

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

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

// Remove all observers
downloadTask.removeAllObservers()
    

Objective-C

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

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

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

// Remove all observers
[downloadTask removeAllObservers];
    

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

गड़बड़ियां ठीक करना

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

पूरा उदाहरण

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

Swift

// Create a reference to the file we want to download
let starsRef = storageRef.child("images/stars.jpg")

// Start the download (in this case writing to a file)
let downloadTask = storageRef.write(toFile: localURL)

// Observe changes in status
downloadTask.observe(.resume) { snapshot in
  // Download resumed, also fires when the download starts
}

downloadTask.observe(.pause) { snapshot in
  // Download paused
}

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

downloadTask.observe(.success) { snapshot in
  // Download completed successfully
}

// Errors only occur in the "Failure" case
downloadTask.observe(.failure) { snapshot in
  guard let errorCode = (snapshot.error as? NSError)?.code else {
    return
  }
  guard let error = StorageErrorCode(rawValue: errorCode) else {
    return
  }
  switch (error) {
  case .objectNotFound:
    // File doesn't exist
    break
  case .unauthorized:
    // User doesn't have permission to access file
    break
  case .cancelled:
    // User cancelled the download
    break

  /* ... */

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

Objective-C

// Create a reference to the file we want to download
FIRStorageReference *starsRef = [storageRef child:@"images/stars.jpg"];

// Start the download (in this case writing to a file)
FIRStorageDownloadTask *downloadTask = [storageRef writeToFile:localURL];

// Observe changes in status
[downloadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Download resumed, also fires when the download starts
}];

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

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

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

// Errors only occur in the "Failure" case
[downloadTask 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 में सेव की गई फ़ाइलों के लिए, मेटाडेटा को डाउनलोड और अपडेट किया जा सकता है.