1# @ohos.file.cloudSyncManager (端云同步管理能力)(系统接口)
2
3该模块向云空间应用提供端云同步管理能力,包括使能/去使能端云协同能力、修改应用同步开关,云端数据变化通知以及帐号退出清理/保留云相关文件等。
4
5> **说明:**
6>
7> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> - 本模块为系统接口。
9
10## 导入模块
11
12```ts
13import { cloudSyncManager } from '@kit.CoreFileKit';
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以下错误码的详细介绍请参见[文件管理子系统错误码](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. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
47
48**示例:**
49
50  ```ts
51  import { BusinessError } from '@kit.BasicServicesKit';
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.error("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以下错误码的详细介绍请参见[文件管理子系统错误码](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. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
87
88**示例:**
89
90  ```ts
91  import { BusinessError } from '@kit.BasicServicesKit';
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.error("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以下错误码的详细介绍请参见[文件管理子系统错误码](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. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
133
134**示例:**
135
136  ```ts
137  import { BusinessError } from '@kit.BasicServicesKit';
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.error("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以下错误码的详细介绍请参见[文件管理子系统错误码](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. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
172
173**示例:**
174
175  ```ts
176  import { BusinessError } from '@kit.BasicServicesKit';
177  let accountId: string = "testAccount";
178  let bundleName: string = "com.example.bundle";
179  cloudSyncManager.notifyDataChange(accountId, bundleName, (err: BusinessError) => {
180    if (err) {
181      console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
182    } else {
183      console.info("notifyDataChange successfully");
184    }
185  });
186  ```
187
188## ExtraData<sup>11+</sup>
189
190云端数据变更信息。
191
192**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
193
194| 名称     | 类型   | 必填 | 说明 |
195| ---------- | ------ | ---- | ---- |
196| eventId | string | 是   | 变更事件id|
197| extraData | ExtraData | 是   | 云端数据变更信息|
198
199## cloudSyncManager.notifyDataChange<sup>11+</sup>
200
201notifyDataChange(userId: number, extraData: ExtraData): Promise&lt;void&gt;
202
203异步方法通知端云服务应用的云数据变更,以Promise形式返回结果。
204
205**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
206
207**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
208
209**参数:**
210
211| 参数名     | 类型   | 必填 | 说明 |
212| ---------- | ------ | ---- | ---- |
213| userId | number | 是   | 用户Id|
214| extraData | ExtraData | 是   | 云端数据变更信息|
215
216**返回值:**
217
218| 类型                  | 说明             |
219| --------------------- | ---------------- |
220| Promise&lt;void&gt; | 使用Promise形式返回通知端云服务应用的云数据变更的结果。 |
221
222**错误码:**
223
224以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
225
226| 错误码ID                     | 错误信息        |
227| ---------------------------- | ---------- |
228| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
229| 202 | Permission verification failed, application which is not a system application uses system API. |
230| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
231| 13600001  | IPC error. |
232
233**示例:**
234
235  ```ts
236  import { BusinessError } from '@kit.BasicServicesKit';
237  let userId: number = 100;
238  let extraData: cloudSyncManager.ExtraData = {eventId: "eventId", extraData: "data"};
239  cloudSyncManager.notifyDataChange(userId, extraData).then(() => {
240    console.info("notifyDataChange successfully");
241  }).catch((err: BusinessError) => {
242    console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
243  });
244  ```
245
246## cloudSyncManager.notifyDataChange<sup>11+</sup>
247
248notifyDataChange(userId: number, extraData: ExtraData, callback: AsyncCallback&lt;void&gt;): void
249
250异步方法通知端云服务应用的云数据变更,以callback形式返回结果。
251
252**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
253
254**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
255
256**参数:**
257
258| 参数名     | 类型   | 必填 | 说明 |
259| ---------- | ------ | ---- | ---- |
260| userId | number | 是   | 用户Id|
261| extraData | ExtraData | 是   | 云端数据变更信息|
262| callback | AsyncCallback&lt;void&gt; | 是   | 异步通知端云服务应用的云数据变更之后的回调。 |
263
264**错误码:**
265
266以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
267
268| 错误码ID                     | 错误信息        |
269| ---------------------------- | ---------- |
270| 201 | Permission verification failed. |
271| 202 | The caller is not a system application. |
272| 401 | The input parameter is invalid. |
273| 13600001  | IPC error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
274
275**示例:**
276
277  ```ts
278  import { BusinessError } from '@kit.BasicServicesKit';
279  let userId: number = 100;
280  let extraData: cloudSyncManager.ExtraData = {eventId: "eventId", extraData: "data"};
281  cloudSyncManager.notifyDataChange(userId, extraData, (err: BusinessError) => {
282    if (err) {
283      console.error("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
284    } else {
285      console.info("notifyDataChange successfully");
286    }
287  });
288  ```
289
290## cloudSyncManager.enableCloud
291
292enableCloud(accountId: string, switches: Record<string, boolean>): Promise&lt;void&gt;
293
294异步方法使能端云协同能力,以Promise形式返回结果。
295
296**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
297
298**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
299
300**参数:**
301
302| 参数名     | 类型   | 必填 | 说明 |
303| ---------- | ------ | ---- | ---- |
304| accountId | string | 是   | 帐号Id|
305| switches | object | 是   | 应用的端云协同特性使能开关,bundleName为string类型应用包名,开关状态是个boolean类型|
306
307**返回值:**
308
309| 类型                  | 说明             |
310| --------------------- | ---------------- |
311| Promise&lt;void&gt; | 使用Promise形式返回使能端云协同能力的结果 |
312
313**错误码:**
314
315以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
316
317| 错误码ID                     | 错误信息        |
318| ---------------------------- | ---------- |
319| 201 | Permission verification failed. |
320| 202 | The caller is not a system application. |
321| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
322
323**示例:**
324
325  ```ts
326  import { BusinessError } from '@kit.BasicServicesKit';
327  let accountId: string = "testAccount";
328  let switches: Record<string, boolean> = {
329    'com.example.bundleName1': true,
330    'com.example.bundleName2': false
331  }
332  cloudSyncManager.enableCloud(accountId, switches).then(() => {
333    console.error("enableCloud successfully");
334  }).catch((err: BusinessError) => {
335    console.info("enableCloud failed with error message: " + err.message + ", error code: " + err.code);
336  });
337  ```
338
339## cloudSyncManager.enableCloud
340
341enableCloud(accountId: string, switches: Record<string, boolean>, callback: AsyncCallback&lt;void&gt;): void
342
343异步方法使能端云协同能力,以callback形式返回结果。
344
345**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
346
347**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
348
349**参数:**
350
351| 参数名     | 类型   | 必填 | 说明 |
352| ---------- | ------ | ---- | ---- |
353| accountId | string | 是   | 帐号Id|
354| switches | object | 是   | 应用的端云协同特性使能开关,bundleName为string类型应用包名,开关状态是个boolean类型|
355| callback | AsyncCallback&lt;void&gt; | 是   | 异步使能端云协同能力之后的回调 |
356
357**错误码:**
358
359以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
360
361| 错误码ID                     | 错误信息        |
362| ---------------------------- | ---------- |
363| 201 | Permission verification failed. |
364| 202 | The caller is not a system application. |
365| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
366
367**示例:**
368
369  ```ts
370  import { BusinessError } from '@kit.BasicServicesKit';
371  let accountId: string = "testAccount";
372  let switches: Record<string, boolean> = {
373    'com.example.bundleName1': true,
374    'com.example.bundleName2': false
375  }
376  cloudSyncManager.enableCloud(accountId, switches, (err: BusinessError) => {
377    if (err) {
378      console.error("enableCloud failed with error message: " + err.message + ", error code: " + err.code);
379    } else {
380      console.info("enableCloud successfully");
381    }
382  });
383  ```
384
385## cloudSyncManager.disableCloud
386
387disableCloud(accountId: string): Promise&lt;void&gt;
388
389异步方法去使能端云协同能力,以Promise形式返回结果。
390
391**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
392
393**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
394
395**参数:**
396
397| 参数名     | 类型   | 必填 | 说明 |
398| ---------- | ------ | ---- | ---- |
399| accountId | string | 是   | 帐号Id|
400
401**返回值:**
402
403| 类型                  | 说明             |
404| --------------------- | ---------------- |
405| Promise&lt;void&gt; | 使用Promise形式返回去使能端云协同能力的结果。 |
406
407**错误码:**
408
409以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
410
411| 错误码ID                     | 错误信息        |
412| ---------------------------- | ---------- |
413| 201 | Permission verification failed. |
414| 202 | The caller is not a system application. |
415| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
416
417**示例:**
418
419  ```ts
420  import { BusinessError } from '@kit.BasicServicesKit';
421  let accountId: string = "testAccount";
422  cloudSyncManager.disableCloud(accountId).then(() => {
423    console.info("disableCloud successfully");
424  }).catch((err: BusinessError) => {
425    console.error("disableCloud failed with error message: " + err.message + ", error code: " + err.code);
426  });
427  ```
428
429## cloudSyncManager.disableCloud
430
431disableCloud(accountId: string, callback: AsyncCallback&lt;void&gt;): void
432
433异步方法去使能端云协同能力,以callback形式返回结果。
434
435**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
436
437**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
438
439**参数:**
440
441| 参数名     | 类型   | 必填 | 说明 |
442| ---------- | ------ | ---- | ---- |
443| accountId | string | 是   | 帐号Id|
444| callback | AsyncCallback&lt;void&gt; | 是   | 异步去使能端云协同能力之后的回调 |
445
446**错误码:**
447
448以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
449
450| 错误码ID                     | 错误信息        |
451| ---------------------------- | ---------- |
452| 201 | Permission verification failed. |
453| 202 | The caller is not a system application. |
454| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
455
456**示例:**
457
458  ```ts
459  import { BusinessError } from '@kit.BasicServicesKit';
460  let accountId: string = "testAccount";
461  cloudSyncManager.disableCloud(accountId, (err: BusinessError) => {
462    if (err) {
463      console.error("disableCloud failed with error message: " + err.message + ", error code: " + err.code);
464    } else {
465      console.info("disableCloud successfully");
466    }
467  });
468  ```
469
470## Action
471
472清理本地云相关数据时的Action,为枚举类型。
473
474**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
475
476**系统能力**: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
477
478| 名称 |  值|  说明 |
479| ----- |  ---- |  ---- |
480| RETAIN_DATA |  0 |  仅清除云端标识,保留本地缓存文件|
481| CLEAR_DATA |  1 |  清除云端标识信息,若存在本地缓存文件,一并删除|
482
483## cloudSyncManager.clean
484
485clean(accountId: string, appActions: Record<string, Action>): Promise&lt;void&gt;
486
487异步方法清理本地云相关数据,以Promise形式返回结果。
488
489**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
490
491**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
492
493**参数:**
494
495| 参数名     | 类型   | 必填 | 说明 |
496| ---------- | ------ | ---- | ---- |
497| accountId | string | 是   | 帐号Id|
498| appActions | object | 是   | 清理动作类型,bundleName为string类型待清理应用包名, [Action](#action)为清理动作类型|
499
500**返回值:**
501
502| 类型                  | 说明             |
503| --------------------- | ---------------- |
504| Promise&lt;void&gt; | 使用Promise形式返回清理本地云相关数据的结果 |
505
506**错误码:**
507
508以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
509
510| 错误码ID                     | 错误信息        |
511| ---------------------------- | ---------- |
512| 201 | Permission verification failed. |
513| 202 | The caller is not a system application. |
514| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
515
516**示例:**
517
518  ```ts
519  import { BusinessError } from '@kit.BasicServicesKit';
520  let accountId: string = "testAccount";
521  let appActions: Record<string, cloudSyncManager.Action> = {
522    'com.example.bundleName1': cloudSyncManager.Action.RETAIN_DATA,
523    'com.example.bundleName2': cloudSyncManager.Action.CLEAR_DATA
524  };
525  cloudSyncManager.clean(accountId, appActions).then(() => {
526    console.info("clean successfully");
527  }).catch((err: BusinessError) => {
528    console.error("clean failed with error message: " + err.message + ", error code: " + err.code);
529  });
530  ```
531
532## cloudSyncManager.clean
533
534clean(accountId: string, appActions: Record<string, Action>, callback: AsyncCallback&lt;void&gt;): void
535
536异步方法清理本地云相关数据,以callback形式返回结果。
537
538**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER
539
540**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager
541
542**参数:**
543
544| 参数名     | 类型   | 必填 | 说明 |
545| ---------- | ------ | ---- | ---- |
546| accountId | string | 是   | 帐号Id|
547| appActions | object | 是   | 清理动作类型,bundleName为string类型待清理应用包名, [Action](#action)为清理动作类型|
548| callback | AsyncCallback&lt;void&gt; | 是   | 异步方法清理本地云相关数据 |
549
550**错误码:**
551
552以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。
553
554| 错误码ID                     | 错误信息        |
555| ---------------------------- | ---------- |
556| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
557| 202 | Permission verification failed, application which is not a system application uses system API. |
558| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
559| 13600001  | IPC error. |
560
561**示例:**
562
563  ```ts
564  import { BusinessError } from '@kit.BasicServicesKit';
565  let accountId: string = "testAccount";
566    let appActions: Record<string, cloudSyncManager.Action> = {
567    'com.example.bundleName1': cloudSyncManager.Action.RETAIN_DATA,
568    'com.example.bundleName2': cloudSyncManager.Action.CLEAR_DATA
569  };
570  cloudSyncManager.clean(accountId, appActions, (err: BusinessError) => {
571    if (err) {
572      console.error("clean failed with error message: " + err.message + ", error code: " + err.code);
573    } else {
574      console.info("clean successfully");
575    }
576  });
577  ```
578