1# @ohos.file.cloudSyncManager (端云同步管理能力) 2 3该模块向云空间应用提供端云同步管理能力,包括使能/去使能端云协同能力、修改应用同步开关,云端数据变化通知以及帐号退出清理/保留云相关文件等。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 本模块接口为系统接口,三方应用不支持调用。 9 10## 导入模块 11 12```ts 13import cloudSyncManager from '@ohos.file.cloudSyncManager'; 14``` 15 16## cloudSyncManager.changeAppCloudSwitch 17 18changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean): Promise<void> 19 20异步方法修改应用的端云文件同步开关,以Promise形式返回结果。 21 22**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 23 24**参数:** 25 26| 参数名 | 类型 | 必填 | 说明 | 27| ---------- | ------ | ---- | ---- | 28| accountId | string | 是 | 帐号Id | 29| bundleName | string | 是 | 应用包名| 30| status | boolean | 是 | 修改的应用云同步开关状态,true为打开,false为关闭| 31 32**返回值:** 33 34| 类型 | 说明 | 35| --------------------- | ---------------- | 36| Promise<void> | 使用Promise形式返回修改应用的端云文件同步开关的结果 | 37 38**错误码:** 39 40以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 41 42| 错误码ID | 错误信息 | 43| ---------------------------- | ---------- | 44| 201 | Permission verification failed. | 45| 202 | The caller is not a system application. | 46| 401 | The input parameter is invalid. | 47 48**示例:** 49 50 ```ts 51 import { BusinessError } from '@ohos.base'; 52 let accountId: string = "testAccount"; 53 let bundleName: string = "com.example.bundle"; 54 cloudSyncManager.changeAppCloudSwitch(accountId, bundleName, true).then(() => { 55 console.info("changeAppCloudSwitch successfully"); 56 }).catch((err: BusinessError) => { 57 console.info("changeAppCloudSwitch failed with error message: " + err.message + ", error code: " + err.code); 58 }); 59 ``` 60 61## cloudSyncManager.changeAppCloudSwitch 62 63changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean, callback: AsyncCallback<void>): void 64 65异步方法修改应用的端云文件同步开关,以callback形式返回结果。 66 67**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 68 69**参数:** 70 71| 参数名 | 类型 | 必填 | 说明 | 72| ---------- | ------ | ---- | ---- | 73| accountId | string | 是 | 帐号Id| 74| bundleName | string | 是 | 应用包名| 75| status | boolean | 是 | 修改的应用云同步开关状态,true为打开,false为关闭| 76| callback | AsyncCallback<void> | 是 | 异步修改应用的端云文件同步开关之后的回调 | 77 78**错误码:** 79 80以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 81 82| 错误码ID | 错误信息 | 83| ---------------------------- | ---------- | 84| 201 | Permission verification failed. | 85| 202 | The caller is not a system application. | 86| 401 | The input parameter is invalid. | 87 88**示例:** 89 90 ```ts 91 import { BusinessError } from '@ohos.base'; 92 let accountId: string = "testAccount"; 93 let bundleName: string = "com.example.bundle"; 94 cloudSyncManager.changeAppCloudSwitch(accountId, bundleName, true, (err: BusinessError) => { 95 if (err) { 96 console.info("changeAppCloudSwitch failed with error message: " + err.message + ", error code: " + err.code); 97 } else { 98 console.info("changeAppCloudSwitch successfully"); 99 } 100 }); 101 ``` 102 103## cloudSyncManager.notifyDataChange 104 105notifyDataChange(accountId: string, bundleName: string): Promise<void> 106 107异步方法通知端云服务应用的云数据变更,以Promise形式返回结果。 108 109**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 110 111**参数:** 112 113| 参数名 | 类型 | 必填 | 说明 | 114| ---------- | ------ | ---- | ---- | 115| accountId | string | 是 | 帐号Id| 116| bundleName | string | 是 | 应用包名| 117 118**返回值:** 119 120| 类型 | 说明 | 121| --------------------- | ---------------- | 122| Promise<void> | 使用Promise形式返回通知端云服务应用的云数据变更的结果 | 123 124**错误码:** 125 126以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 127 128| 错误码ID | 错误信息 | 129| ---------------------------- | ---------- | 130| 201 | Permission verification failed. | 131| 202 | The caller is not a system application. | 132| 401 | The input parameter is invalid. | 133 134**示例:** 135 136 ```ts 137 import { BusinessError } from '@ohos.base'; 138 let accountId: string = "testAccount"; 139 let bundleName: string = "com.example.bundle"; 140 cloudSyncManager.notifyDataChange(accountId, bundleName).then(() => { 141 console.info("notifyDataChange successfully"); 142 }).catch((err: BusinessError) => { 143 console.info("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code); 144 }); 145 ``` 146 147## cloudSyncManager.notifyDataChange 148 149notifyDataChange(accountId: string, bundleName: string, callback: AsyncCallback<void>): void 150 151异步方法通知端云服务应用的云数据变更,以callback形式返回结果。 152 153**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 154 155**参数:** 156 157| 参数名 | 类型 | 必填 | 说明 | 158| ---------- | ------ | ---- | ---- | 159| accountId | string | 是 | 帐号Id| 160| bundleName | string | 是 | 应用包名| 161| callback | AsyncCallback<void> | 是 | 异步通知端云服务应用的云数据变更之后的回调 | 162 163**错误码:** 164 165以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 166 167| 错误码ID | 错误信息 | 168| ---------------------------- | ---------- | 169| 201 | Permission verification failed. | 170| 202 | The caller is not a system application. | 171| 401 | The input parameter is invalid. | 172 173**示例:** 174 175 ```ts 176 import { BusinessError } from '@ohos.base'; 177 let accountId: string = "testAccount"; 178 let bundleName: string = "com.example.bundle"; 179 cloudSyncManager.notifyDataChange(accountId, bundleName, (err: BusinessError) => { 180 if (err) { 181 console.info("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code); 182 } else { 183 console.info("notifyDataChange successfully"); 184 } 185 }); 186 ``` 187 188## cloudSyncManager.enableCloud 189 190enableCloud(accountId: string, switches: { [bundleName: string]: boolean }): Promise<void> 191 192异步方法使能端云协同能力,以Promise形式返回结果。 193 194**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER 195 196**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 197 198**参数:** 199 200| 参数名 | 类型 | 必填 | 说明 | 201| ---------- | ------ | ---- | ---- | 202| accountId | string | 是 | 帐号Id| 203| switches | object | 是 | 应用的端云协同特性使能开关,bundleName为应用包名,开关状态是个boolean类型| 204 205**返回值:** 206 207| 类型 | 说明 | 208| --------------------- | ---------------- | 209| Promise<void> | 使用Promise形式返回使能端云协同能力的结果 | 210 211**错误码:** 212 213以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 214 215| 错误码ID | 错误信息 | 216| ---------------------------- | ---------- | 217| 201 | Permission verification failed. | 218| 202 | The caller is not a system application. | 219| 401 | The input parameter is invalid. | 220 221**示例:** 222 223 ```ts 224 import { BusinessError } from '@ohos.base'; 225 let accountId: string = "testAccount"; 226 let switches: Record<string, boolean> = { 227 'com.example.bundleName1': true, 228 'com.example.bundleName2': false 229 } 230 cloudSyncManager.enableCloud(accountId, switches).then(() => { 231 console.info("enableCloud successfully"); 232 }).catch((err: BusinessError) => { 233 console.info("enableCloud failed with error message: " + err.message + ", error code: " + err.code); 234 }); 235 ``` 236 237## cloudSyncManager.enableCloud 238 239enableCloud(accountId: string, switches: { [bundleName: string]: boolean }, callback: AsyncCallback<void>): void 240 241异步方法使能端云协同能力,以callback形式返回结果。 242 243**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER 244 245**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 246 247**参数:** 248 249| 参数名 | 类型 | 必填 | 说明 | 250| ---------- | ------ | ---- | ---- | 251| accountId | string | 是 | 帐号Id| 252| switches | object | 是 | 应用的端云协同特性使能开关,bundleName为应用包名,开关状态是个boolean类型| 253| callback | AsyncCallback<void> | 是 | 异步使能端云协同能力之后的回调 | 254 255**错误码:** 256 257以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 258 259| 错误码ID | 错误信息 | 260| ---------------------------- | ---------- | 261| 201 | Permission verification failed. | 262| 202 | The caller is not a system application. | 263| 401 | The input parameter is invalid. | 264 265**示例:** 266 267 ```ts 268 import { BusinessError } from '@ohos.base'; 269 let accountId: string = "testAccount"; 270 let switches: Record<string, boolean> = { 271 'com.example.bundleName1': true, 272 'com.example.bundleName2': false 273 } 274 cloudSyncManager.enableCloud(accountId, switches, (err: BusinessError) => { 275 if (err) { 276 console.info("enableCloud failed with error message: " + err.message + ", error code: " + err.code); 277 } else { 278 console.info("enableCloud successfully"); 279 } 280 }); 281 ``` 282 283## cloudSyncManager.disableCloud 284 285disableCloud(accountId: string): Promise<void> 286 287异步方法去使能端云协同能力,以Promise形式返回结果。 288 289**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER 290 291**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 292 293**参数:** 294 295| 参数名 | 类型 | 必填 | 说明 | 296| ---------- | ------ | ---- | ---- | 297| accountId | string | 是 | 帐号Id| 298 299**返回值:** 300 301| 类型 | 说明 | 302| --------------------- | ---------------- | 303| Promise<void> | 使用Promise形式返回去使能端云协同能力的结果 | 304 305**错误码:** 306 307以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 308 309| 错误码ID | 错误信息 | 310| ---------------------------- | ---------- | 311| 201 | Permission verification failed. | 312| 202 | The caller is not a system application. | 313| 401 | The input parameter is invalid. | 314 315**示例:** 316 317 ```ts 318 import { BusinessError } from '@ohos.base'; 319 let accountId: string = "testAccount"; 320 cloudSyncManager.disableCloud(accountId).then(() => { 321 console.info("disableCloud successfully"); 322 }).catch((err: BusinessError) => { 323 console.info("disableCloud failed with error message: " + err.message + ", error code: " + err.code); 324 }); 325 ``` 326 327## cloudSyncManager.disableCloud 328 329disableCloud(accountId: string, callback: AsyncCallback<void>): void 330 331异步方法去使能端云协同能力,以callback形式返回结果。 332 333**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER 334 335**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 336 337**参数:** 338 339| 参数名 | 类型 | 必填 | 说明 | 340| ---------- | ------ | ---- | ---- | 341| accountId | string | 是 | 帐号Id| 342| callback | AsyncCallback<void> | 是 | 异步去使能端云协同能力之后的回调 | 343 344**错误码:** 345 346以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 347 348| 错误码ID | 错误信息 | 349| ---------------------------- | ---------- | 350| 201 | Permission verification failed. | 351| 202 | The caller is not a system application. | 352| 401 | The input parameter is invalid. | 353 354**示例:** 355 356 ```ts 357 import { BusinessError } from '@ohos.base'; 358 let accountId: string = "testAccount"; 359 cloudSyncManager.disableCloud(accountId, (err: BusinessError) => { 360 if (err) { 361 console.info("disableCloud failed with error message: " + err.message + ", error code: " + err.code); 362 } else { 363 console.info("disableCloud successfully"); 364 } 365 }); 366 ``` 367 368## Action 369 370清理本地云相关数据时的Action,为枚举类型。 371 372**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER 373 374**系统能力**: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 375 376| 名称 | 值| 说明 | 377| ----- | ---- | ---- | 378| RETAIN_DATA | 0 | 仅清除云端标识,保留本地缓存文件| 379| CLEAR_DATA | 1 | 清除云端标识信息,若存在本地缓存文件,一并删除| 380 381## cloudSyncManager.clean 382 383clean(accountId: string, appActions: { [bundleName: string]: Action }): Promise<void> 384 385异步方法清理本地云相关数据,以Promise形式返回结果。 386 387**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER 388 389**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 390 391**参数:** 392 393| 参数名 | 类型 | 必填 | 说明 | 394| ---------- | ------ | ---- | ---- | 395| accountId | string | 是 | 帐号Id| 396| appActions | object | 是 | 清理动作类型,bundleName为待清理应用包名, [Action](#action)为清理动作类型| 397 398**返回值:** 399 400| 类型 | 说明 | 401| --------------------- | ---------------- | 402| Promise<void> | 使用Promise形式返回清理本地云相关数据的结果 | 403 404**错误码:** 405 406以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 407 408| 错误码ID | 错误信息 | 409| ---------------------------- | ---------- | 410| 201 | Permission verification failed. | 411| 202 | The caller is not a system application. | 412| 401 | The input parameter is invalid. | 413 414**示例:** 415 416 ```ts 417 import { BusinessError } from '@ohos.base'; 418 let accountId: string = "testAccount"; 419 let appActions: Record<string, cloudSyncManager.Action> = { 420 'com.example.bundleName1': cloudSyncManager.Action.RETAIN_DATA, 421 'com.example.bundleName2': cloudSyncManager.Action.CLEAR_DATA 422 }; 423 cloudSyncManager.clean(accountId, appActions).then(() => { 424 console.info("clean successfully"); 425 }).catch((err: BusinessError) => { 426 console.info("clean failed with error message: " + err.message + ", error code: " + err.code); 427 }); 428 ``` 429 430## cloudSyncManager.clean 431 432clean(accountId: string, appActions: { [bundleName: string]: Action }, callback: AsyncCallback<void>): void 433 434异步方法清理本地云相关数据,以callback形式返回结果。 435 436**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER 437 438**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager 439 440**参数:** 441 442| 参数名 | 类型 | 必填 | 说明 | 443| ---------- | ------ | ---- | ---- | 444| accountId | string | 是 | 帐号Id| 445| appActions | object | 是 | 清理动作类型,bundleName为待清理应用包名, [Action](#action)为清理动作类型| 446| callback | AsyncCallback<void> | 是 | 异步方法清理本地云相关数据 | 447 448**错误码:** 449 450以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)。 451 452| 错误码ID | 错误信息 | 453| ---------------------------- | ---------- | 454| 201 | Permission verification failed. | 455| 202 | The caller is not a system application. | 456| 401 | The input parameter is invalid. | 457 458**示例:** 459 460 ```ts 461 import { BusinessError } from '@ohos.base'; 462 let accountId: string = "testAccount"; 463 let appActions: Record<string, cloudSyncManager.Action> = { 464 'com.example.bundleName1': cloudSyncManager.Action.RETAIN_DATA, 465 'com.example.bundleName2': cloudSyncManager.Action.CLEAR_DATA 466 }; 467 cloudSyncManager.clean(accountId, appActions, (err: BusinessError) => { 468 if (err) { 469 console.info("clean failed with error message: " + err.message + ", error code: " + err.code); 470 } else { 471 console.info("clean successfully"); 472 } 473 }); 474 ``` 475