Cloud Storage for Firebase আপনাকে Firebase-এর প্রদান ও ম্যানেজ করা Cloud Storage বাকেট থেকে দ্রুত ও সহজে ফাইল ডাউনলোড করতে দেয়।
রেফারেন্স তৈরি করা
কোনও ফাইল ডাউনলোড করতে, প্রথমে আপনি যে ফাইল ডাউনলোড করতে চান সেটির Cloud Storage রেফারেন্স তৈরি করুন।
আপনার
Cloud Storage বাকেটের রুটে চাইল্ড পাথ যোগ করে আপনি রেফারেন্স তৈরি করতে পারেন অথবা আগে থেকে থাকা
gs:// বা https:// URL থেকে রেফারেন্স তৈরি করতে পারেন যা Cloud Storage-এ কোনও অবজেক্টকে রেফার করছে।
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 তিনটি উপায়ে ফাইল ডাউনলোড করতে পারবেন:
- মেমরিতে
NSDataডাউনলোড করুন - ডিভাইসে থাকা ফাইলের প্রতিনিধিত্বকারী
NSURL-এ ডাউনলোড করা - অনলাইনে ফাইলটি প্রতিনিধিত্ব করে এমন একটি
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-এর উপর ভিত্তি করে ডাউনলোড ইনফ্রাস্ট্রাকচার থাকে অথবা আপনি যদি শুধু
শেয়ার করার জন্য URL চান, তাহলে Cloud Storage রেফারেন্সে downloadURLWithCompletion: মেথড কল করে কোনও ফাইলের ডাউনলোড URL
পেতে পারেন।
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 NSString *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 NSString *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-এ সেভ করা ফাইলের জন্য আপনি মেটাডেটা পেতে ও আপডেট করতে পারবেন।