Apple প্ল্যাটফর্মে Cloud Storage ব্যবহার করে ফাইল ডাউনলোড করা

Cloud Storage for Firebase আপনাকে Firebase-এর মাধ্যমে প্রদান করা ও ম্যানেজ করা Cloud Storage বাকেট থেকে দ্রুত ও সহজে ফাইল ডাউনলোড করতে দেয়।

রেফারেন্স তৈরি করা

কোনও ফাইল ডাউনলোড করতে, প্রথমে আপনি যে ফাইল ডাউনলোড করতে চান সেটির জন্য একটি Cloud Storage রেফারেন্স তৈরি করুন।

আপনার Cloud Storage বাকেটের রুটে চাইল্ড পাথ যোগ করে আপনি রেফারেন্স তৈরি করতে পারেন অথবা Cloud Storage-এ কোনও অবজেক্টকে রেফারেন্স করছে এমন আগে থেকে থাকা gs:// বা https:// URL থেকে আপনি রেফারেন্স তৈরি করতে পারেন।

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: পদ্ধতি ব্যবহার করতে এবং ডাউনলোড টাস্ক পর্যবেক্ষণ করতে পারেন। আরও তথ্যের জন্য ডাউনলোড ম্যানেজ করুন বিকল্প দেখুন।

ডাউনলোড 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 ব্যবহার করে আপনি 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
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-এ স্টোর করা ফাইলের জন্য মেটাডেটা পেতে ও আপডেট করতে পারবেন।