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

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

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

फ़ाइल डाउनलोड करने के लिए, सबसे पहले 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 जनरेट करें

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

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

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: का इस्तेमाल करें पूरा करने वाले हैंडलर का इस्तेमाल करने के बजाय, डाउनलोड टास्क पर नज़र रखें. ज़्यादा जानकारी के लिए, डाउनलोड मैनेज करना देखें.

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

अगर आपके पास पहले से ही यूआरएल के हिसाब से डाउनलोड इन्फ़्रास्ट्रक्चर है या आपको कोई URL है, तो आप फ़ाइल के डाउनलोड URL को 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 का इस्तेमाल करके, ये काम किए जा सकते हैं तेज़ी और आसानी से डाउनलोड, कैश, और डिसप्ले इमेज Cloud Storage से बाहर निकालना है, जो SDWebImage.

सबसे पहले, अपने 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 में सेव की गई फ़ाइलों के लिए.