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

Firebase-এর জন্য ক্লাউড স্টোরেজ আপনাকে Firebase দ্বারা প্রদত্ত এবং পরিচালিত ক্লাউড স্টোরেজ বাকেট থেকে দ্রুত এবং সহজে ফাইল ডাউনলোড করতে দেয়।

একটি রেফারেন্স তৈরি করুন

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

আপনি আপনার ক্লাউড স্টোরেজ বাকেটের রুটে চাইল্ড পাথ যুক্ত করে একটি রেফারেন্স তৈরি করতে পারেন, অথবা আপনি ক্লাউড স্টোরেজের একটি অবজেক্টকে উল্লেখ করে বিদ্যমান 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"];
  

ফাইল ডাউনলোড করুন

একবার আপনার কাছে একটি রেফারেন্স থাকলে, আপনি তিনটি উপায়ে ক্লাউড স্টোরেজ থেকে ফাইলগুলি ডাউনলোড করতে পারেন:

  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 শেয়ার করতে চান, তাহলে আপনি ক্লাউড স্টোরেজ রেফারেন্সে 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- এর সাথে আমাদের ইন্টিগ্রেশন ব্যবহার করে ক্লাউড স্টোরেজ থেকে ছবিগুলি দ্রুত এবং সহজেই ডাউনলোড করতে, ক্যাশে করতে এবং প্রদর্শন করতে পারেন৷

প্রথমে, আপনার 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 s-এ পর্যবেক্ষকদের সংযুক্ত করতে পারেন। একটি পর্যবেক্ষক যোগ করা একটি 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;
    }
  }
}];
    

এছাড়াও আপনি ক্লাউড স্টোরেজে সংরক্ষিত ফাইলগুলির জন্য মেটাডেটা পেতে এবং আপডেট করতে পারেন৷