目录

Setting Metadata of a Cloud File

File metadata includes common attributes such as the cloud file name, size, and type, as well as custom file attributes.

You can set custom file attributes after saving a file on the cloud.

Constraints

This feature supports phones and tablets by default. Starting from version 5.1.0(18), support for wearables is added. From version 5.1.1(19), TVs are also supported. From version 6.1.0(23), PCs/2-in-1 devices are supported as well.

Prerequisites

Procedure

  1. Import required modules.

    import { cloudStorage } from '@kit.CloudFoundationKit';
    // ...
    import { BusinessError } from '@kit.BasicServicesKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. Call StorageBucket.setMetadata to set metadata of a cloud file.

    let bucket: cloudStorage.StorageBucket = cloudStorage.bucket();
    hilog.info(0x0000, 'Storage', `promise setMetadata  cloudPath: ${UI.uploadFileName}`);
    bucket.setMetadata(UI.uploadFileName, {
      customMetadata: {
        key1: 'value1',
        key2: 'value2'
      }
    }).then((result: Object) => {
      hilog.info(0x0000, 'Storage', `Succeeded in setting Metadata  ${JSON.stringify(result)}`);
    }).catch((err: BusinessError) => {
      hilog.error(0x0000, 'Storage', `Failed to set Metadata code: ${err.code}, message: ${err.message}`);
    });