অ্যাপল প্ল্যাটফর্মে ক্লাউড স্টোরেজ সহ ফাইল ডাউনলোড করুন

Cloud Storage for Firebase আপনাকে 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 থেকে তিনটি উপায়ে ফাইল ডাউনলোড করতে পারবেন:

  1. মেমরিতে NSData তে ডাউনলোড করুন
  2. ডিভাইসে থাকা একটি ফাইলের প্রতিনিধিত্বকারী NSURL এ ডাউনলোড করুন
  3. অনলাইনে ফাইলটি উপস্থাপন করে একটি 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: পদ্ধতিটি ব্যবহার করতে পারেন এবং সমাপ্তি হ্যান্ডলার ব্যবহার করার পরিবর্তে ডাউনলোডের কাজটি পর্যবেক্ষণ করতে পারেন। আরও তথ্যের জন্য ডাউনলোড পরিচালনা করুন দেখুন।

একটি ডাউনলোড URL তৈরি করুন

যদি আপনার ইতিমধ্যেই 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 ব্যবহার করে ছবি ডাউনলোড করা

FirebaseUI বয়লারপ্লেট কোড বাদ দিতে এবং Google এর সেরা অনুশীলনগুলিকে প্রচার করতে সহজ, কাস্টমাইজযোগ্য এবং উৎপাদন-প্রস্তুত নেটিভ মোবাইল বাইন্ডিং প্রদান করে। 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 , and cancel পদ্ধতি ব্যবহার করে ডাউনলোডগুলি থামাতে, পুনরায় শুরু করতে এবং বাতিল করতে পারেন। এই পদ্ধতিগুলি আপনি যে ইভেন্টগুলি পর্যবেক্ষণ করতে পারেন সেগুলিকে 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 ঘটলে সমস্ত পর্যবেক্ষক সরিয়ে ফেলা হয়।

হ্যান্ডেল ত্রুটি

ডাউনলোড করার সময় ত্রুটি দেখা দেওয়ার অনেক কারণ থাকতে পারে, যার মধ্যে রয়েছে ফাইলটি বিদ্যমান না থাকা, অথবা ব্যবহারকারীর পছন্দসই ফাইলটি অ্যাক্সেস করার অনুমতি না থাকা। ত্রুটি সম্পর্কে আরও তথ্য ডক্সের "হ্যান্ডেল এররস" বিভাগে পাওয়া যাবে।

সম্পূর্ণ উদাহরণ

ত্রুটি পরিচালনা সহ স্থানীয় ফাইলে ডাউনলোড করার একটি সম্পূর্ণ উদাহরণ নীচে দেখানো হয়েছে:

সুইফট

// 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 সংরক্ষিত ফাইলগুলির মেটাডেটা পেতে এবং আপডেট করতে পারেন।