Cloud Storage for Firebase আপনাকে ফায়ারবেস দ্বারা সরবরাহকৃত ও পরিচালিত Cloud Storage বাকেট থেকে দ্রুত এবং সহজে ফাইল ডাউনলোড করার সুযোগ দেয়।
একটি রেফারেন্স তৈরি করুন
ফাইল ডাউনলোড করতে, প্রথমে আপনি যে ফাইলটি ডাউনলোড করতে চান সেটির জন্য একটি Cloud Storage রেফারেন্স তৈরি করুন ।
আপনি আপনার Cloud Storage বাকেটের রুটে চাইল্ড পাথ যুক্ত করে একটি রেফারেন্স তৈরি করতে পারেন, অথবা Cloud Storage কোনো অবজেক্টকে রেফারেন্স করে এমন একটি বিদ্যমান gs:// বা https:// URL থেকেও একটি রেফারেন্স তৈরি করতে পারেন।
সুইফট
// 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"];
ফাইলগুলি ডাউনলোড করুন
একবার আপনার কাছে রেফারেন্স থাকলে, আপনি Cloud Storage থেকে তিনভাবে ফাইল ডাউনলোড করতে পারেন:
- মেমরিতে
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: মেথডটি ব্যবহার করতে পারেন এবং ডাউনলোড টাস্কটি পর্যবেক্ষণ করতে পারেন। আরও তথ্যের জন্য `Manage Downloads` দেখুন।
একটি ডাউনলোড ইউআরএল তৈরি করুন
আপনার যদি আগে থেকেই URL-ভিত্তিক ডাউনলোড পরিকাঠামো থাকে, অথবা আপনি শুধু শেয়ার করার জন্য একটি URL চান, তাহলে একটি Cloud Storage রেফারেন্সে downloadURLWithCompletion: মেথডটি কল করে ফাইলটির ডাউনলোড URL পেতে পারেন।
সুইফট
// 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 দিয়ে ছবি ডাউনলোড করা
ফায়ারবেসইউআই গতানুগতিক কোড দূর করতে এবং গুগলের সেরা অনুশীলনগুলিকে উৎসাহিত করতে সহজ, কাস্টমাইজযোগ্য এবং প্রোডাকশন-রেডি নেটিভ মোবাইল বাইন্ডিং প্রদান করে। ফায়ারবেসইউআই ব্যবহার করে আপনি আমাদের এসডিওয়েবইমেজ (SDWebImage) ইন্টিগ্রেশনের মাধ্যমে Cloud Storage থেকে দ্রুত ও সহজে ছবি ডাউনলোড, ক্যাশ এবং প্রদর্শন করতে পারেন।
প্রথমে, আপনার Podfile এ FirebaseUI যোগ করুন:
pod 'FirebaseStorageUI'
তারপর আপনি সরাসরি Cloud Storage থেকে একটি 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];
ডাউনলোডগুলি পরিচালনা করুন
ডাউনলোড শুরু করার পাশাপাশি, আপনি pause, resume, এবং 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 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 | এই কাজটি যে সূত্র থেকে নেওয়া হয়েছে। |
আপনি পর্যবেক্ষকদেরকে এককভাবে, অবস্থা অনুযায়ী, অথবা একসাথেও অপসারণ করতে পারেন।
সুইফট
// 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 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 সংঘটিত হওয়ার পর সমস্ত অবজারভারকে সরিয়ে দেওয়া হয়।
ত্রুটিগুলি পরিচালনা করুন
ডাউনলোড করার সময় বিভিন্ন কারণে ত্রুটি ঘটতে পারে, যার মধ্যে রয়েছে ফাইলটির অস্তিত্ব না থাকা, অথবা কাঙ্ক্ষিত ফাইলটি অ্যাক্সেস করার জন্য ব্যবহারকারীর অনুমতি না থাকা। ত্রুটি সম্পর্কে আরও তথ্য ডকুমেন্টেশনের ' Handle Errors' বিভাগে পাওয়া যাবে।
সম্পূর্ণ উদাহরণ
ত্রুটি পরিচালনা সহ স্থানীয় ফাইলে ডাউনলোড করার একটি সম্পূর্ণ উদাহরণ নিচে দেখানো হলো:
সুইফট
// 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; } } }];
আপনি Cloud Storage সংরক্ষিত ফাইলগুলির মেটাডেটাও পেতে এবং আপডেট করতে পারেন।