फायरबेस के लिए क्लाउड स्टोरेज आपको फायरबेस द्वारा प्रदान और प्रबंधित क्लाउड स्टोरेज बकेट से फ़ाइलों को जल्दी और आसानी से डाउनलोड करने की अनुमति देता है।
एक संदर्भ बनाएँ
किसी फ़ाइल को डाउनलोड करने के लिए, पहले उस फ़ाइल का क्लाउड स्टोरेज संदर्भ बनाएं जिसे आप डाउनलोड करना चाहते हैं।
आप अपने क्लाउड स्टोरेज बकेट के रूट में चाइल्ड पाथ जोड़कर एक संदर्भ बना सकते हैं, या आप क्लाउड स्टोरेज में किसी ऑब्जेक्ट को संदर्भित करने वाले मौजूदा gs://
या https://
यूआरएल से एक संदर्भ बना सकते हैं।
तीव्र
// 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")
उद्देश्य सी
// 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"];
फ़ाइलें डाउनलोड करें
एक बार आपके पास संदर्भ हो जाने पर, आप क्लाउड स्टोरेज से तीन तरीकों से फ़ाइलें डाउनलोड कर सकते हैं:
- मेमोरी में
NSData
पर डाउनलोड करें - डिवाइस पर फ़ाइल का प्रतिनिधित्व करने वाले
NSURL
पर डाउनलोड करें - फ़ाइल का ऑनलाइन प्रतिनिधित्व करने वाला एक
NSURL
उत्पन्न करें
स्मृति में डाउनलोड करें
dataWithMaxSize:completion:
विधि का उपयोग करके फ़ाइल को मेमोरी में NSData
ऑब्जेक्ट में डाउनलोड करें। किसी फ़ाइल को शीघ्रता से डाउनलोड करने का यह सबसे आसान तरीका है, लेकिन इसके लिए आपकी फ़ाइल की संपूर्ण सामग्री को मेमोरी में लोड करना होगा। यदि आप अपने ऐप की उपलब्ध मेमोरी से बड़ी फ़ाइल का अनुरोध करते हैं, तो आपका ऐप क्रैश हो जाएगा। मेमोरी समस्याओं से बचाने के लिए, अधिकतम आकार को उस चीज़ पर सेट करना सुनिश्चित करें जिसे आप जानते हैं कि आपका ऐप संभाल सकता है, या किसी अन्य डाउनलोड विधि का उपयोग करें।
तीव्र
// 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!) } }
उद्देश्य सी
// 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
लौटाता है जिसका उपयोग आप अपने डाउनलोड को प्रबंधित करने और अपलोड की स्थिति की निगरानी करने के लिए कर सकते हैं।
तीव्र
// 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 } }
उद्देश्य सी
// 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:
विधि का उपयोग कर सकते हैं और डाउनलोड कार्य का निरीक्षण कर सकते हैं। अधिक जानकारी के लिए डाउनलोड प्रबंधित करें देखें।
एक डाउनलोड यूआरएल जनरेट करें
यदि आपके पास पहले से ही यूआरएल पर आधारित डाउनलोड इंफ्रास्ट्रक्चर है, या आप साझा करने के लिए सिर्फ एक यूआरएल चाहते हैं, तो आप क्लाउड स्टोरेज संदर्भ पर downloadURLWithCompletion:
विधि को कॉल करके किसी फ़ाइल के लिए डाउनलोड यूआरएल प्राप्त कर सकते हैं।
तीव्र
// 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' } }
उद्देश्य सी
// 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 के साथ छवियाँ डाउनलोड करना
फायरबेसयूआई बॉयलरप्लेट कोड को खत्म करने और Google की सर्वोत्तम प्रथाओं को बढ़ावा देने के लिए सरल, अनुकूलन योग्य और उत्पादन-तैयार देशी मोबाइल बाइंडिंग प्रदान करता है। FirebaseUI का उपयोग करके आप SDWebImage के साथ हमारे एकीकरण का उपयोग करके क्लाउड स्टोरेज से छवियों को जल्दी और आसानी से डाउनलोड, कैश और प्रदर्शित कर सकते हैं।
सबसे पहले, अपने Podfile
में FirebaseUI जोड़ें:
pod 'FirebaseStorageUI'
फिर आप छवियों को सीधे क्लाउड स्टोरेज से UIImageView
में लोड कर सकते हैं:
तीव्र
// 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)
उद्देश्य सी
// 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];
डाउनलोड प्रबंधित करें
डाउनलोड शुरू करने के अलावा, आप रोकें, फिर से शुरू करें और cancel
विधियों का उपयोग करके डाउनलोड को रोक सकते हैं, pause
resume
और रद्द कर सकते हैं। ये विधियाँ pause
रोकती हैं, resume
और cancel
जिन्हें आप देख सकते हैं।
तीव्र
// 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()
उद्देश्य सी
// 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
मिलता है जिसका उपयोग पर्यवेक्षक को हटाने के लिए किया जा सकता है।
तीव्र
// Add a progress observer to a download task let observer = downloadTask.observe(.progress) { snapshot in // A progress event occurred }
उद्देश्य सी
// 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 | यह ईवेंट किसी भी समय क्लाउड स्टोरेज से डेटा डाउनलोड होने पर सक्रिय हो जाता है, और इसका उपयोग डाउनलोड प्रगति संकेतक को पॉप्युलेट करने के लिए किया जा सकता है। |
FIRStorageTaskStatusPause | यह ईवेंट किसी भी समय डाउनलोड रुकने पर सक्रिय हो जाता है, और अक्सर FIRStorageTaskStatusResume ईवेंट के संयोजन में उपयोग किया जाता है। |
FIRStorageTaskStatusSuccess | यह ईवेंट तब सक्रिय होता है जब कोई डाउनलोड सफलतापूर्वक पूरा हो जाता है। |
FIRStorageTaskStatusFailure | डाउनलोड विफल होने पर यह ईवेंट सक्रिय हो जाता है। विफलता का कारण निर्धारित करने के लिए त्रुटि का निरीक्षण करें। |
जब कोई घटना घटती है, तो FIRStorageTaskSnapshot
ऑब्जेक्ट वापस भेज दिया जाता है। यह स्नैपशॉट घटना घटित होने के समय कार्य का एक अपरिवर्तनीय दृश्य है। इस ऑब्जेक्ट में निम्नलिखित गुण हैं:
संपत्ति | प्रकार | विवरण |
---|---|---|
progress | NSProgress | एक NSProgress ऑब्जेक्ट जिसमें डाउनलोड की प्रगति शामिल है। |
error | NSError | डाउनलोड के दौरान हुई कोई त्रुटि, यदि कोई हो। |
metadata | FIRStorageMetadata | डाउनलोड पर nil . |
task | FIRStorageDownloadTask | यह कार्य एक स्नैपशॉट है, जिसका उपयोग कार्य को प्रबंधित करने ( pause , resume , cancel ) के लिए किया जा सकता है। |
reference | FIRStorageReference | यह कार्य जिस संदर्भ से आया है। |
आप पर्यवेक्षकों को व्यक्तिगत रूप से, स्थिति के आधार पर या उन सभी को हटाकर भी हटा सकते हैं।
तीव्र
// 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()
उद्देश्य सी
// 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
होने के बाद सभी पर्यवेक्षक हटा दिए जाते हैं।
त्रुटियाँ संभालें
ऐसे कई कारण हैं जिनकी वजह से डाउनलोड पर त्रुटियां हो सकती हैं, जिनमें फ़ाइल का मौजूदा न होना, या उपयोगकर्ता के पास वांछित फ़ाइल तक पहुंचने की अनुमति न होना शामिल है। त्रुटियों के बारे में अधिक जानकारी डॉक्स के हैंडल एरर्स अनुभाग में पाई जा सकती है।
पूर्ण उदाहरण
त्रुटि प्रबंधन के साथ स्थानीय फ़ाइल में डाउनलोड करने का एक पूरा उदाहरण नीचे दिखाया गया है:
तीव्र
// 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 } }
उद्देश्य सी
// 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; } } }];
आप क्लाउड स्टोरेज में संग्रहीत फ़ाइलों के लिए मेटाडेटा भी प्राप्त और अपडेट कर सकते हैं।