Apple प्लैटफ़ॉर्म पर Cloud Storage का इस्तेमाल करके फ़ाइलें अपलोड करना

Firebase के लिए Cloud Storage आपको जल्दी और आसानी से Cloud Storage बकेट दिया गया और Firebase से मैनेज होता है.

रेफ़रंस बनाना

फ़ाइल अपलोड करने के लिए, सबसे पहले Cloud Storage के लिए रेफ़रंस बनाना Cloud Storage में उस जगह पर जाएं जहां आपको फ़ाइल अपलोड करनी है.

आप अपने चैनल के रूट में चाइल्ड पाथ को जोड़कर रेफ़रंस बना सकते हैं Cloud Storage बकेट:

Swift

// Create a root reference
let storageRef = storage.reference()

// Create a reference to "mountains.jpg"
let mountainsRef = storageRef.child("mountains.jpg")

// Create a reference to 'images/mountains.jpg'
let mountainImagesRef = storageRef.child("images/mountains.jpg")

// While the file names are the same, the references point to different files
mountainsRef.name == mountainImagesRef.name            // true
mountainsRef.fullPath == mountainImagesRef.fullPath    // false
    

Objective-C

// Create a root reference
FIRStorageReference *storageRef = [storage reference];

// Create a reference to "mountains.jpg"
FIRStorageReference *mountainsRef = [storageRef child:@"mountains.jpg"];

// Create a reference to 'images/mountains.jpg'
FIRStorageReference *mountainImagesRef = [storageRef child:@"images/mountains.jpg"];

// While the file names are the same, the references point to different files
[mountainsRef.name isEqualToString:mountainImagesRef.name];         // true
[mountainsRef.fullPath isEqualToString:mountainImagesRef.fullPath]; // false
  

आप अपने Cloud Storage बकेट. आपका संदर्भ किसी चाइल्ड यूआरएल पर ले जाना चाहिए.

फ़ाइलें अपलोड करें

पहचान फ़ाइल मिलने के बाद, आप Cloud Storage में फ़ाइलें अपलोड कर सकते हैं दो तरीके से:

  1. मेमोरी में मौजूद डेटा से अपलोड करें
  2. डिवाइस पर मौजूद फ़ाइल दिखाने वाले यूआरएल से अपलोड करें

मेमोरी में मौजूद डेटा से अपलोड करें

putData:metadata:completion: तरीका, किसी फ़ाइल को अपलोड करने का सबसे आसान तरीका है Cloud Storage में ले जाते हैं. putData:metadata:completion: एक NSData लेता है ऑब्जेक्ट करता है और FIRStorageUploadTask दिखाता है, जिसका इस्तेमाल अपलोड किया जा सकता है और इसकी स्थिति को मॉनिटर किया जा सकता है.

Swift

// Data in memory
let data = Data()

// Create a reference to the file you want to upload
let riversRef = storageRef.child("images/rivers.jpg")

// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putData(data, metadata: nil) { (metadata, error) in
  guard let metadata = metadata else {
    // Uh-oh, an error occurred!
    return
  }
  // Metadata contains file metadata such as size, content-type.
  let size = metadata.size
  // You can also access to download URL after upload.
  riversRef.downloadURL { (url, error) in
    guard let downloadURL = url else {
      // Uh-oh, an error occurred!
      return
    }
  }
}
    

Objective-C

// Data in memory
NSData *data = [NSData dataWithContentsOfFile:@"rivers.jpg"];

// Create a reference to the file you want to upload
FIRStorageReference *riversRef = [storageRef child:@"images/rivers.jpg"];

// Upload the file to the path "images/rivers.jpg"
FIRStorageUploadTask *uploadTask = [riversRef putData:data
                                             metadata:nil
                                           completion:^(FIRStorageMetadata *metadata,
                                                        NSError *error) {
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    int size = metadata.size;
    // You can also access to download URL after upload.
    [riversRef downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
      if (error != nil) {
        // Uh-oh, an error occurred!
      } else {
        NSURL *downloadURL = URL;
      }
    }];
  }
}];
  

किसी डिवाइस पर मौजूद फ़ाइल से अपलोड करें

आप कैमरा.putFile:metadata:completion: putFile:metadata:completion: एक NSURL लेता है और FIRStorageUploadTask का इस्तेमाल किया जा सकता है. इसका इस्तेमाल करके, अपने अपलोड को मैनेज किया जा सकता है. साथ ही, स्थिति.

Swift

// File located on disk
let localFile = URL(string: "path/to/image")!

// Create a reference to the file you want to upload
let riversRef = storageRef.child("images/rivers.jpg")

// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putFile(from: localFile, metadata: nil) { metadata, error in
  guard let metadata = metadata else {
    // Uh-oh, an error occurred!
    return
  }
  // Metadata contains file metadata such as size, content-type.
  let size = metadata.size
  // You can also access to download URL after upload.
  riversRef.downloadURL { (url, error) in
    guard let downloadURL = url else {
      // Uh-oh, an error occurred!
      return
    }
  }
}
    

Objective-C

// File located on disk
NSURL *localFile = [NSURL URLWithString:@"path/to/image"];

// Create a reference to the file you want to upload
FIRStorageReference *riversRef = [storageRef child:@"images/rivers.jpg"];

// Upload the file to the path "images/rivers.jpg"
FIRStorageUploadTask *uploadTask = [riversRef putFile:localFile metadata:nil completion:^(FIRStorageMetadata *metadata, NSError *error) {
  if (error != nil) {
    // Uh-oh, an error occurred!
  } else {
    // Metadata contains file metadata such as size, content-type, and download URL.
    int size = metadata.size;
    // You can also access to download URL after upload.
    [riversRef downloadURLWithCompletion:^(NSURL * _Nullable URL, NSError * _Nullable error) {
      if (error != nil) {
        // Uh-oh, an error occurred!
      } else {
        NSURL *downloadURL = URL;
      }
    }];
  }
}];
  

अगर आपको अपलोड किए गए डेटा को सही तरीके से मैनेज करना है, तो putData: या कन्वर्ज़न ट्रैकिंग का इस्तेमाल करने के बजाय, putFile: तरीकों का इस्तेमाल करें और अपलोड टास्क पर नज़र रखें पूरा होने वाला हैंडलर. ज़्यादा जानकारी के लिए, अपलोड मैनेज करें पर जाएं जानकारी.

फ़ाइल मेटाडेटा जोड़ें

फ़ाइलें अपलोड करते समय भी मेटाडेटा को शामिल किया जा सकता है. इस मेटाडेटा में फ़ाइल मेटाडेटा की सामान्य प्रॉपर्टी हैं. जैसे, name, size, और contentType (इसे आम तौर पर MIME टाइप कहा जाता है). putFile: तरीका NSURL फ़ाइल नाम के एक्सटेंशन से अपने-आप कॉन्टेंट टाइप का पता लगाता है, लेकिन अपने-आप पता लगाया गया टाइप बदलने के लिए, आपके पास contentType को तय करने का विकल्प होगा मेटाडेटा. contentType और Cloud Storage उपलब्ध नहीं कराने पर फ़ाइल एक्सटेंशन से डिफ़ॉल्ट अनुमान का अनुमान नहीं लगाया जा सकता. Cloud Storage application/octet-stream. ज़्यादा जानकारी के लिए, फ़ाइल मेटाडेटा का इस्तेमाल करना सेक्शन देखें.

Swift

// Create storage reference
let mountainsRef = storageRef.child("images/mountains.jpg")

// Create file metadata including the content type
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"

// Upload data and metadata
mountainsRef.putData(data, metadata: metadata)

// Upload file and metadata
mountainsRef.putFile(from: localFile, metadata: metadata)
    

Objective-C

// Create storage reference
FIRStorageReference *mountainsRef = [storageRef child:@"images/mountains.jpg"];

// Create file metadata including the content type
FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
metadata.contentType = @"image/jpeg";

// Upload data and metadata
FIRStorageUploadTask *uploadTask = [mountainsRef putData:data metadata:metadata];

// Upload file and metadata
uploadTask = [mountainsRef putFile:localFile metadata:metadata];
  

अपलोड प्रबंधित करें

अपलोड शुरू करने के अलावा, आप इनका इस्तेमाल करके अपलोड को रोक सकते हैं, फिर से शुरू कर सकते हैं या रद्द कर सकते हैं pause, resume, और cancel तरीके. इन तरीकों से pause, resume और cancel इवेंट. अपलोड को रद्द करने से, फ़ाइल अपलोड नहीं हो पाती है जिसमें यह बताया गया हो कि अपलोड रद्द कर दिया गया है.

Swift

// Start uploading a file
let uploadTask = storageRef.putFile(from: localFile)

// Pause the upload
uploadTask.pause()

// Resume the upload
uploadTask.resume()

// Cancel the upload
uploadTask.cancel()
    

Objective-C

// Start uploading a file
FIRStorageUploadTask *uploadTask = [storageRef putFile:localFile];

// Pause the upload
[uploadTask pause];

// Resume the upload
[uploadTask resume];

// Cancel the upload
[uploadTask cancel];
  

अपलोड की स्थिति पर नज़र रखें

निगरानी करने के लिए, FIRStorageUploadTask में ऑब्ज़र्वर अटैच किया जा सकता है अपलोड की प्रोग्रेस. ऑब्ज़र्वर जोड़ने से FIRStorageHandle मिलता है जिसका इस्तेमाल ऑब्ज़र्वर को हटाने के लिए किया जा सकता है.

Swift

// Add a progress observer to an upload task
let observer = uploadTask.observe(.progress) { snapshot in
  // A progress event occured
}
    

Objective-C

// Add a progress observer to an upload task
FIRStorageHandle observer = [uploadTask 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 अपलोड के दौरान, मेटाडेटा अपलोड किया जा रहा है. FIRTaskStatusSuccess इवेंट के बाद, इसमें अपलोड की गई फ़ाइल का मेटाडेटा शामिल होता है.
task FIRStorageUploadTask यह टास्क का एक स्नैपशॉट है. इसका इस्तेमाल, टास्क (pause, resume, cancel) को मैनेज करने के लिए किया जा सकता है.
reference FIRStorageReference यह टास्क, किस रेफ़रंस से मिला है.

ऑब्ज़र्वर को अलग-अलग, स्टेटस के हिसाब से या हटाकर भी हटाया जा सकता है उन सभी को.

Swift

// Create a task listener handle
let observer = uploadTask.observe(.progress) { snapshot in
  // A progress event occurred
}

// Remove an individual observer
uploadTask.removeObserver(withHandle: observer)

// Remove all observers of a particular status
uploadTask.removeAllObservers(for: .progress)

// Remove all observers
uploadTask.removeAllObservers()
    

Objective-C

// Create a task listener handle
FIRStorageHandle observer = [uploadTask observeStatus:FIRStorageTaskStatusProgress
                                              handler:^(FIRStorageTaskSnapshot *snapshot) {
                                                // A progress event occurred
                                              }];

// Remove an individual observer
[uploadTask removeObserverWithHandle:observer];

// Remove all observers of a particular status
[uploadTask removeAllObserversForStatus:FIRStorageTaskStatusProgress];

// Remove all observers
[uploadTask removeAllObservers];
  

मेमोरी लीक होने से रोकने के लिए, सभी ऑब्ज़र्वर को FIRStorageTaskStatusSuccess या FIRStorageTaskStatusFailure मिलता है.

गड़बड़ी ठीक करना

अपलोड करते समय गड़बड़ियां होने की कई वजहें हो सकती हैं. इनमें ये शामिल हैं डिवाइस में मौजूद फ़ाइल मौजूद न हो या उपयोगकर्ता को उसे अपलोड करने की अनुमति न हो पसंदीदा फ़ाइल चुनें. आपको यहां पर गड़बड़ियों के बारे में ज़्यादा जानकारी मिल सकती है: गड़बड़ियां ठीक करें सेक्शन में जाएं.

पूरा उदाहरण

प्रोग्रेस मॉनिटर करने और गड़बड़ियों को ठीक करने के तरीके के साथ अपलोड का पूरा उदाहरण नीचे दिखाया गया है:

Swift

// Local file you want to upload
let localFile = URL(string: "path/to/image")!

// Create the file metadata
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"

// Upload file and metadata to the object 'images/mountains.jpg'
let uploadTask = storageRef.putFile(from: localFile, metadata: metadata)

// Listen for state changes, errors, and completion of the upload.
uploadTask.observe(.resume) { snapshot in
  // Upload resumed, also fires when the upload starts
}

uploadTask.observe(.pause) { snapshot in
  // Upload paused
}

uploadTask.observe(.progress) { snapshot in
  // Upload reported progress
  let percentComplete = 100.0 * Double(snapshot.progress!.completedUnitCount)
    / Double(snapshot.progress!.totalUnitCount)
}

uploadTask.observe(.success) { snapshot in
  // Upload completed successfully
}

uploadTask.observe(.failure) { snapshot in
  if let error = snapshot.error as? NSError {
    switch (StorageErrorCode(rawValue: error.code)!) {
    case .objectNotFound:
      // File doesn't exist
      break
    case .unauthorized:
      // User doesn't have permission to access file
      break
    case .cancelled:
      // User canceled the upload
      break

    /* ... */

    case .unknown:
      // Unknown error occurred, inspect the server response
      break
    default:
      // A separate error occurred. This is a good place to retry the upload.
      break
    }
  }
}
    

Objective-C

// Local file you want to upload
NSURL *localFile = [NSURL URLWithString:@"path/to/image"];

// Create the file metadata
FIRStorageMetadata *metadata = [[FIRStorageMetadata alloc] init];
metadata.contentType = @"image/jpeg";

// Upload file and metadata to the object 'images/mountains.jpg'
FIRStorageUploadTask *uploadTask = [storageRef putFile:localFile metadata:metadata];

// Listen for state changes, errors, and completion of the upload.
[uploadTask observeStatus:FIRStorageTaskStatusResume handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Upload resumed, also fires when the upload starts
}];

[uploadTask observeStatus:FIRStorageTaskStatusPause handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Upload paused
}];

[uploadTask observeStatus:FIRStorageTaskStatusProgress handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Upload reported progress
  double percentComplete = 100.0 * (snapshot.progress.completedUnitCount) / (snapshot.progress.totalUnitCount);
}];

[uploadTask observeStatus:FIRStorageTaskStatusSuccess handler:^(FIRStorageTaskSnapshot *snapshot) {
  // Upload completed successfully
}];

// Errors only occur in the "Failure" case
[uploadTask 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 से.