目录

Setting Tags for a File

Use Cases

Enterprise Data Guard Kit allows apps to set tags for a file to facilitate identification and classification of files.

API Description

For details about these APIs, please refer to the API Reference.

API Description
setFileTag(path: string, level: SecurityLevel, tag: string, callback: AsyncCallback<void>): void Sets file tags using a callback.
setFileTag(path: string, level: SecurityLevel, tag: string): Promise<void> Sets file tags using a promise.
setFileCustomTag(path: string, tagList: Array<string>, callback: AsyncCallback<void>): void Sets custom tags for a file using a callback.
setFileCustomTag(path: string, tagList: Array<string>): Promise<void> Sets custom tags for a file using a promise.
unsetFileCustomTag(path: string, tagList: Array<string>, callback: AsyncCallback<void>): void Cancels custom tags for a file using a callback.
unsetFileCustomTag(path: string, tagList: Array<string>): Promise<void> Cancels custom tags for a file using a promise.

Development Procedure

  1. Import required modules.

    import { BusinessError } from '@kit.BasicServicesKit';
    import { fileGuard } from '@kit.EnterpriseDataGuardKit';
    import { hilog } from '@kit.PerformanceAnalysisKit';
  2. Initialize the guard object of FileGuard and call the setFileTag API to set file tags.

    • Set file tags using a callback.

      const TAG: string = 'FileGuard_FileTag';
      const DOMAIN: number = 0x0000;
      
      /**
       * Set file tags and return the result asynchronously using a callback.
       */
      function setFileTagCallback() {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let path: string = '/data/service/el2/test/test1.txt';
        let tag: string = 'test1';
        guard.setFileTag(path, fileGuard.SecurityLevel.EXTERNAL, tag, (err: BusinessError) => {
          if (err) {
            hilog.error(DOMAIN, TAG, `Failed to set file tag. Code: ${err.code}, message: ${err.message}.`);
            return;
          }
          hilog.info(DOMAIN, TAG, `Succeeded in setting file tag.`);
        });
      }
    • Set file tags using a promise.

      const TAG: string = 'FileGuard_FileTag';
      const DOMAIN: number = 0x0000;
      
      // ...
      /**
       * Set file tags and return the result asynchronously using a promise.
       */
      function setFileTagPromise() {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let path: string = '/data/service/el2/test/test2.txt';
        let tag: string = 'test2';
        guard.setFileTag(path, fileGuard.SecurityLevel.EXTERNAL, tag).then(() => {
          hilog.info(DOMAIN, TAG, `Succeeded in setting file tag.`);
        }).catch((err: BusinessError) => {
          hilog.error(DOMAIN, TAG, `Failed to set file tag. Code: ${err.code}, message: ${err.message}.`);
        });
      }
  3. Initialize the guard object of FileGuard and call the setFileCustomTag API to customize file tags.

    • Customize file tags using a callback.

      const TAG: string = 'FileGuard_FileTag';
      const DOMAIN: number = 0x0000;
      
      // ...
      /**
       * Customize file tags and return the result asynchronously using a callback.
       * @param accountId: user ID
       */
      function setFileCustomTagCallback(accountId: number) {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let path: string = `/data/service/el2/${accountId}/hmdfs/account/files/Docs/Documents/test1.txt`;
        let tagList: string[] = ['sensitive', 'confidential', 'public', 'general', 'special'];
        guard.setFileCustomTag(path, tagList, (err: BusinessError) => {
          if (err) {
            hilog.error(DOMAIN, TAG, `Failed to set file custom tag. Code: ${err.code}, message: ${err.message}.`);
          } else {
            hilog.info(DOMAIN, TAG, `Succeeded in setting file custom tag.`);
          }
        });
      }
    • Customize file tags using a promise.

      const TAG: string = 'FileGuard_FileTag';
      const DOMAIN: number = 0x0000;
      
      // ...
      /**
       * Customize file tags and return the result asynchronously using a promise.
       * @param accountId: user ID
       */
      function setFileCustomTagPromise(accountId: number) {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let path: string = `/data/service/el2/${accountId}/hmdfs/account/files/Docs/Documents/test2.txt`;
        let tagList: string[] = ['sensitive', 'confidential', 'public', 'general', 'special'];
        guard.setFileCustomTag(path, tagList).then(() => {
          hilog.info(DOMAIN, TAG, `Succeeded in setting file custom tag.`);
        }).catch((err: BusinessError) => {
          hilog.error(DOMAIN, TAG, `Failed to set file custom tag. Code: ${err.code}, message: ${err.message}.`);
        });
      }
  4. Initialize the guard object of FileGuard and call the unsetFileCustomTag API to unset customized file tags.

    • Unset customized file tags using a callback.

      const TAG: string = 'FileGuard_FileTag';
      const DOMAIN: number = 0x0000;
      
      // ...
      /**
       * Unset customized file tags and return the result asynchronously using a callback.
       * @param accountId: user ID
       */
      function unsetFileCustomTagCallback(accountId: number) {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let path: string = `/data/service/el2/${accountId}/hmdfs/account/files/Docs/Documents/test1.txt`;
        let tagList: string[] = ['sensitive', 'confidential', 'public', 'general', 'special'];
        guard.unsetFileCustomTag(path, tagList, (err: BusinessError) => {
          if (err) {
            hilog.error(DOMAIN, TAG, `Failed to unset file custom tag. Code: ${err.code}, message: ${err.message}.`);
          } else {
            hilog.info(DOMAIN, TAG, `Succeeded in unsetting file custom tag.`);
          }
        });
      }
    • Unset customized file tags using a promise.

      const TAG: string = 'FileGuard_FileTag';
      const DOMAIN: number = 0x0000;
      
      // ...
      /**
       * Unset customized file tags and return the result asynchronously using a promise.
       * @param accountId: user ID
       */
      function unsetFileCustomTagPromise(accountId: number) {
        let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
        let path: string = `/data/service/el2/${accountId}/hmdfs/account/files/Docs/Documents/test2.txt`;
        let tagList: string[] = ['sensitive', 'confidential', 'public', 'general', 'special'];
        guard.unsetFileCustomTag(path, tagList).then(() => {
          hilog.info(DOMAIN, TAG, `Succeeded in unsetting file custom tag.`);
        }).catch((err: BusinessError) => {
          hilog.error(DOMAIN, TAG, `Failed to unset file custom tag. Code: ${err.code}, message: ${err.message}.`);
        });
      }