Creating a Bucket

To store data in Kii Cloud, you first need to create a bucket.

Bucket name

You can assign any name to a bucket according to the following rules. The bucket name is used to reference the bucket later.

The naming rules for buckets are as below:

  • The following names are reserved and cannot be used.
    • users
    • devices
    • installations
    • internal
    • things
  • The bucket name cannot start with "_" (underscore).
  • The bucket name can be a string of 2 to 64 characters. Valid characters are alphanumeric, "_" (underscore), and "-" (hyphen).

  • You can use the same name for multiple buckets as far as each of the buckets with the same name is unique within the application scope, a group scope, and a user scope. These buckets are recognized individually.

Creating a bucket

You can create a bucket in any of the application scope, a group scope, and a user scope.

  • Application scope

    Unlike buckets in the other scopes, you do not need to specify an instance that a new bucket will belong to because a bucket in the application scope belongs to the application.

    Execute the static method bucket() of the Kii class to create a bucket.

    // Create a bucket in the application scope.
    KiiBucket appBucket = Kii.bucket("my_app");
    

    The application scope is accessible to all users, so the scope is useful for storing the application assets. If the data that can be represented in the JSON format (e.g., text data and application config), you can store them as key-value pairs of KiiObjects. If the data are binary (e.g., images and sounds), you can upload them as an object body.

    Be careful about security when using the application scope. As explained in Security, anyone can access the data in the application scope if they know the AppID and AppKey. Storing confidential data such as customer's personal information in the application scope is, therefore not safe. For storing such data, consider preparing a region that can be accessible only to the app administrator. You can achieve this by leveraging the server code and ACL modification (See Administrator for some implementation hints).

  • Group scope

    A bucket in a group scope stores data for the specific group.

    Get a KiiGroup instance of the group that you want to create a bucket for and execute the bucket() method of the instance to create a bucket.

    // Create a bucket in the group scope. Login is required.
    KiiBucket groupBucket = group.bucket("my_group");
    
  • User scope

    A bucket in a user scope stores data for the specific user.

    Get a KiiUser instance of the user that you want to create a bucket for and execute the bucket() method of the instance to create a bucket.

    // Create a bucket in the user scope. Login is required.
    KiiBucket userBucket = user.bucket("my_private");
    

Time when a bucket is created

Note that a bucket is created on Kii Cloud only when a KiiObject that belongs to the bucket is saved to Kii Cloud. For example, userBucket will be created when a KiiObject is created and saved to Kii Cloud with the save() method as follows:

  • KiiObject object = userBucket.object();
    try {
      object.save();
    } catch (IOException e) {
      // Handle the error.
    } catch (AppException e) {
      // Handle the error.
    }
  • KiiObject object = userBucket.object();
    object.save(new KiiObjectCallBack() {
      @Override
      public void onSaveCompleted(int token, KiiObject object, Exception exception) {
        if (exception != null) {
          // Handle the error.
          return;
        }
      }
    });

Hint for creating a bucket list

If you need to get a list of buckets, you need to implement the feature in your application.

Usually, buckets are created statically with their names in your code, so you should not need to get a list of buckets. If you do need to get the bucket list for some reasons (e.g., your code dynamically create buckets), your application can use a dedicated application scope bucket to store all bucket names. See Hint for creating a user list to learn how you can implement it.