Uploading an Object Body

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

  • // Create a KiiObject in an application-scope bucket.
    KiiObject object = Kii.bucket("AppBucket").object();
    
    // Set key-value pairs.
    object.set("title", "MyVideo");
    object.set("fileSize", 10485760);
    
    // Specify a file to upload.
    File localFile = new File(Environment.getExternalStorageDirectory(),
            "sample.mp4");
    
    // Create an uploader.
    KiiUploader uploader = object.uploader(getApplicationContext(),
            localFile);
    
    try {
      // Start uploading.
      uploader.transfer(new KiiRTransferProgressCallback() {
        @Override
        public void onProgress(KiiRTransfer operator, long completedInBytes, long totalSizeinBytes) {
          float progress = (float)completedInBytes / (float)totalSizeinBytes * 100.0f;
        }
      });
    } catch (AlreadyStartedException e) {
      // The upload is already in progress.
    } catch (SuspendedException e) {
      // The upload has been suspended because of a network error or user interruption.
    } catch (TerminatedException e) {
      // The upload has been terminated because of the missing file or user interruption.
    } catch (StateStoreAccessException e) {
      // Failed to access the local storage.
    }
  • // Create a KiiObject in an application-scope bucket.
    KiiObject object = Kii.bucket("AppBucket").object();
    
    // Set key-value pairs.
    object.set("title", "MyVideo");
    object.set("fileSize", 10485760);
    
    // Specify a file to upload.
    File localFile = new File(Environment.getExternalStorageDirectory(),
            "sample.mp4");
    
    // Create an uploader.
    KiiUploader uploader = object.uploader(getApplicationContext(),
            localFile);
    
    // Start uploading.
    uploader.transferAsync(new KiiRTransferCallback() {
      @Override
      public void onStart(KiiRTransfer operator) {
      }
    
      @Override
      public void onProgress(KiiRTransfer operator, long completedInBytes, long totalSizeinBytes) {
        float progress = (float) completedInBytes / (float) totalSizeinBytes * 100.0f;
      }
    
      @Override
      public void onTransferCompleted(KiiRTransfer operator, Exception exception) {
        if (exception != null) {
          // 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.
  • Start uploading by calling the transfer() method.

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

The onProgress() method will be called as the upload progresses. The method 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 SuspendedException will be thrown. In this case, you can resume the upload (For the steps to resume, see Resuming an Upload).

You might need to get rutime permissions for accessing an external storage during the object body uploading. See Getting runtime permissions for the implementation details.