• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.cloudSync (Device-Cloud Synchronization)
2<!--Kit: Core File Kit-->
3<!--Subsystem: FileManagement-->
4<!--Owner: @zsyztt; @Hermits; @reminder2352-->
5<!--Designer: @yunlanying-->
6<!--Tester: @liuhonggang123-->
7<!--Adviser: @foryourself-->
8
9The **cloudSync** module provides the device-cloud synchronization capabilities for applications. You can use the APIs to start or stop device-cloud synchronization and start or stop the download of images.
10
11> **NOTE**
12>
13> 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.
14
15## Modules to Import
16
17```ts
18import { cloudSync } from '@kit.CoreFileKit';
19```
20## SyncState<sup>12+</sup>
21
22Enumerates the device-cloud sync states.
23
24> **NOTE**
25>
26> If a sync progress event listener is registered for an application, a callback will be invoked to notify the application when the device-cloud sync state is changed.
27
28**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
29
30| Name|  Value|  Description|
31| ----- |  ---- |  ---- |
32| UPLOADING |  0 | The file is being uploaded.|
33| UPLOAD_FAILED |  1 | Upload failed.|
34| DOWNLOADING |  2 | The file is being downloaded.|
35| DOWNLOAD_FAILED |  3 | Download failed.|
36| COMPLETED |  4 | Sync completed.|
37| STOPPED |  5 | Sync stopped.|
38
39## ErrorType<sup>12+</sup>
40
41Enumerates the device-cloud sync errors.
42
43- In the current phase, **NETWORK_UNAVAILABLE** is returned only when the mobile data network and Wi-Fi are unavailable. If the mobile data network is available, the synchronization can be performed normally.
44- During the sync process, if the battery level is lower than 10% in non-charging scenarios, **BATTERY_LEVEL_LOW** will be return when the current upload is complete.
45- When sync is being triggered, if the battery level is lower than 10% in non-charging scenarios, sync is not allowed and an error code will be returned by **start()**.
46- If the cloud space is insufficient when a file is uploaded, the upload will fail and there is no such a file in the cloud.
47
48**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
49
50| Name|  Value|  Description|
51| ----- |  ---- |  ---- |
52| NO_ERROR |  0 | No error.|
53| NETWORK_UNAVAILABLE |  1 | No network is available.|
54| WIFI_UNAVAILABLE |  2 | Wi-Fi is unavailable.|
55| BATTERY_LEVEL_LOW |  3 | The battery level is lower than 10%.|
56| BATTERY_LEVEL_WARNING |  4 | The battery level is lower than 15%.|
57| CLOUD_STORAGE_FULL |  5 | The cloud space is insufficient.|
58| LOCAL_STORAGE_FULL |  6 | The local space is insufficient.|
59| DEVICE_TEMPERATURE_TOO_HIGH |  7 | The device temperature is too high.|
60
61## SyncProgress<sup>12+</sup>
62
63Represents information about the device-cloud sync progress.
64
65### Property
66
67**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
68
69| Name    | Type  | Read-Only| Optional| Description|
70| ---------- | ------ | ---- | ---- | ---- |
71| state | [SyncState](#syncstate12) | No  | No  | Device-cloud sync state.|
72| error | [ErrorType](#errortype12) | No  | No  | Sync error.|
73
74## State<sup>11+</sup>
75
76Enumerates the download states of a cloud file.
77
78**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
79
80| Name|  Value|  Description|
81| ----- |  ---- |  ---- |
82| RUNNING |  0 | The cloud file is being downloaded.|
83| COMPLETED |  1 | The cloud file download is complete.|
84| FAILED |  2 | The cloud file download failed.|
85| STOPPED |  3 | The cloud file download is stopped.|
86
87## DownloadProgress<sup>11+</sup>
88
89Represents information about the download progress of a cloud file.
90
91### Property
92
93**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
94
95| Name    | Type  | Read-Only| Optional| Description|
96| ---------- | ------ | ---- | ---- | ---- |
97| state | [State](#state11) | No  | No  | File download state.|
98| processed | number | No  | No  | Size of the downloaded data, in bytes. The value range is [0, 9223372036854775807].|
99| size | number | No  | No  | Size of the cloud file, in bytes. The value range is [0, 9223372036854775807].|
100| uri | string | No  | No  | URI of the cloud file.|
101| error | [DownloadErrorType](#downloaderrortype11) | No  | No  | Download error type.|
102
103## FileSync<sup>12+</sup>
104
105Provides APIs for the file manager application to perform device-cloud sync of the files stored in the Drive Kit. Before using the APIs of this class, you need to create a **FileSync** instance.
106
107**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
108
109### constructor<sup>12+</sup>
110
111constructor()
112
113A constructor used to create a **FileSync** instance.
114
115**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
116
117**Error codes**
118
119For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
120
121| ID                    | Error Message       |
122| ---------------------------- | ---------- |
123| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
124
125**Example**
126
127  ```ts
128  let fileSync = new cloudSync.FileSync()
129  ```
130
131### on<sup>12+</sup>
132
133on(event: 'progress', callback: Callback\<SyncProgress>): void
134
135Registers a listener for the device-cloud sync progress.
136
137**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
138
139**Parameters**
140
141| Name    | Type  | Mandatory| Description|
142| ---------- | ------ | ---- | ---- |
143| event | string | Yes  | Event type. The value is **progress**, which indicates the sync progress event.|
144| callback | Callback\<[SyncProgress](#syncprogress12)> | Yes  | Callback used to return the sync progress information.|
145
146**Error codes**
147
148For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
149
150| ID                    | Error Message       |
151| ---------------------------- | ---------- |
152| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
153| 13600001  | IPC error. |
154
155**Example**
156
157  ```ts
158  let fileSync = new cloudSync.FileSync();
159  let callback = (pg: cloudSync.SyncProgress) => {
160    console.info("file sync state: " + pg.state + "error type:" + pg.error);
161  }
162
163  fileSync.on('progress', callback);
164  ```
165
166### off<sup>12+</sup>
167
168off(event: 'progress', callback?: Callback\<SyncProgress>): void
169
170Unregisters all listeners for the device-cloud sync progress.
171
172**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
173
174**Parameters**
175
176| Name    | Type  | Mandatory| Description|
177| ---------- | ------ | ---- | ---- |
178| event | string | Yes  | Event type. The value is **progress**, which indicates the sync progress event.|
179| callback | Callback\<[SyncProgress](#syncprogress12)> |  No  | Callback used to return the sync progress. The default value is null.|
180
181**Error codes**
182
183For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
184
185| ID                    | Error Message       |
186| ---------------------------- | ---------- |
187| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
188| 13600001  | IPC error. |
189
190**Example**
191
192  ```ts
193  let fileSync = new cloudSync.FileSync();
194
195  let callback = (pg: cloudSync.SyncProgress) => {
196    console.info("file sync state: " + pg.state + "error type:" + pg.error);
197  }
198
199  fileSync.on('progress', callback);
200
201  fileSync.off('progress', callback);
202  ```
203
204### start<sup>12+</sup>
205
206start(): Promise&lt;void&gt;
207
208Starts device-cloud sync of a file in the Drive Kit. This API uses a promise to return the result.
209
210**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
211
212**Return value**
213
214| Type                 | Description            |
215| --------------------- | ---------------- |
216| Promise&lt;void&gt; | Promise that returns no value.|
217
218**Error codes**
219
220For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
221
222| ID                    | Error Message       |
223| ---------------------------- | ---------- |
224| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
225| 13600001  | IPC error. |
226| 22400001  | Cloud status not ready. |
227| 22400002  | Network unavailable. |
228| 22400003  | Low battery level. |
229
230**Example**
231
232  ```ts
233  import { BusinessError } from '@kit.BasicServicesKit';
234  let fileSync = new cloudSync.FileSync();
235
236  let callback = (pg: cloudSync.SyncProgress) => {
237    console.info("file sync state: " + pg.state + "error type:" + pg.error);
238  }
239
240  fileSync.on('progress', callback);
241
242  fileSync.start().then(() => {
243    console.info("start sync successfully");
244  }).catch((err: BusinessError) => {
245    console.error("start sync failed with error message: " + err.message + ", error code: " + err.code);
246  });
247  ```
248
249### start<sup>12+</sup>
250
251start(callback: AsyncCallback&lt;void&gt;): void
252
253Starts device-cloud sync of a file in the Drive Kit. This API uses an asynchronous callback to return the result.
254
255**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
256
257**Parameters**
258
259| Name    | Type  | Mandatory| Description|
260| ---------- | ------ | ---- | ---- |
261| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
262
263**Error codes**
264
265For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
266
267| ID                    | Error Message       |
268| ---------------------------- | ---------- |
269| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
270| 13600001  | IPC error. |
271| 22400001  | Cloud status not ready. |
272| 22400002  | Network unavailable. |
273| 22400003  | Low battery level. |
274
275**Example**
276
277  ```ts
278  import { BusinessError } from '@kit.BasicServicesKit';
279  let fileSync = new cloudSync.FileSync();
280
281  fileSync.start((err: BusinessError) => {
282    if (err) {
283      console.error("start sync failed with error message: " + err.message + ", error code: " + err.code);
284    } else {
285      console.info("start sync successfully");
286    }
287  });
288  ```
289
290### stop<sup>12+</sup>
291
292stop(): Promise&lt;void&gt;
293
294Stops device-cloud sync of the file in the Drive Kit. This API uses a promise to return the result.
295
296Calling **stop** will stop the sync process. To resume the sync, call [start](#start12).
297
298**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
299
300**Return value**
301
302| Type                 | Description            |
303| --------------------- | ---------------- |
304| Promise&lt;void&gt; | Promise used to return the result.|
305
306**Error codes**
307
308For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
309
310| ID                    | Error Message       |
311| ---------------------------- | ---------- |
312| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
313| 13600001  | IPC error. |
314
315**Example**
316
317  ```ts
318  import { BusinessError } from '@kit.BasicServicesKit';
319  let fileSync = new cloudSync.FileSync();
320
321  fileSync.stop().then(() => {
322    console.info("stop sync successfully");
323  }).catch((err: BusinessError) => {
324    console.error("stop sync failed with error message: " + err.message + ", error code: " + err.code);
325  });
326  ```
327
328### stop<sup>12+</sup>
329
330stop(callback: AsyncCallback&lt;void&gt;): void
331
332Stops device-cloud sync of the file in the Drive Kit. This API uses an asynchronous callback to return the result.
333
334Calling **stop** will stop the sync process. To resume the sync, call [start](#start12).
335
336**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
337
338**Parameters**
339
340| Name    | Type  | Mandatory| Description|
341| ---------- | ------ | ---- | ---- |
342| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
343
344**Error codes**
345
346For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
347
348| ID                    | Error Message       |
349| ---------------------------- | ---------- |
350| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
351| 13600001  | IPC error. |
352
353**Example**
354
355  ```ts
356  import { BusinessError } from '@kit.BasicServicesKit';
357  let fileSync = new cloudSync.FileSync();
358
359  fileSync.stop((err: BusinessError) => {
360    if (err) {
361      console.error("stop sync failed with error message: " + err.message + ", error code: " + err.code);
362    } else {
363      console.info("stop sync successfully");
364    }
365  });
366  ```
367
368### getLastSyncTime<sup>12+</sup>
369
370getLastSyncTime(): Promise&lt;number&gt;
371
372Obtains the last sync time. This API uses a promise to return the result.
373
374**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
375
376**Return value**
377
378| Type                 | Description            |
379| --------------------- | ---------------- |
380| Promise&lt;number&gt; | Promise used to return the last sync time obtained.|
381
382**Error codes**
383
384For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
385
386| ID                    | Error Message       |
387| ---------------------------- | ---------- |
388| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
389| 13600001  | IPC error. |
390
391**Example**
392
393  ```ts
394  import { BusinessError } from '@kit.BasicServicesKit';
395  let fileSync = new cloudSync.FileSync();
396
397  fileSync.getLastSyncTime().then((timeStamp: number) => {
398    let date = new Date(timeStamp);
399    console.info("get last sync time successfully:"+ date);
400  }).catch((err: BusinessError) => {
401    console.error("get last sync time failed with error message: " + err.message + ", error code: " + err.code);
402  });
403
404  ```
405
406### getLastSyncTime<sup>12+</sup>
407
408getLastSyncTime(callback: AsyncCallback&lt;number&gt;): void
409
410Obtains the last sync time. This API uses an asynchronous callback to return the result.
411
412**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
413
414**Parameters**
415
416| Name    | Type  | Mandatory| Description|
417| ---------- | ------ | ---- | ---- |
418| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the last sync time obtained.|
419
420**Error codes**
421
422For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
423
424| ID                    | Error Message       |
425| ---------------------------- | ---------- |
426| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
427| 13600001  | IPC error. |
428
429**Example**
430
431  ```ts
432  import { BusinessError } from '@kit.BasicServicesKit';
433  let fileSync = new cloudSync.FileSync();
434
435  fileSync.getLastSyncTime((err: BusinessError, timeStamp: number) => {
436    if (err) {
437      console.error("get last sync time with error message: " + err.message + ", error code: " + err.code);
438    } else {
439      let date = new Date(timeStamp);
440      console.info("get last sync time successfully:"+ date);
441    }
442  });
443  ```
444
445## CloudFileCache<sup>11+</sup>
446
447Provides APIs for the file manager application to download files from the Drive Kit to a local device.
448
449**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
450
451### construct<sup>11+</sup>
452
453constructor()
454
455A constructor used to create a **CloudFileCache** instance. Data is not shared between multiple instances.
456
457**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
458
459**Error codes**
460
461For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
462
463| ID                    | Error Message       |
464| ---------------------------- | ---------- |
465| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
466
467**Example**
468
469  ```ts
470  let fileCache = new cloudSync.CloudFileCache();
471  ```
472
473### on<sup>11+</sup>
474
475on(event: 'progress', callback: Callback\<DownloadProgress>): void
476
477Registers a listener for the download progress of a file from the Drive Kit.
478
479**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
480
481**Parameters**
482
483| Name    | Type  | Mandatory| Description|
484| ---------- | ------ | ---- | ---- |
485| event | string | Yes  | Event. The value is **progress**, which indicates the download progress event of a cloud file.|
486| callback | Callback\<[DownloadProgress](#downloadprogress11)> | Yes  | Callback for the download progress event of a file from the Drive Kit.|
487
488**Error codes**
489
490For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
491
492| ID                    | Error Message       |
493| ---------------------------- | ---------- |
494| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
495| 13600001  | IPC error. |
496
497**Example**
498
499  ```ts
500  import { BusinessError } from '@kit.BasicServicesKit';
501  let fileCache = new cloudSync.CloudFileCache();
502  let callback = (pg: cloudSync.DownloadProgress) => {
503    console.info("download state: " + pg.state);
504  };
505
506  try {
507    fileCache.on('progress', callback);
508  } catch (e) {
509    const error = e as BusinessError;
510    console.error(`Error code: ${error.code}, message: ${error.message}`);
511  }
512  ```
513
514### on<sup>20+</sup>
515
516on(event: 'batchDownload', callback: Callback&lt;MultiDownloadProgress&gt;): void
517
518Registers a listener for the batch download of a file from the Drive Kit.
519
520**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
521
522**Parameters**
523
524| Name  | Type                                                             | Mandatory| Description                                                         |
525| -------- | ----------------------------------------------------------------- | ---- | ------------------------------------------------------------- |
526| event    | string                                                            | Yes  | Event type. The value is **'batchDownload'**, indicating the batch download event.|
527| callback | Callback&lt;[MultiDownloadProgress](#multidownloadprogress20)&gt; | Yes  | Callback for the batch download event of a file from the Drive Kit.                               |
528
529**Error codes**
530
531For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
532
533| ID| Error Message|
534| -------- | -------- |
535| 13900020 | Invalid argument. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.                                                                     |
536| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
537
538**Example**
539
540  ```ts
541  import { BusinessError } from '@kit.BasicServicesKit';
542
543  let fileCache = new cloudSync.CloudFileCache();
544  let callback = (data: cloudSync.MultiDownloadProgress) => {
545    console.info(`Batch download progress: downloadedSize: ${data.downloadedSize}, totalSize: ${data.totalSize}`);
546    if (data.state == cloudSync.State.COMPLETED) {
547      console.info('Batch download finished.');
548    } else if (data.state == cloudSync.State.FAILED) {
549      console.info(`Batch download stopped, error type: ${data.errType}.`);
550    }
551  };
552
553  try {
554    fileCache.on('batchDownload', callback);
555  } catch (e) {
556    let error = e as BusinessError;
557    console.error(`Failed to register download callback, error code: ${error.code}, message: ${error.message}`);
558  }
559  ```
560
561### off<sup>11+</sup>
562
563off(event: 'progress', callback?: Callback\<DownloadProgress>): void
564
565Unregisters a listener for the download progress of a file from the Drive Kit.
566
567**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
568
569**Parameters**
570
571| Name    | Type  | Mandatory| Description|
572| ---------- | ------ | ---- | ---- |
573| event | string | Yes  | Event type. The value is **progress**, which indicates the sync progress event.|
574| callback | Callback\<[DownloadProgress](#downloadprogress11)> | No  | Callback for the download progress event of a file from the Drive Kit. If this parameter is not specified, this API unregisters all callbacks for the download progress event.|
575
576**Error codes**
577
578For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
579
580| ID                    | Error Message       |
581| ---------------------------- | ---------- |
582| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
583| 13600001  | IPC error. |
584
585**Example**
586
587  ```ts
588  import { BusinessError } from '@kit.BasicServicesKit';
589  let fileCache = new cloudSync.CloudFileCache();
590
591  let callback = (pg: cloudSync.DownloadProgress) => {
592    console.info("download state: " + pg.state);
593  }
594
595  try {
596    fileCache.on('progress', callback);
597    fileCache.off('progress', callback);
598  } catch (e) {
599    const error = e as BusinessError;
600    console.error(`Error code: ${error.code}, message: ${error.message}`);
601  }
602  ```
603
604### off<sup>20+</sup>
605
606off(event: 'batchDownload', callback?: Callback&lt;MultiDownloadProgress&gt;): void
607
608Unregisters a listener enabled by [on](#on20) for the batch download of a file from the Drive Kit.
609
610**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
611
612**Parameters**
613
614| Name  | Type                                                             | Mandatory| Description                                                                                                   |
615| -------- | ----------------------------------------------------------------- | ---- | ------------------------------------------------------------------------------------------------------- |
616| event    | string                                                            | Yes  | Event type. The value is **'batchDownload'**, indicating the batch download event.                                      |
617| callback | Callback&lt;[MultiDownloadProgress](#multidownloadprogress20)&gt; | No  | Callback for the batch download of a file from the Drive Kit. If this parameter is set, the specified callback will be canceled; otherwise, all currently subscribed callbacks of the same event type will be canceled.|
618
619**Error codes**
620
621For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
622
623| ID| Error Message|
624| -------- | -------- |
625| 13900020 | Invalid argument. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.                                                                     |
626| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
627
628**Example**
629
630  ```ts
631  import { BusinessError } from '@kit.BasicServicesKit';
632
633  let fileCache = new cloudSync.CloudFileCache();
634  let callback = (pg: cloudSync.MultiDownloadProgress) => {
635    console.info("download state: " + pg.state);
636  }
637
638  try {
639    fileCache.on('batchDownload', callback);
640    fileCache.off('batchDownload', callback);
641  } catch (e) {
642    let error = e as BusinessError;
643    console.error(`Failed to unregister download callback, error code: ${error.code}, message: ${error.message}`);
644  }
645  ```
646
647### start<sup>11+</sup>
648
649start(uri: string): Promise&lt;void&gt;
650
651Starts to download a file from the Drive Kit to the local device. This API uses a promise to return the result.
652
653**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
654
655**Parameters**
656
657| Name    | Type  | Mandatory| Description|
658| ---------- | ------ | ---- | ---- |
659| uri | string | Yes  | URI of the file to download.|
660
661**Return value**
662
663| Type                 | Description            |
664| --------------------- | ---------------- |
665| Promise&lt;void&gt; | Promise used to return the result.|
666
667**Error codes**
668
669For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
670
671| ID                    | Error Message       |
672| ---------------------------- | ---------- |
673| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
674| 13600001  | IPC error. |
675| 13900002 | No such file or directory. |
676| 13900025 | No space left on device. |
677| 14000002 | Invalid URI. |
678
679**Example**
680
681  ```ts
682  import { BusinessError } from '@kit.BasicServicesKit';
683  import { fileUri } from '@kit.CoreFileKit';
684  let fileCache = new cloudSync.CloudFileCache();
685  let path = "/data/storage/el2/cloud/1.txt";
686  let uri = fileUri.getUriFromPath(path);
687
688  try {
689    fileCache.on('progress', (pg: cloudSync.DownloadProgress) => {
690      console.info("download state:" + pg.state);
691    });
692  } catch (e) {
693    const error = e as BusinessError;
694    console.error(`Error code: ${error.code}, message: ${error.message}`);
695  }
696
697  fileCache.start(uri).then(() => {
698    console.info("start download successfully");
699  }).catch((err: BusinessError) => {
700    console.error("start download failed with error message: " + err.message + ", error code: " + err.code);
701  });
702  ```
703
704### start<sup>11+</sup>
705
706start(uri: string, callback: AsyncCallback&lt;void&gt;): void
707
708Starts to download a file from the Drive Kit to the local device. This API uses an asynchronous callback to return the result.
709
710**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
711
712**Parameters**
713
714| Name    | Type  | Mandatory| Description|
715| ---------- | ------ | ---- | ---- |
716| uri | string | Yes  | URI of the file to download.|
717| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
718
719**Error codes**
720
721For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
722
723| ID                    | Error Message       |
724| ---------------------------- | ---------- |
725| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
726| 13600001  | IPC error. |
727| 13900002 | No such file or directory. |
728| 13900025 | No space left on device. |
729| 14000002 | Invalid URI. |
730
731**Example**
732
733  ```ts
734  import { BusinessError } from '@kit.BasicServicesKit';
735  import { fileUri } from '@kit.CoreFileKit';
736  let fileCache = new cloudSync.CloudFileCache();
737  let path = "/data/storage/el2/cloud/1.txt";
738  let uri = fileUri.getUriFromPath(path);
739
740  fileCache.start(uri, (err: BusinessError) => {
741    if (err) {
742      console.error("start download failed with error message: " + err.message + ", error code: " + err.code);
743    } else {
744      console.info("start download successfully");
745    }
746  });
747  ```
748
749### startBatch<sup>20+</sup>
750
751startBatch(uris: Array&lt;string&gt;, fileType?: DownloadFileType): Promise&lt;number&gt;
752
753Starts the batch download of a file from the Drive Kit. This API uses a promise to return the result.
754
755Different batch download tasks can be distinguished by the task ID returned.
756
757**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
758
759**Parameters**
760
761| Name  | Type                                   | Mandatory| Description                                                               |
762| -------- | --------------------------------------- | ---- | ------------------------------------------------------------------- |
763| uris     | Array&lt;string&gt;                     | Yes  | URI list. A maximum of 400 URIs can be transferred at a time. An error (22400004) will be thrown if the number of URIs exceeds 400.|
764| fileType | [DownloadFileType](#downloadfiletype20) | No  | File type. The default value is **CONTENT**.                                    |
765
766**Return value**
767
768|         Type         |               Description                          |
769| --------------------- | ------------------------------------------- |
770| Promise&lt;number&gt; | Promise used to return the ID of the batch download task.|
771
772**Error codes**
773
774For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
775
776| ID| Error Message|
777| -------- | -------- |
778| 13600001 | IPC error. Possible causes: 1.IPC failed or timed out. 2.Failed to load the service.                                                                                              |
779| 13900020 | Invalid argument. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.                                                                     |
780| 22400004 | Exceed the maximum limit.                                                                                                                                                         |
781| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
782
783**Example**
784
785  ```ts
786  import { BusinessError } from '@kit.BasicServicesKit';
787
788  let fileCache = new cloudSync.CloudFileCache();
789  try {
790    fileCache.on('batchDownload', (pg: cloudSync.MultiDownloadProgress) => {
791      console.info(`batch download state: ${pg.state}`);
792    });
793  } catch (e) {
794    let error = e as BusinessError;
795    console.error(`Failed to unregister download callback, error code: ${error.code}, message: ${error.message}`);
796  }
797
798  let uriList: Array<string> = [];
799  fileCache.startBatch(uriList, cloudSync.DownloadFileType.CONTENT).then((downloadId: number) => {
800    console.info(`start batch download successfully, taskId: ${downloadId}`);
801  }).catch((err: BusinessError) => {
802    console.error(`start download failed with error message: ${err.message}, error code: ${err.code}`);
803  });
804  ```
805
806### stop<sup>11+</sup>
807
808stop(uri: string, needClean?: boolean): Promise&lt;void&gt;
809
810Stops downloading a file from the Drive Kit to the local device. This API uses a promise to return the result.
811
812When **stop()** is called, the current file download process terminates, and downloaded files are retained by default. You can call **start()** to resume the download.
813
814**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
815
816**Parameters**
817
818| Name    | Type  | Mandatory| Description|
819| ---------- | ------ | ---- | ---- |
820| uri | string | Yes  | URI of the file to download.|
821| needClean<sup>12+</sup> | boolean | No  | Whether to delete the downloaded files. The default value **false** means not to delete the downloaded files; the value **true** means the opposite.<br>This parameter is available since API version 12.|
822
823**Return value**
824
825| Type                 | Description            |
826| --------------------- | ---------------- |
827| Promise&lt;void&gt; | Promise used to return the result.|
828
829**Error codes**
830
831For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
832
833| ID                    | Error Message       |
834| ---------------------------- | ---------- |
835| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
836| 13600001  | IPC error. |
837| 13900002 | No such file or directory. |
838| 14000002 | Invalid URI. |
839
840**Example**
841
842  ```ts
843  import { BusinessError } from '@kit.BasicServicesKit';
844  import { fileUri } from '@kit.CoreFileKit';
845  let fileCache = new cloudSync.CloudFileCache();
846  let path = "/data/storage/el2/cloud/1.txt";
847  let uri = fileUri.getUriFromPath(path);
848
849  fileCache.stop(uri, true).then(() => {
850    console.info("stop download successfully");
851  }).catch((err: BusinessError) => {
852    console.error("stop download failed with error message: " + err.message + ", error code: " + err.code);
853  });
854  ```
855
856### stop<sup>11+</sup>
857
858stop(uri: string, callback: AsyncCallback&lt;void&gt;): void
859
860Stops downloading a file from the Drive Kit to the local device. This API uses an asynchronous callback to return the result.
861
862When **stop()** is called, the current file download process terminates, and downloaded files are retained. You can call **start()** to resume the download.
863
864**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
865
866**Parameters**
867
868| Name    | Type  | Mandatory| Description|
869| ---------- | ------ | ---- | ---- |
870| uri | string | Yes  | URI of the file to download.|
871| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
872
873**Error codes**
874
875For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
876
877| ID                    | Error Message       |
878| ---------------------------- | ---------- |
879| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
880| 13600001  | IPC error. |
881| 13900002 | No such file or directory. |
882| 14000002 | Invalid URI. |
883
884**Example**
885
886  ```ts
887  import { BusinessError } from '@kit.BasicServicesKit';
888  import { fileUri } from '@kit.CoreFileKit';
889  let fileCache = new cloudSync.CloudFileCache();
890  let path = "/data/storage/el2/cloud/1.txt";
891  let uri = fileUri.getUriFromPath(path);
892
893  fileCache.stop(uri, (err: BusinessError) => {
894    if (err) {
895      console.error("stop download failed with error message: " + err.message + ", error code: " + err.code);
896    } else {
897      console.info("stop download successfully");
898    }
899  });
900  ```
901
902### stopBatch<sup>20+</sup>
903
904stopBatch(downloadId: number, needClean?: boolean): Promise&lt;void&gt;
905
906Stops the batch download task enabled by [startBatch](#startbatch20) of a file from the Drive Kit. This API uses a promise to return the result.
907
908When **stopBatch()** is called, the batch download terminates. The **needClean** parameter determines whether to delete incompletely downloaded files.
909
910**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
911
912**Parameters**
913
914| Name    | Type   | Mandatory| Description                                                             |
915| ---------- | ------- | ---- | ----------------------------------------------------------------- |
916| downloadId | number  | Yes  | ID of the download task to be stopped.                                           |
917| needClean  | boolean | No  | Whether to delete incompletely downloaded files. The default value **false** means not to delete the files; the value **true** means the opposite.|
918
919**Return value**
920
921| Type               | Description                     |
922| ------------------- | ------------------------- |
923| Promise&lt;void&gt; | Promise that returns no value.|
924
925**Error codes**
926
927For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
928
929| ID| Error Message|
930| -------- | -------- |
931| 13600001 | IPC error. Possible causes: 1.IPC failed or timed out. 2.Failed to load the service.                                                                                              |
932| 13900020 | Invalid argument. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.                                                                     |
933| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
934
935**Example**
936
937  ```ts
938  import { BusinessError } from '@kit.BasicServicesKit';
939
940  let taskId = -1;
941  let uriList: Array<string> = [];
942  let fileCache = new cloudSync.CloudFileCache();
943  fileCache.startBatch(uriList, cloudSync.DownloadFileType.CONTENT).then((downloadId: number) => {
944    taskId = downloadId;
945    console.info("start batch download successfully");
946  }).catch((err: BusinessError) => {
947    console.error(`start batch download failed with error message: ${err.message}, error code: ${err.code}`);
948  });
949
950  let needStop = true;
951  if (needStop && taskId > 0) {
952    fileCache.stopBatch(taskId, true).then(() => {
953      console.info("stop batch download successfully");
954    }).catch((err: BusinessError) => {
955      console.error(`stop batch download failed with error message: ${err.message}, error code: ${err.code}`);
956    });
957  }
958  ```
959
960## DownloadErrorType<sup>11+</sup>
961
962Enumerates the device-cloud download error types.
963
964**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
965
966| Name|  Value|  Description|
967| ----- |  ---- |  ---- |
968| NO_ERROR |  0 | No error.|
969| UNKNOWN_ERROR |  1 | Unknown error.|
970| NETWORK_UNAVAILABLE |  2 | The network is unavailable.|
971| LOCAL_STORAGE_FULL |  3 | The local space is insufficient.|
972| CONTENT_NOT_FOUND |  4 | The file is not found in the cloud space.|
973| FREQUENT_USER_REQUESTS |  5 | The user requests are too frequent to respond.|
974
975## DownloadFileType<sup>20+</sup>
976
977Enumerates the download file types from the Drive Kit.
978
979**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
980
981| Name     | Value | Description               |
982| --------- | --- | ------------------- |
983| CONTENT   | 0   | Content file.  |
984| THUMBNAIL | 1   | Thumbnail file.|
985| LCD       | 2   | LCD file.      |
986
987## FailedFileInfo<sup>20+</sup>
988
989Represents a list of files that fail to be downloaded from the Drive Kit and failure causes.
990
991**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
992
993| Name | Type                                     | Mandatory| Description                  |
994| ----- | ----------------------------------------- | ---- | ---------------------- |
995| uri   | string                                    | Yes  | URI of the file that fails to be downloaded.     |
996| error | [DownloadErrorType](#downloaderrortype11) | Yes  | Error type of the file download failure.|
997
998
999## MultiDownloadProgress<sup>20+</sup>
1000
1001Represents the batch download progress of a file from the Drive Kit.
1002
1003### Property
1004
1005| Name           | Type                                     | Read-Only| Optional| Description                                                                                              |
1006| --------------- | ----------------------------------------- | ---- | ---- | -------------------------------------------------------------------------------------------------- |
1007| state           | [State](#state11)                         | No  | No  | Execution state of the batch download.                                                                            |
1008| taskId          | number                                    | No  | No  | ID of a batch download task. The value ranges from 0 to INT64_MAX. If the progress is abnormal, the value **-1** is returned.                                      |
1009| successfulCount | number                                    | No  | No  | Number of successfully downloaded files. The value ranges from 0 to 400. If the progress is abnormal, the value **-1** is returned.                                          |
1010| failedCount     | number                                    | No  | No  | Number of files that fail to be downloaded. The value ranges from 0 to 400. If the progress is abnormal, the value **-1** is returned.                                  |
1011| totalCount      | number                                    | No  | No  | Total number of files. The value ranges from 0 to 400. If the progress is abnormal, the value **-1** is returned.                                          |
1012| downloadedSize  | number                                    | No  | No  | Size of the downloaded file, in bytes. The value range is [0, INT64_MAX). If the progress is abnormal, the value **INT64_MAX** is returned.           |
1013| totalSize       | number                                    | No  | No  | Total size of the files to be downloaded, in bytes. The value range is [0, INT64_MAX). If the progress is abnormal, the value **INT64_MAX** is returned.|
1014| errType         | [DownloadErrorType](#downloaderrortype11) | No  | No  | Type of the error returned when the batch download fails.|
1015
1016**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1017
1018### getFailedFiles<sup>20+</sup>
1019
1020getFailedFiles(): Array&lt;FailedFileInfo&gt;
1021
1022Obtains the list of files that fail to be downloaded in batches.
1023
1024**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1025
1026**Return value**
1027
1028| Type                                            | Description                                               |
1029| ------------------------------------------------ | --------------------------------------------------- |
1030| Array&lt;[FailedFileInfo](#failedfileinfo20)&gt; | List of file URIs that fail to be downloaded and the corresponding error types.|
1031
1032**Error codes**
1033
1034For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
1035
1036| ID| Error Message|
1037| -------- | -------- |
1038| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
1039
1040**Example**
1041
1042  ```ts
1043  import { BusinessError } from '@kit.BasicServicesKit';
1044
1045  let taskId = -1;
1046  let failedList: Array<cloudSync.FailedFileInfo> = [];
1047  let fileCache = new cloudSync.CloudFileCache();
1048  let callback = (data: cloudSync.MultiDownloadProgress) => {
1049    console.info(`Batch download progress: downloadedSize: ${data.downloadedSize}, totalSize: ${data.totalSize}`);
1050    if (data.state == cloudSync.State.FAILED) {
1051      console.info(`Batch download stopped, error type: ${data.errType}.`);
1052      failedList = data.getFailedFiles();
1053    }
1054  };
1055
1056  try {
1057    fileCache.on('batchDownload', callback);
1058  } catch (e) {
1059    let error = e as BusinessError;
1060    console.error(`Failed to register download callback, error code: ${error.code}, message: ${error.message}`);
1061  }
1062
1063  let uriList: Array<string> = [];
1064  fileCache.startBatch(uriList, cloudSync.DownloadFileType.CONTENT).then((downloadId: number) => {
1065    taskId = downloadId;
1066    console.info("start batch download successfully");
1067  }).catch((err: BusinessError) => {
1068    console.error(`start batch download failed with error message: ${err.message}, error code: ${err.code}`);
1069  });
1070  ```
1071
1072### getSuccessfulFiles<sup>20+</sup>
1073
1074getSuccessfulFiles(): Array&lt;string&gt;
1075
1076Obtains the list of files that are successfully downloaded in batches.
1077
1078**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1079
1080**Return value**
1081
1082| Type               | Description                                 |
1083| ------------------- | ------------------------------------- |
1084| Array&lt;string&gt; | List of URIs of the files that are successfully downloaded. The value is an array.|
1085
1086**Error codes**
1087
1088For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
1089
1090| ID| Error Message|
1091| -------- | -------- |
1092| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
1093
1094**Example**
1095
1096  ```ts
1097  import { BusinessError } from '@kit.BasicServicesKit';
1098
1099  let finishedList: Array<string> = [];
1100  let fileCache = new cloudSync.CloudFileCache();
1101  let callback = (data: cloudSync.MultiDownloadProgress) => {
1102    console.info(`Batch download progress: downloadedSize: ${data.downloadedSize}, totalSize: ${data.totalSize}`);
1103    if (data.state == cloudSync.State.COMPLETED) {
1104      console.info(`Batch download stopped, error type: ${data.errType}.`);
1105      finishedList = data.getSuccessfulFiles();
1106    }
1107  };
1108
1109  try {
1110    fileCache.on('batchDownload', callback);
1111  } catch (e) {
1112    const error = e as BusinessError;
1113    console.error(`Failed to register download callback, error code: ${error.code}, message: ${error.message}`);
1114  }
1115
1116  let uriList: Array<string> = [];
1117  fileCache.startBatch(uriList, cloudSync.DownloadFileType.CONTENT).then((downloadId: number) => {
1118    console.info(`start batch download successfully, taskId: ${downloadId}`);
1119  }).catch((err: BusinessError) => {
1120    console.error(`start batch download failed with error message: ${err.message}, error code: ${err.code}`);
1121  });
1122  ```
1123
1124## cloudSync.registerChange<sup>12+</sup>
1125
1126registerChange(uri: string, recursion: boolean, callback: Callback&lt;ChangeData&gt;): void
1127
1128Subscribes to the change of a file.
1129
1130**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1131
1132**Parameters**
1133
1134| Name    | Type  | Mandatory| Description|
1135| ---------- | ------ | ---- | ---- |
1136| uri | string | Yes  | URI of the file to download.|
1137| recursion | boolean | Yes  | Whether to listen for the change of the URI, subfiles, and subdirectories. The value **true** means to listen for the change of the URI, subfiles, and subdirectories; the value **false** means to only listen for the change of the URI.|
1138| callback | Callback&lt;[ChangeData](#changedata12)&gt; | Yes  | Callback used to return the changed data.|
1139
1140**Error codes**
1141
1142For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
1143
1144| ID                    | Error Message       |
1145| ---------------------------- | ---------- |
1146| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
1147| 13900001  | Operation not permitted. |
1148| 13900002  | No such file or directory. |
1149| 13900012  | Permission denied. |
1150| 14000002  | Invalid URI. |
1151
1152**Example**
1153
1154  ```ts
1155  import { fileUri } from '@kit.CoreFileKit';
1156  let path = "/data/storage/el2/cloud/1.txt";
1157  let uri = fileUri.getUriFromPath(path);
1158  let onCallback1 = (changeData: cloudSync.ChangeData) => {
1159    if (changeData.type == cloudSync.NotifyType.NOTIFY_ADDED) {
1160      //file had added, do something
1161    } else if (changeData.type== cloudSync.NotifyType.NOTIFY_DELETED) {
1162      //file had removed, do something
1163    }
1164  }
1165  cloudSync.registerChange(uri, false, onCallback1);
1166  // Unregister the listener.
1167  cloudSync.unregisterChange(uri);
1168  ```
1169
1170## cloudSync.unregisterChange<sup>12+</sup>
1171
1172unregisterChange(uri: string): void
1173
1174Unsubscribes from the change of a file.
1175
1176**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1177
1178**Parameters**
1179
1180| Name    | Type  | Mandatory| Description|
1181| ---------- | ------ | ---- | ---- |
1182| uri | string | Yes  | URI of the file to download.|
1183
1184**Error codes**
1185
1186For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [File Management Error Codes](errorcode-filemanagement.md).
1187
1188| ID                    | Error Message       |
1189| ---------------------------- | ---------- |
1190| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
1191| 13900001  | Operation not permitted. |
1192| 13900002  | No such file or directory. |
1193| 13900012  | Permission denied. |
1194| 14000002  | Invalid URI. |
1195
1196**Example**
1197
1198  ```ts
1199  import { fileUri } from '@kit.CoreFileKit';
1200  let path = "/data/storage/el2/cloud/1.txt";
1201  let uri = fileUri.getUriFromPath(path);
1202  let onCallback1 = (changeData: cloudSync.ChangeData) => {
1203    if (changeData.type == cloudSync.NotifyType.NOTIFY_ADDED) {
1204      //file had added, do something
1205    } else if (changeData.type== cloudSync.NotifyType.NOTIFY_DELETED) {
1206      //file had removed, do something
1207    }
1208  }
1209  cloudSync.registerChange(uri, false, onCallback1);
1210  // Unregister the listener.
1211  cloudSync.unregisterChange(uri);
1212  ```
1213
1214## NotifyType<sup>12+</sup>
1215
1216Enumerates the data change types.
1217
1218**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1219
1220| Name|  Value|  Description|
1221| ----- |  ---- |  ---- |
1222| NOTIFY_ADDED |  0 | A file is created.|
1223| NOTIFY_MODIFIED |  1 | The file is modified.|
1224| NOTIFY_DELETED |  2 | The file is deleted.|
1225| NOTIFY_RENAMED |  3 | The file is renamed or moved.|
1226
1227## ChangeData<sup>12+</sup>
1228
1229Represents the data change information.
1230
1231**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1232
1233| Name    | Type  | Mandatory| Description|
1234| ---------- | ------ | ---- | ---- |
1235| type | [NotifyType](#notifytype12) | Yes  | Type of the data change.|
1236| isDirectory | Array&lt;boolean&gt; | Yes  | Whether the URIs with data changed are of directories. The value **true** means the URIs are of directories; the value **false** means the opposite.|
1237| uris | Array&lt;string&gt; | Yes  | List of URIs whose data needs to be changed.|
1238
1239
1240## HistoryVersion<sup>20+</sup>
1241
1242Represents the historical version information of the device-cloud file when the [gethistoryversionlist](#gethistoryversionlist20) method of the [FileVersion](#fileversion20) class is called.
1243
1244### Property
1245
1246**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1247
1248| Name    | Type  | Read-Only| Optional| Description|
1249| ---------- | ------ | ---- | ---- | ---- |
1250| editedTime | number | No  | No  | Time when the file content is modified.|
1251| fileSize | number | No  | No  | File size in bytes.|
1252| versionId | string | No  | No  | File version.|
1253| originalFileName | string | No  | No  | File name of the current version.|
1254| sha256 | string | No  | No  | Hash value of the file content of the current version.|
1255| autoResolved | boolean | No  | No  | Whether the current version is the one where conflicts were automatically resolved.<br>When the application is set to manually resolve conflicts, **false** is returned by default, which is meaningless.<br>When the application is set to automatically resolve conflicts, the device side automatically resolves conflicts. The value **true** means conflicts exist in the current version and have been automatically resolved by the device-cloud service; the value **false** means no conflict exists and conflicts are not automatically resolved.|
1256
1257**NOTE**<br>The current device-cloud sync provides an automatic conflict resolution mechanism for cases where a file is modified simultaneously on both the local device and another device. To enable users to manually resolve the conflict, add the **cloudkit_config.json** file to the **/entry/src/main/resources/base/profile** directory of the project and set **manualConflictResolutionEnable** to **true**. Otherwise, skip this step or set **manualConflictResolutionEnable** to **false**.
1258
1259Example of configuring the **cloudkit_config.json** file:
1260
1261  ```json
1262  {
1263    "cloudKitConfig": {
1264      "cloudFileSyncConfig": {
1265        "manualConflictResolutionEnable": "true"
1266      }
1267    }
1268  }
1269  ```
1270
1271## VersionDownloadProgress<sup>20+</sup>
1272
1273Represents the download state and progress information of historical version files when the [downloadHistoryVersion](#downloadhistoryversion20) method of the [FileVersion](#fileversion20) class is called.
1274
1275### Property
1276
1277**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1278
1279| Name| Type| Read-Only| Optional| Description|
1280| ---------- | ------ | ---- | ---- | ---- |
1281| state | [State](#state11) | No  | No  | Download state of the cloud file of the selected version.|
1282| progress | number | No  | No  | Download progress.|
1283| errType | [DownloadErrorType](#downloaderrortype11) | No  | No  | Type of the error that occurs when the download fails.|
1284
1285## FileVersion<sup>20+</sup>
1286
1287Represents the device-cloud file version management class. It allows you to manage historical versions of client-cloud files, obtain the list of historical versions, download historical versions to the local device, replace the current local file with a historical version file, and query and remove conflict flags for version conflicts.
1288
1289### construct<sup>20+</sup>
1290
1291constructor()
1292
1293A constructor used to create a **FileVersion** instance.
1294
1295**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1296
1297**Error codes**
1298
1299For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
1300
1301| ID                    | Error Message       |
1302| ---------------------------- | ---------- |
1303| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
1304
1305**Example**
1306
1307  ```ts
1308  let fileVersion = new cloudSync.FileVersion();
1309  ```
1310
1311### getHistoryVersionList<sup>20+</sup>
1312
1313getHistoryVersionList(uri: string, versionNumLimit: number): Promise&lt;Array&lt;HistoryVersion&gt;&gt;
1314
1315Obtains the list of historical versions. The length of the returned list can be limited. If the number of cloud versions is less than the specified limit, the list will be returned with the actual number of versions. This API uses a promise to return the result.
1316
1317**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1318
1319**Parameters**
1320
1321| Name    | Type  | Mandatory| Description|
1322| ---------- | ------ | ---- | ---- |
1323| uri | string | Yes  |  File URI.|
1324| versionNumLimit | number | Yes| Length limit of the historical version list.|
1325
1326**Return value**
1327
1328| Type | Description|
1329| ------ | ---- |
1330| Promise&lt;Array&lt;[HistoryVersion](#historyversion20)&gt;&gt; | Promise used to return the list of historical versions.|
1331
1332**Error codes**
1333
1334For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
1335
1336| ID                    | Error Message       |
1337| ---------------------------- | ---------- |
1338| 13600001 | IPC error. Possible causes: 1.IPC failed or timed out. 2.Failed to load the service. |
1339| 13900002 | No such file or directory. |
1340| 13900010 | Try again. |
1341| 13900012 | Permission denied by the file system. |
1342| 13900020 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1343| 14000002 | Invalid URI. |
1344| 22400002 | Network unavailable. |
1345| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
1346
1347**Example**
1348
1349  ```ts
1350  import { fileUri } from '@kit.CoreFileKit';
1351  import { BusinessError } from '@kit.BasicServicesKit';
1352
1353  let fileVersion = new cloudSync.FileVersion();
1354
1355  let path = "/data/storage/el2/cloud/1.txt";
1356  let uri = fileUri.getUriFromPath(path);
1357  let limit = 10;
1358
1359  fileVersion.getHistoryVersionList(uri, limit).then((versionList: Array<cloudSync.HistoryVersion>) => {
1360    for(let i = 0, len = versionList.length; i < len; i++) {
1361      console.info("get history versionId: " + versionList[i].versionId);
1362    }
1363  }).catch((err: BusinessError) => {
1364    console.error("get history version failed with error message: " + err.message + ", error code: " + err.code);
1365  });
1366  ```
1367
1368### downloadHistoryVersion<sup>20+</sup>
1369
1370downloadHistoryVersion(uri: string, versionId: string, callback: Callback&lt;[VersionDownloadProgress](#versiondownloadprogress20)&gt;): Promise&lt;string&gt;
1371
1372Obtains the content of a file of a specified version based on the version number. You can download a file of a specified version from the cloud to a temporary local path. The application determines whether to replace the original file with the temporary file, or retain or delete the temporary file. This API uses a promise to return the result.
1373
1374**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1375
1376**Parameters**
1377
1378| Name    | Type  | Mandatory| Description|
1379| ---------- | ------ | ---- | ---- |
1380| uri | string | Yes  |  File URI.|
1381| versionId | string | Yes| Version ID of a file. The format is returned by the [gethistoryversionlist](#gethistoryversionlist20) API.|
1382| callback | Callback&lt;[VersionDownloadProgress](#versiondownloadprogress20)&gt; | Yes| Callback for download progress.|
1383
1384**Return value**
1385
1386| Type | Description|
1387| ------ | ---- |
1388| Promise&lt;string&gt; | Promise used to return the URI of the temporary file of a historical version.|
1389
1390**Error codes**
1391
1392For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
1393
1394| ID                    | Error Message       |
1395| ---------------------------- | ---------- |
1396| 13600001 | IPC error. Possible causes: 1.IPC failed or timed out. 2.Failed to load the service. |
1397| 13900002 | No such file or directory. |
1398| 13900010 | Try again. |
1399| 13900012 | Permission denied by the file system. |
1400| 13900020 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1401| 14000002 | Invalid URI. |
1402| 22400002 | Network unavailable. |
1403| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
1404
1405**Example**
1406
1407  ```ts
1408  import { fileUri } from '@kit.CoreFileKit';
1409  import { BusinessError } from '@kit.BasicServicesKit';
1410
1411  let fileVersion = new cloudSync.FileVersion();
1412
1413  let path = "/data/storage/el2/cloud/1.txt";
1414  let uri = fileUri.getUriFromPath(path);
1415  let versionId = '123456'; // The format returned by the getHistoryVersionList method is used as an example.
1416
1417  let callback = (data: cloudSync.VersionDownloadProgress) => {
1418    if (data.state == cloudSync.State.RUNNING) {
1419      console.info("download progress: " + data.progress);
1420    } else if (data.state == cloudSync.State.FAILED) {
1421      console.info("download failed errType: " + data.errType);
1422    } else if (data.state == cloudSync.State.COMPLETED) {
1423      console.info("download version file success");
1424    }
1425  };
1426
1427  fileVersion.downloadHistoryVersion(uri, versionId, callback).then((fileUri: string) => {
1428    console.info("success to begin download, downloadFileUri: " + fileUri);
1429  }).catch((err: BusinessError) => {
1430    console.error("download history version file failed with error message: " + err.message + ", error code: " + err.code);
1431  });
1432  ```
1433
1434### replaceFileWithHistoryVersion<sup>20+</sup>
1435
1436replaceFileWithHistoryVersion(originalUri: string, versionUri: string): Promise&lt;void&gt;
1437
1438Replaces the local file with the file of a historical version. Before replacement, call the [downloadHistoryVersion](#downloadhistoryversion20) method to download the selected historical version and obtain its version URI. If this API is called directly without prior download or the version URI is invalid, an exception will be thrown. Once replacement is complete, the temporary file will be automatically deleted. This API uses a promise to return the result.
1439
1440**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1441
1442**Parameters**
1443
1444| Name    | Type  | Mandatory| Description|
1445| ---------- | ------ | ---- | ---- |
1446| originalUri | string | Yes  |  URI of the local file.|
1447| versionUri | string | Yes| URI of the historical file.|
1448
1449**Return value**
1450
1451| Type | Description|
1452| ------ | ---- |
1453| Promise&lt;void&gt; | Promise that returns no value.|
1454
1455**Error codes**
1456
1457For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
1458
1459| ID                    | Error Message       |
1460| ---------------------------- | ---------- |
1461| 13600001 | IPC error. Possible causes: 1.IPC failed or timed out. 2.Failed to load the service. |
1462| 13900002 | No such file or directory. |
1463| 13900005 | I/O error. |
1464| 13900008 | Bad file descriptor. |
1465| 13900010 | Try again. |
1466| 13900012 | Permission denied by the file system. |
1467| 13900020 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1468| 14000002 | Invalid URI. |
1469| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
1470| 22400007 | Version file not exist. |
1471
1472**Example**
1473
1474  ```ts
1475  import { fileUri } from '@kit.CoreFileKit';
1476  import { BusinessError } from '@kit.BasicServicesKit';
1477
1478  let fileVersion = new cloudSync.FileVersion();
1479
1480  let path = "/data/storage/el2/cloud/1.txt";
1481  let uri = fileUri.getUriFromPath(path);
1482  let versionId = '123456'; // The format returned by the getHistoryVersionList method is used as an example.
1483
1484  let callback = (data: cloudSync.VersionDownloadProgress) => {
1485    if (data.state == cloudSync.State.RUNNING) {
1486      console.info("download progress: " + data.progress);
1487    } else if (data.state == cloudSync.State.FAILED) {
1488      console.info("download failed errType: " + data.errType);
1489    } else if (data.state == cloudSync.State.COMPLETED) {
1490      console.info("download version file success");
1491    }
1492  };
1493
1494  let versionUri = "";
1495  fileVersion.downloadHistoryVersion(uri, versionId, callback).then((fileUri: string) => {
1496    versionUri = fileUri;
1497    console.info("success to begin download, downloadFileUri: " + fileUri);
1498  }).catch((err: BusinessError) => {
1499    console.error(`download history version file failed with error message: ${err.message}, error code: ${err.code}`);
1500  });
1501  fileVersion.replaceFileWithHistoryVersion(uri, versionUri).then(() => {
1502    console.info("replace file with history version success.");
1503  }).catch((err: BusinessError) => {
1504    console.error("replace file with history version filed with error message: " + err.message + ", error code: " + err.code);
1505  });
1506  ```
1507
1508### isFileConflict<sup>20+</sup>
1509
1510isFileConflict(uri: string): Promise&lt;boolean&gt;
1511
1512Obtains the version conflict flag of a local file. This API uses a promise to return the result. This API takes effect only when the application is configured for manual conflict resolution. Otherwise, conflicts are automatically resolved during synchronization, and the return value will be **false**.
1513
1514Once the application is configured for manual conflict resolution, calling this API returns whether the current local file conflicts with the cloud file. The application then prompts the user to handle the conflict. After the conflict is resolved, you need to call the [clearfileconflict](#clearfileconflict20) method to clear the conflict flag and synchronize the file to the cloud.
1515
1516**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1517
1518**Parameters**
1519
1520| Name    | Type  | Mandatory| Description|
1521| ---------- | ------ | ---- | ---- |
1522| uri | string | Yes  |  File URI.|
1523
1524**Return value**
1525
1526| Type | Description|
1527| ------ | ---- |
1528| Promise&lt;boolean&gt; | Promise used to return the conflict flag between the local file and the cloud file. The value **true** indicates that the local file conflicts with the cloud file, and the value **false** indicates the opposite.|
1529
1530**Error codes**
1531
1532For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
1533
1534| ID                    | Error Message       |
1535| ---------------------------- | ---------- |
1536| 13600001 | IPC error. Possible causes: 1.IPC failed or timed out. 2.Failed to load the service. |
1537| 13900002 | No such file or directory. |
1538| 13900010 | Try again. |
1539| 13900012 | Permission denied by the file system. |
1540| 13900020 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1541| 14000002 | Invalid URI. |
1542| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
1543
1544**Example**
1545
1546  ```ts
1547  import { fileUri } from '@kit.CoreFileKit';
1548  import { BusinessError } from '@kit.BasicServicesKit';
1549
1550  let fileVersion = new cloudSync.FileVersion();
1551
1552  let path = "/data/storage/el2/cloud/1.txt";
1553  let uri = fileUri.getUriFromPath(path);
1554
1555  fileVersion.isFileConflict(uri).then((isConflict: boolean) => {
1556    console.info("current file is conflict: " + isConflict);
1557  }).catch((err: BusinessError) => {
1558    console.error("get current file conflict flag failed with error message: " + err.message + ", error code: " + err.code);
1559  });
1560  ```
1561
1562### clearFileConflict<sup>20+</sup>
1563
1564clearFileConflict(uri: string): Promise&lt;void&gt;
1565
1566Clears the version conflict flag of the local file. If a conflict occurs, you need to call this API to clear the conflict flag after the conflict is resolved locally and trigger automatic synchronization. This API uses a promise to return the result.
1567
1568**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
1569
1570**Parameters**
1571
1572| Name    | Type  | Mandatory| Description|
1573| ---------- | ------ | ---- | ---- |
1574| uri | string | Yes  |  URI of the file for which the conflict flag is to be cleared.|
1575
1576**Return value**
1577
1578| Type | Description|
1579| ------ | ---- |
1580| Promise&lt;void&gt; | Promise that returns no value.|
1581
1582**Error codes**
1583
1584For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
1585
1586| ID                    | Error Message       |
1587| ---------------------------- | ---------- |
1588| 13600001 | IPC error. Possible causes: 1.IPC failed or timed out. 2.Failed to load the service. |
1589| 13900002 | No such file or directory. |
1590| 13900010 | Try again. |
1591| 13900012 | Permission denied by the file system. |
1592| 13900020 | Parameter error. Possible causes: 1.Mandatory parameters are left unspecified; 2.Incorrect parameter types. |
1593| 14000002 | Invalid URI. |
1594| 22400005 | Inner error. Possible causes: 1.Failed to access the database or execute the SQL statement. 2.System error, such as a null pointer, insufficient memory or a JS engine exception. |
1595
1596**Example**
1597
1598  ```ts
1599  import { fileUri } from '@kit.CoreFileKit';
1600  import { BusinessError } from '@kit.BasicServicesKit';
1601
1602  let fileVersion = new cloudSync.FileVersion();
1603
1604  let path = "/data/storage/el2/cloud/1.txt";
1605  let uri = fileUri.getUriFromPath(path);
1606
1607  let isConflict = false;
1608  fileVersion.isFileConflict(uri).then((isConflictRet: boolean) => {
1609    isConflict = isConflictRet;
1610    console.info("current file is conflict: " + isConflictRet);
1611  }).catch((err: BusinessError) => {
1612    console.error(`get current file conflict flag failed with error message: ${err.message}, error code: ${err.code}`);
1613  });
1614  fileVersion.clearFileConflict(uri).then(() => {
1615    console.info("clean file conflict flag success");
1616  }).catch((err: BusinessError) => {
1617    console.error("clean file conflict flag failed with error message: " + err.message + ", error code: " + err.code);
1618  });
1619  ```
1620