openharmony 鸿蒙 js-apis-file-cloudsyncmanager-sys

2025-06-12 浏览 (1)

@ohos.file.cloudSyncManager (Device-Cloud Sync Management) (System API)

The cloudSyncManager module provides APIs for managing device-cloud synergy for applications. You can use the APIs to enable or disable device-cloud synergy, change the device-cloud sync switch for an application, notify cloud data changes, and clear or retain cloud files when a cloud account exits.

NOTE

  • The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The APIs provided by this module are system APIs.

Modules to Import

import { cloudSyncManager } from '@kit.CoreFileKit';

cloudSyncManager.changeAppCloudSwitch

changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean): Promise<void>

Changes the device-cloud file sync switch for an application. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
bundleNamestringYesBundle name.
statusbooleanYesState of the cloud-device file sync switch to set. The value true means to enable this function; the value false means the opposite.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let bundleName: string = "com.example.bundle";
cloudSyncManager.changeAppCloudSwitch(accountId, bundleName, true).then(() => {
  console.info("changeAppCloudSwitch successfully");
}).catch((err: BusinessError) => {
  console.error("changeAppCloudSwitch failed with error message: " + err.message + ", error code: " + err.code);
});

cloudSyncManager.changeAppCloudSwitch

changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean, callback: AsyncCallback<void>): void

Changes the device-cloud file sync switch for an application. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
bundleNamestringYesBundle name of the application.
statusbooleanYesState of the cloud-device file sync switch to set. The value true means to enable this function; the value false means the opposite.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let bundleName: string = "com.example.bundle";
cloudSyncManager.changeAppCloudSwitch(accountId, bundleName, true, (err: BusinessError) => {
  if (err) {
    console.error("changeAppCloudSwitch failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("changeAppCloudSwitch successfully");
  }
});

cloudSyncManager.notifyDataChange

notifyDataChange(accountId: string, bundleName: string): Promise<void>

Notifies the cloud sync service of the application data change in the cloud. This API uses a promise to return the result.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
bundleNamestringYesBundle name.

Return value

TypeDescription
Promise<void>Promise used to return the application data change in the cloud.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let bundleName: string = "com.example.bundle";
cloudSyncManager.notifyDataChange(accountId, bundleName).then(() => {
  console.info("notifyDataChange successfully");
}).catch((err: BusinessError) => {
  console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
});

cloudSyncManager.notifyDataChange

notifyDataChange(accountId: string, bundleName: string, callback: AsyncCallback<void>): void

Notifies the cloud sync service of the application data change in the cloud. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
bundleNamestringYesBundle name.
callbackAsyncCallback<void>YesCallback used to return the application data change in the cloud.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let bundleName: string = "com.example.bundle";
cloudSyncManager.notifyDataChange(accountId, bundleName, (err: BusinessError) => {
  if (err) {
    console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("notifyDataChange successfully");
  }
});

ExtraData11+

Represents the cloud data change information.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

NameTypeMandatoryDescription
eventIdstringYesChange event ID.
extraDataExtraDataYesRepresents the cloud data change information.

cloudSyncManager.notifyDataChange11+

notifyDataChange(userId: number, extraData: ExtraData): Promise<void>

Notifies the cloud sync service of the application data change in the cloud. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
userIdnumberYesUser ID.
extraDataExtraDataYesRepresents the cloud data change information.

Return value

TypeDescription
Promise<void>Promise used to return the application data change in the cloud.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let userId: number = 100;
let extraData: cloudSyncManager.ExtraData = {eventId: "eventId", extraData: "data"};
cloudSyncManager.notifyDataChange(userId, extraData).then(() => {
  console.info("notifyDataChange successfully");
}).catch((err: BusinessError) => {
  console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
});

cloudSyncManager.notifyDataChange11+

notifyDataChange(userId: number, extraData: ExtraData, callback: AsyncCallback<void>): void

Notifies the cloud sync service of the application data change in the cloud. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
userIdnumberYesUser ID.
extraDataExtraDataYesRepresents the cloud data change information.
callbackAsyncCallback<void>YesCallback used to return the application data change in the cloud.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid.
13600001IPC error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let userId: number = 100;
let extraData: cloudSyncManager.ExtraData = {eventId: "eventId", extraData: "data"};
cloudSyncManager.notifyDataChange(userId, extraData, (err: BusinessError) => {
  if (err) {
    console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("notifyDataChange successfully");
  }
});

cloudSyncManager.enableCloud

enableCloud(accountId: string, switches: Record<string, boolean>): Promise<void>

Enables device-cloud synergy. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
switchesRecord<string, boolean>YesWhether to enable the device-cloud synergy feature. The application bundle name is a string. The switch status is a Boolean value. The value true means to enable this function; the value false means the opposite.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let switches: Record<string, boolean> = {
  'com.example.bundleName1': true,
  'com.example.bundleName2': false
}
cloudSyncManager.enableCloud(accountId, switches).then(() => {
  console.error("enableCloud successfully");
}).catch((err: BusinessError) => {
  console.info("enableCloud failed with error message: " + err.message + ", error code: " + err.code);
});

cloudSyncManager.enableCloud

enableCloud(accountId: string, switches: Record<string, boolean>, callback: AsyncCallback<void>): void

Enables device-cloud synergy. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
switchesRecord<string, boolean>YesWhether to enable the device-cloud synergy feature. The application bundle name is a string. The switch status is a Boolean value. The value true means to enable this function; the value false means the opposite.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let switches: Record<string, boolean> = {
  'com.example.bundleName1': true,
  'com.example.bundleName2': false
}
cloudSyncManager.enableCloud(accountId, switches, (err: BusinessError) => {
  if (err) {
    console.error("enableCloud failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("enableCloud successfully");
  }
});

cloudSyncManager.disableCloud

disableCloud(accountId: string): Promise<void>

Disables device-cloud synergy. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
cloudSyncManager.disableCloud(accountId).then(() => {
  console.info("disableCloud successfully");
}).catch((err: BusinessError) => {
  console.error("disableCloud failed with error message: " + err.message + ", error code: " + err.code);
});

cloudSyncManager.disableCloud

disableCloud(accountId: string, callback: AsyncCallback<void>): void

Disables device-cloud synergy. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
cloudSyncManager.disableCloud(accountId, (err: BusinessError) => {
  if (err) {
    console.error("disableCloud failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("disableCloud successfully");
  }
});

Action

Enumerates the actions that can be taken to clear local cloud data.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

NameValueDescription
RETAIN_DATA0Clear the cloud identifier but retain the files cached locally.
CLEAR_DATA1Clear the cloud identifier and the files cached locally.

cloudSyncManager.clean

clean(accountId: string, appActions: Record<string, Action>): Promise<void>

Clears the cloud data locally. This API uses a promise to return the result.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
appActionsRecord<string, Action>YesAction to perform. The bundle name of the application whose data is to be cleared is a string. Action specifies the action to perform.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
let appActions: Record<string, cloudSyncManager.Action> = {
  'com.example.bundleName1': cloudSyncManager.Action.RETAIN_DATA,
  'com.example.bundleName2': cloudSyncManager.Action.CLEAR_DATA
};
cloudSyncManager.clean(accountId, appActions).then(() => {
  console.info("clean successfully");
}).catch((err: BusinessError) => {
  console.error("clean failed with error message: " + err.message + ", error code: " + err.code);
});

cloudSyncManager.clean

clean(accountId: string, appActions: Record<string, Action>, callback: AsyncCallback<void>): void

Clears the cloud data locally. This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.CLOUDFILE_SYNC_MANAGER

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

Parameters

NameTypeMandatoryDescription
accountIdstringYesAccount ID.
appActionsRecord<string, Action>YesAction to perform. The bundle name of the application whose data is to be cleared is a string. Action specifies the action to perform.
callbackAsyncCallback<void>YesCallback used to clear the cloud data locally.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let accountId: string = "testAccount";
  let appActions: Record<string, cloudSyncManager.Action> = {
  'com.example.bundleName1': cloudSyncManager.Action.RETAIN_DATA,
  'com.example.bundleName2': cloudSyncManager.Action.CLEAR_DATA
};
cloudSyncManager.clean(accountId, appActions, (err: BusinessError) => {
  if (err) {
    console.error("clean failed with error message: " + err.message + ", error code: " + err.code);
  } else {
    console.info("clean successfully");
  }
});

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Core File Kit

harmony 鸿蒙Environment

harmony 鸿蒙FileIO

harmony 鸿蒙FileShare_PolicyErrorResult

harmony 鸿蒙FileShare_PolicyInfo

harmony 鸿蒙error_code.h

harmony 鸿蒙File Management Error Codes

harmony 鸿蒙FileShare

harmony 鸿蒙FileUri

harmony 鸿蒙@ohos.application.BackupExtensionAbility (Backup and Restore Extension Capability) (System API)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/JYqYju