Uploading an Object Body

The following sample code shows how to upload an object body with the resumable transfer.

Swift 3:

  • // Create a KiiObject in a user-scope bucket.
    let bucket = KiiUser.current()!.bucket(withName: "MyBucket")
    let object = bucket.createObject()
    
    // Set key-value pairs.
    object.setObject("myVideo", forKey: "title")
    object.setObject(NSNumber(value: 10485760 as Int), forKey: "fileSize")
    
    // Specify a file to upload.
    let targetDirectory : NSString = (NSHomeDirectory() as NSString).appendingPathComponent("Documents") as NSString
    let sourceFilePath = targetDirectory.appendingPathComponent("sample.mp4")
    
    // Create an uploader.
    let uploader = object.uploader(sourceFilePath)
    
    // Create a progress block.
    let progress : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in
      let info = transferObject.info()
      print("Progress : \(Float(info.completedSizeInBytes()/info.totalSizeInBytes()))")
    }
    
    do{
      // Start uploading.
      try uploader.transfer(progressBlock: progress)
    } catch let error as NSError {
      // Handle the error.
      return
    }
  • // Create a KiiObject in a user-scope bucket.
    let bucket = KiiUser.current()!.bucket(withName: "MyBucket")
    let object = bucket.createObject()
    
    // Set key-value pairs.
    object.setObject("myVideo", forKey: "title")
    object.setObject(NSNumber(value: 10485760 as Int), forKey: "fileSize")
    
    // Specify a file to upload.
    let targetDirectory : NSString = (NSHomeDirectory() as NSString).appendingPathComponent("Documents") as NSString
    let sourceFilePath = targetDirectory.appendingPathComponent("sample.mp4")
    
    // Create an uploader.
    let uploader = object.uploader(sourceFilePath)
    
    // Create a progress block.
    let progress : KiiRTransferBlock = { (transferObject : KiiRTransfer, error ) in
      let info = transferObject.info()
      print("Progress : \(Float(info.completedSizeInBytes()/info.totalSizeInBytes()))")
    }
    
    // Start uploading.
    uploader.transfer(progressBlock: progress, andCompletionBlock: { (transferObject : KiiRTransfer, error ) in
      if error != nil {
        // Handle the error.
        return
      }
    })

Objective-C:

  • // Create a KiiObject in a user-scope bucket.
    KiiBucket *bucket = [[KiiUser currentUser] bucketWithName:@"MyBucket"];
    KiiObject *object = [bucket createObject];
    
    // Set key-value pairs.
    [object setObject:@"MyVideo"
               forKey:@"title"];
    [object setObject:[NSNumber numberWithInt:10485760]
               forKey:@"fileSize"];
    
    // Specify a file to upload.
    NSString *targetDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *sourceFilePath = [targetDirectory stringByAppendingPathComponent:@"sample.mp4"];
    
    // Create an uploader.
    KiiUploader *uploader = [object uploader:sourceFilePath];
    
    // Create a progress block.
    KiiRTransferBlock progress = ^(id <KiiRTransfer> transferObject, NSError *retError) {
      KiiRTransferInfo *info = [transferObject info];
      NSLog(@"Progress : %f", (float) [info completedSizeInBytes] / [info totalSizeInBytes]);
    };
    
    // Start uploading.
    NSError *error = nil;
    [uploader transferWithProgressBlock:progress
                               andError:&error];
    if (error != nil) {
      // Handle the error.
      return;
    }
  • // Create a KiiObject in a user-scope bucket.
    KiiBucket *bucket = [[KiiUser currentUser] bucketWithName:@"MyBucket"];
    KiiObject *object = [bucket createObject];
    
    // Set key-value pairs.
    [object setObject:@"MyVideo"
               forKey:@"title"];
    [object setObject:[NSNumber numberWithInt:10485760]
               forKey:@"fileSize"];
    
    // Specify a file to upload.
    NSString *targetDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *sourceFilePath = [targetDirectory stringByAppendingPathComponent:@"sample.mp4"];
    
    // Create an uploader.
    KiiUploader *uploader = [object uploader:sourceFilePath];
    
    // Create a progress block.
    KiiRTransferBlock progress = ^(id <KiiRTransfer> transferObject, NSError *retError) {
      KiiRTransferInfo *info = [transferObject info];
      NSLog(@"Progress : %f", (float) [info completedSizeInBytes] / [info totalSizeInBytes]);
    };
    
    // Start uploading.
    [uploader transferWithProgressBlock:progress
                     andCompletionBlock:^(id<KiiRTransfer> transferObject, NSError *error) {
      if (error != nil) {
        // Handle the error.
        return;
      }
    }];

Here is what is happening in the sample code:

  • Set key-value pairs (e.g., file name, file size, and the availability of an object body) in a KiiObject as needed.
  • Create a reference to the target file (sample.mp4).
  • Create a KiiUploader instance by calling the uploader(_:) method with the target file reference.
  • Define a progress block.
  • Start uploading by calling the transfer(progressBlock:andCompletionBlock:_:) method.

If the KiiObject has an object body, uploading a new file will replace the existing object body.

For a resumable upload, the save(_:) method is automatically executed in case the method is not executed prior to calling the transfer(progressBlock:andCompletionBlock:_:) method.

Note that the progress block will not be called if you execute the blocking transfer(progressBlock:_:) method from the main thread.

The progress block will be called as the upload progresses. The block can reach 100% on the first call if the data size is small.

If the upload is interrupted for some reasons, it will be suspended, and an error will be thrown. In this case, you can resume the upload (For the steps to resume, see Resuming an Upload).