• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.file.cloudSync (Device-Cloud Synchronization)
2
3The **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.
4
5> **NOTE**
6>
7> 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.
8
9## Modules to Import
10
11```ts
12import { cloudSync } from '@kit.CoreFileKit';
13```
14## SyncState<sup>12+</sup>
15
16Enumerates the device-cloud sync states.
17
18> **NOTE**
19>
20> 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.
21
22**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
23
24| Name|  Value|  Description|
25| ----- |  ---- |  ---- |
26| UPLOADING |  0 | The file is being uploaded.|
27| UPLOAD_FAILED |  1 | Upload failed.|
28| DOWNLOADING |  2 | The file is being downloaded.|
29| DOWNLOAD_FAILED |  3 | Download failed.|
30| COMPLETED |  4 | Sync completed.|
31| STOPPED |  5 | Sync stopped.|
32
33## ErrorType<sup>12+</sup>
34
35Enumerates the device-cloud sync errors.
36
37- 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.
38- 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.
39- 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()**.
40- 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.
41
42**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
43
44| Name|  Value|  Description|
45| ----- |  ---- |  ---- |
46| NO_ERROR |  0 | No error.|
47| NETWORK_UNAVAILABLE |  1 | No network is available.|
48| WIFI_UNAVAILABLE |  2 | Wi-Fi is unavailable.|
49| BATTERY_LEVEL_LOW |  3 | The battery level is lower than 10%.|
50| BATTERY_LEVEL_WARNING |  4 | The battery level is lower than 15%.|
51| CLOUD_STORAGE_FULL |  5 | The cloud space is insufficient.|
52| LOCAL_STORAGE_FULL |  6 | The local space is insufficient.|
53| DEVICE_TEMPERATURE_TOO_HIGH |  7 | The device temperature is too high.|
54
55## SyncProgress<sup>12+</sup>
56
57Represents information about the device-cloud sync progress.
58
59### Property
60
61**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
62
63| Name    | Type  | Mandatory| Description|
64| ---------- | ------ | ---- | ---- |
65| state | [SyncState](#syncstate12) | Yes  | Device-cloud sync state.|
66| error | [ErrorType](#errortype12) | Yes  | Sync error.|
67
68## State<sup>11+</sup>
69
70Enumerates the download states of a cloud file.
71
72**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
73
74| Name|  Value|  Description|
75| ----- |  ---- |  ---- |
76| RUNNING |  0 | The cloud file is being downloaded.|
77| COMPLETED |  1 | The cloud file download is complete.|
78| FAILED |  2 | The cloud file download failed.|
79| STOPPED |  3 | The cloud file download is stopped.|
80
81## DownloadProgress<sup>11+</sup>
82
83Represents information about the download progress of a cloud file.
84
85### Property
86
87**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
88
89| Name    | Type  | Mandatory| Description|
90| ---------- | ------ | ---- | ---- |
91| state | [State](#state11) | Yes  | File download state.|
92| processed | number | Yes  | Size of the data downloaded.|
93| size | number | Yes  | Size of the cloud file.|
94| uri | string | Yes  | URI of the cloud file.|
95| error | [DownloadErrorType](#downloaderrortype11) | Yes  | Download error type.|
96
97## FileSync<sup>12+</sup>
98
99Provides 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.
100
101**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
102
103### constructor<sup>12+</sup>
104
105constructor()
106
107A constructor used to create a **FileSync** instance.
108
109**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
110
111**Error codes**
112
113For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
114
115| ID                    | Error Message       |
116| ---------------------------- | ---------- |
117| 202 | Permission verification failed, application which is not a system application uses system API. |
118| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
119
120**Example**
121
122  ```ts
123  let fileSync = new cloudSync.FileSync()
124  ```
125
126### on<sup>12+</sup>
127
128on(event: 'progress', callback: Callback\<SyncProgress>): void
129
130Registers a listener for the device-cloud sync progress.
131
132**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
133
134**Parameters**
135
136| Name    | Type  | Mandatory| Description|
137| ---------- | ------ | ---- | ---- |
138| event | string | Yes  | Event type. The value is **progress**, which indicates the sync progress event.|
139| callback | Callback\<[SyncProgress](#syncprogress12)> | Yes  | Callback used to return the sync progress information.|
140
141**Error codes**
142
143For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
144
145| ID                    | Error Message       |
146| ---------------------------- | ---------- |
147| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
148| 202 | Permission verification failed, application which is not a system application uses system API. |
149| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
150| 13600001  | IPC error. |
151
152**Example**
153
154  ```ts
155  let fileSync = new cloudSync.FileSync();
156  let callback = (pg: cloudSync.SyncProgress) => {
157    console.info("file sync state: " + pg.state + "error type:" + pg.error);
158  }
159
160  fileSync.on('progress', callback);
161  ```
162
163### off<sup>12+</sup>
164
165off(event: 'progress', callback?: Callback\<SyncProgress>): void
166
167Unregisters all listeners for the device-cloud sync progress.
168
169**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
170
171**Parameters**
172
173| Name    | Type  | Mandatory| Description|
174| ---------- | ------ | ---- | ---- |
175| event | string | Yes  | Event type. The value is **progress**, which indicates the sync progress event.|
176| callback | Callback\<[SyncProgress](#syncprogress12)> |  No  | Callback used to return the sync progress information.|
177
178**Error codes**
179
180For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
181
182| ID                    | Error Message       |
183| ---------------------------- | ---------- |
184| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
185| 202 | Permission verification failed, application which is not a system application uses system API. |
186| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
187| 13600001  | IPC error. |
188
189**Example**
190
191  ```ts
192  let fileSync = new cloudSync.FileSync();
193
194  let callback = (pg: cloudSync.SyncProgress) => {
195    console.info("file sync state: " + pg.state + "error type:" + pg.error);
196  }
197
198  fileSync.on('progress', callback);
199
200  fileSync.off('progress', callback);
201  ```
202
203### start<sup>12+</sup>
204
205start(): Promise&lt;void&gt;
206
207Starts device-cloud sync of a file in the Drive Kit. This API uses a promise to return the result.
208
209**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
210
211**Return value**
212
213| Type                 | Description            |
214| --------------------- | ---------------- |
215| Promise&lt;void&gt; | Promise that returns no value.|
216
217**Error codes**
218
219For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
220
221| ID                    | Error Message       |
222| ---------------------------- | ---------- |
223| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
224| 202 | Permission verification failed, application which is not a system application uses system API. |
225| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
226| 13600001  | IPC error. |
227| 22400001  | Cloud status not ready. |
228| 22400002  | Network unavailable. |
229| 22400003  | Low battery level. |
230
231**Example**
232
233  ```ts
234  import { BusinessError } from '@kit.BasicServicesKit';
235  let fileSync = new cloudSync.FileSync();
236
237  let callback = (pg: cloudSync.SyncProgress) => {
238    console.info("file sync state: " + pg.state + "error type:" + pg.error);
239  }
240
241  fileSync.on('progress', callback);
242
243  fileSync.start().then(() => {
244    console.info("start sync successfully");
245  }).catch((err: BusinessError) => {
246    console.error("start sync failed with error message: " + err.message + ", error code: " + err.code);
247  });
248  ```
249
250### start<sup>12+</sup>
251
252start(callback: AsyncCallback&lt;void&gt;): void
253
254Starts device-cloud sync of a file in the Drive Kit. This API uses an asynchronous callback to return the result.
255
256**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
257
258**Parameters**
259
260| Name    | Type  | Mandatory| Description|
261| ---------- | ------ | ---- | ---- |
262| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
263
264**Error codes**
265
266For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
267
268| ID                    | Error Message       |
269| ---------------------------- | ---------- |
270| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
271| 202 | Permission verification failed, application which is not a system application uses system API. |
272| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
273| 13600001  | IPC error. |
274| 22400001  | Cloud status not ready. |
275| 22400002  | Network unavailable. |
276| 22400003  | Low battery level. |
277
278**Example**
279
280  ```ts
281  import { BusinessError } from '@kit.BasicServicesKit';
282  let fileSync = new cloudSync.FileSync();
283
284  fileSync.start((err: BusinessError) => {
285    if (err) {
286      console.error("start sync failed with error message: " + err.message + ", error code: " + err.code);
287    } else {
288      console.info("start sync successfully");
289    }
290  });
291  ```
292
293### stop<sup>12+</sup>
294
295stop(): Promise&lt;void&gt;
296
297Stops device-cloud sync of the file in the Drive Kit. This API uses a promise to return the result.
298
299Calling **stop** will stop the sync process. To resume the sync, call [start](#start12).
300
301**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
302
303**Return value**
304
305| Type                 | Description            |
306| --------------------- | ---------------- |
307| Promise&lt;void&gt; | Promise used to return the result.|
308
309**Error codes**
310
311For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
312
313| ID                    | Error Message       |
314| ---------------------------- | ---------- |
315| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
316| 202 | Permission verification failed, application which is not a system application uses system API. |
317| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
318| 13600001  | IPC error. |
319
320**Example**
321
322  ```ts
323  import { BusinessError } from '@kit.BasicServicesKit';
324  let fileSync = new cloudSync.FileSync();
325
326  fileSync.stop().then(() => {
327    console.info("stop sync successfully");
328  }).catch((err: BusinessError) => {
329    console.error("stop sync failed with error message: " + err.message + ", error code: " + err.code);
330  });
331  ```
332
333### stop<sup>12+</sup>
334
335stop(callback: AsyncCallback&lt;void&gt;): void
336
337Stops device-cloud sync of the file in the Drive Kit. This API uses an asynchronous callback to return the result.
338
339Calling **stop** will stop the sync process. To resume the sync, call [start](#start12).
340
341**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
342
343**Parameters**
344
345| Name    | Type  | Mandatory| Description|
346| ---------- | ------ | ---- | ---- |
347| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
348
349**Error codes**
350
351For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
352
353| ID                    | Error Message       |
354| ---------------------------- | ---------- |
355| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
356| 202 | Permission verification failed, application which is not a system application uses system API. |
357| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
358| 13600001  | IPC error. |
359
360**Example**
361
362  ```ts
363  import { BusinessError } from '@kit.BasicServicesKit';
364  let fileSync = new cloudSync.FileSync();
365
366  fileSync.stop((err: BusinessError) => {
367    if (err) {
368      console.error("stop sync failed with error message: " + err.message + ", error code: " + err.code);
369    } else {
370      console.info("stop sync successfully");
371    }
372  });
373  ```
374
375### getLastSyncTime<sup>12+</sup>
376
377getLastSyncTime(): Promise&lt;number&gt;
378
379Obtains the last sync time. This API uses a promise to return the result.
380
381**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
382
383**Return value**
384
385| Type                 | Description            |
386| --------------------- | ---------------- |
387| Promise&lt;number&gt; | Promise used to return the last sync time obtained.|
388
389**Error codes**
390
391For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
392
393| ID                    | Error Message       |
394| ---------------------------- | ---------- |
395| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
396| 202 | Permission verification failed, application which is not a system application uses system API. |
397| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
398| 13600001  | IPC error. |
399
400**Example**
401
402  ```ts
403  import { BusinessError } from '@kit.BasicServicesKit';
404  let fileSync = new cloudSync.FileSync();
405
406  fileSync.getLastSyncTime().then((timeStamp: number) => {
407    let date = new Date(timeStamp);
408    console.info("get last sync time successfully:"+ date);
409  }).catch((err: BusinessError) => {
410    console.error("get last sync time failed with error message: " + err.message + ", error code: " + err.code);
411  });
412
413  ```
414
415### getLastSyncTime<sup>12+</sup>
416
417getLastSyncTime(callback: AsyncCallback&lt;number&gt;): void
418
419Obtains the last sync time. This API uses an asynchronous callback to return the result.
420
421**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
422
423**Parameters**
424
425| Name    | Type  | Mandatory| Description|
426| ---------- | ------ | ---- | ---- |
427| callback | AsyncCallback&lt;number&gt; | Yes  | Callback used to return the last sync time obtained.|
428
429**Error codes**
430
431For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
432
433| ID                    | Error Message       |
434| ---------------------------- | ---------- |
435| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
436| 202 | Permission verification failed, application which is not a system application uses system API. |
437| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
438| 13600001  | IPC error. |
439
440**Example**
441
442  ```ts
443  import { BusinessError } from '@kit.BasicServicesKit';
444  let fileSync = new cloudSync.FileSync();
445
446  fileSync.getLastSyncTime((err: BusinessError, timeStamp: number) => {
447    if (err) {
448      console.error("get last sync time with error message: " + err.message + ", error code: " + err.code);
449    } else {
450      let date = new Date(timeStamp);
451      console.info("get last sync time successfully:"+ date);
452    }
453  });
454  ```
455
456## CloudFileCache<sup>11+</sup>
457
458Provides APIs for the file manager application to download files from the Drive Kit to a local device.
459
460**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
461
462### construct<sup>11+</sup>
463
464constructor()
465
466A constructor used to create a **CloudFileCache** instance.
467
468**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
469
470**Error codes**
471
472| ID                    | Error Message       |
473| ---------------------------- | ---------- |
474| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. |
475
476**Example**
477
478  ```ts
479  let fileCache = new cloudSync.CloudFileCache();
480  ```
481
482### on<sup>11+</sup>
483
484on(event: 'progress', callback: Callback\<DownloadProgress>): void
485
486Registers a listener for the download progress of a file from the Drive Kit.
487
488**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
489
490**Parameters**
491
492| Name    | Type  | Mandatory| Description|
493| ---------- | ------ | ---- | ---- |
494| event | string | Yes  | Event. The value is **progress**, which indicates the download progress event of a cloud file.|
495| callback | Callback\<[DownloadProgress](#downloadprogress11)> | Yes  | Callback for the download progress event of a file from the Drive Kit.|
496
497**Error codes**
498
499For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
500
501| ID                    | Error Message       |
502| ---------------------------- | ---------- |
503| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
504| 13600001  | IPC error. |
505
506**Example**
507
508  ```ts
509  import { BusinessError } from '@kit.BasicServicesKit';
510  let fileCache = new cloudSync.CloudFileCache();
511  let callback = (pg: cloudSync.DownloadProgress) => {
512    console.info("download state: " + pg.state);
513  };
514
515  try {
516    fileCache.on('progress', callback);
517  } catch (e) {
518    const error = e as BusinessError;
519    console.error(`Error code: ${error.code}, message: ${error.message}`);
520  }
521  ```
522
523### off<sup>11+</sup>
524
525off(event: 'progress', callback?: Callback\<DownloadProgress>): void
526
527Unregisters a listener for the download progress of a file from the Drive Kit.
528
529**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
530
531**Parameters**
532
533| Name    | Type  | Mandatory| Description|
534| ---------- | ------ | ---- | ---- |
535| event | string | Yes  | Event type. The value is **progress**, which indicates the sync progress event.|
536| 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.|
537
538**Error codes**
539
540For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
541
542| ID                    | Error Message       |
543| ---------------------------- | ---------- |
544| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
545| 13600001  | IPC error. |
546
547**Example**
548
549  ```ts
550  import { BusinessError } from '@kit.BasicServicesKit';
551  let fileCache = new cloudSync.CloudFileCache();
552
553  let callback = (pg: cloudSync.DownloadProgress) => {
554    console.info("download state: " + pg.state);
555  }
556
557  try {
558    fileCache.on('progress', callback);
559    fileCache.off('progress', callback);
560  } catch (e) {
561    const error = e as BusinessError;
562    console.error(`Error code: ${error.code}, message: ${error.message}`);
563  }
564  ```
565
566### start<sup>11+</sup>
567
568start(uri: string): Promise&lt;void&gt;
569
570Starts to download a file from the Drive Kit to the local device. This API uses a promise to return the result.
571
572**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
573
574**Parameters**
575
576| Name    | Type  | Mandatory| Description|
577| ---------- | ------ | ---- | ---- |
578| uri | string | Yes  | URI of the file to download.|
579
580**Return value**
581
582| Type                 | Description            |
583| --------------------- | ---------------- |
584| Promise&lt;void&gt; | Promise used to return the result.|
585
586**Example**
587
588  ```ts
589  import { BusinessError } from '@kit.BasicServicesKit';
590  import { fileUri } from '@kit.CoreFileKit';
591  let fileCache = new cloudSync.CloudFileCache();
592  let path = "/data/storage/el2/cloud/1.txt";
593  let uri = fileUri.getUriFromPath(path);
594
595  try {
596    fileCache.on('progress', (pg: cloudSync.DownloadProgress) => {
597      console.info("download state:" + pg.state);
598    });
599  } catch (e) {
600    const error = e as BusinessError;
601    console.error(`Error code: ${error.code}, message: ${error.message}`);
602  }
603
604  fileCache.start(uri).then(() => {
605    console.info("start download successfully");
606  }).catch((err: BusinessError) => {
607    console.error("start download failed with error message: " + err.message + ", error code: " + err.code);
608  });
609  ```
610
611**Error codes**
612
613For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
614
615| ID                    | Error Message       |
616| ---------------------------- | ---------- |
617| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
618| 13600001  | IPC error. |
619| 13900002 | No such file or directory. |
620| 13900025 | No space left on device. |
621| 14000002 | Invalid uri. |
622
623### start<sup>11+</sup>
624
625start(uri: string, callback: AsyncCallback&lt;void&gt;): void
626
627Starts to download a file from the Drive Kit to the local device. This API uses an asynchronous callback to return the result.
628
629**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
630
631**Parameters**
632
633| Name    | Type  | Mandatory| Description|
634| ---------- | ------ | ---- | ---- |
635| uri | string | Yes  | URI of the file to download.|
636| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
637
638**Error codes**
639
640For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
641
642| ID                    | Error Message       |
643| ---------------------------- | ---------- |
644| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
645| 13600001  | IPC error. |
646| 13900002 | No such file or directory. |
647| 13900025 | No space left on device. |
648| 14000002 | Invalid uri. |
649
650**Example**
651
652  ```ts
653  import { BusinessError } from '@kit.BasicServicesKit';
654  import { fileUri } from '@kit.CoreFileKit';
655  let fileCache = new cloudSync.CloudFileCache();
656  let path = "/data/storage/el2/cloud/1.txt";
657  let uri = fileUri.getUriFromPath(path);
658
659  fileCache.start(uri, (err: BusinessError) => {
660    if (err) {
661      console.error("start download failed with error message: " + err.message + ", error code: " + err.code);
662    } else {
663      console.info("start download successfully");
664    }
665  });
666  ```
667
668### stop<sup>11+</sup>
669
670stop(uri: string, needClean?: boolean): Promise&lt;void&gt;
671
672Stops downloading a file from the Drive Kit to the local device. This API uses a promise to return the result.
673
674Calling **stop** will terminate the download of the current file and clear the cache file. You can use **start** to start the download again.
675
676**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
677
678**Parameters**
679
680| Name    | Type  | Mandatory| Description|
681| ---------- | ------ | ---- | ---- |
682| uri | string | Yes  | URI of the file to download.|
683| needClean<sup>12+</sup> | boolean | No  | Whether to delete the downloaded files. The default value **false** means to delete the downloaded files; the value **true** means the opposite.<br>This parameter is available since API version 12.|
684
685**Return value**
686
687| Type                 | Description            |
688| --------------------- | ---------------- |
689| Promise&lt;void&gt; | Promise used to return the result.|
690
691**Error codes**
692
693For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
694
695| ID                    | Error Message       |
696| ---------------------------- | ---------- |
697| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
698| 13600001  | IPC error. |
699| 13900002 | No such file or directory. |
700| 14000002 | Invalid URI. |
701
702**Example**
703
704  ```ts
705  import { BusinessError } from '@kit.BasicServicesKit';
706  import { fileUri } from '@kit.CoreFileKit';
707  let fileCache = new cloudSync.CloudFileCache();
708  let path = "/data/storage/el2/cloud/1.txt";
709  let uri = fileUri.getUriFromPath(path);
710
711  fileCache.stop(uri, true).then(() => {
712    console.info("stop download successfully");
713  }).catch((err: BusinessError) => {
714    console.error("stop download failed with error message: " + err.message + ", error code: " + err.code);
715  });
716  ```
717
718### stop<sup>11+</sup>
719
720stop(uri: string, callback: AsyncCallback&lt;void&gt;): void
721
722Stops downloading a file from the Drive Kit to the local device. This API uses an asynchronous callback to return the result.
723
724Calling **stop** will terminate the download of the current file and clear the cache file. You can use **start** to start the download again.
725
726**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
727
728**Parameters**
729
730| Name    | Type  | Mandatory| Description|
731| ---------- | ------ | ---- | ---- |
732| uri | string | Yes  | URI of the file to download.|
733| callback | AsyncCallback&lt;void&gt; | Yes  | Callback used to return the result.|
734
735**Error codes**
736
737For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
738
739| ID                    | Error Message       |
740| ---------------------------- | ---------- |
741| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
742| 13600001  | IPC error. |
743| 13900002 | No such file or directory. |
744| 14000002 | Invalid URI. |
745
746**Example**
747
748  ```ts
749  import { BusinessError } from '@kit.BasicServicesKit';
750  import { fileUri } from '@kit.CoreFileKit';
751  let fileCache = new cloudSync.CloudFileCache();
752  let path = "/data/storage/el2/cloud/1.txt";
753  let uri = fileUri.getUriFromPath(path);
754
755  fileCache.stop(uri, (err: BusinessError) => {
756    if (err) {
757      console.error("stop download failed with error message: " + err.message + ", error code: " + err.code);
758    } else {
759      console.info("stop download successfully");
760    }
761  });
762  ```
763
764## DownloadErrorType<sup>11+</sup>
765
766Enumerates the device-cloud download error types.
767
768**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
769
770| Name|  Value|  Description|
771| ----- |  ---- |  ---- |
772| NO_ERROR |  0 | No error.|
773| UNKNOWN_ERROR |  1 | Unknown error.|
774| NETWORK_UNAVAILABLE |  2 | The network is unavailable.|
775| LOCAL_STORAGE_FULL |  3 | The local space is insufficient.|
776| CONTENT_NOT_FOUND |  4 | The file is not found in the cloud space.|
777| FREQUENT_USER_REQUESTS |  5 | The user requests are too frequent to respond.|
778
779## cloudSync.registerChange<sup>12+</sup>
780
781registerChange(uri: string, recursion: boolean, callback: Callback&lt;ChangeData&gt;): void
782
783Subscribes to the change of a file.
784
785**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
786
787**Parameters**
788
789| Name    | Type  | Mandatory| Description|
790| ---------- | ------ | ---- | ---- |
791| uri | string | Yes  | URI of the file to download.|
792| recursion | boolean | Yes  | Whether to subscribe to the change of the sub-folders and files of the given URI. The value **true** means to subscribe to the change of the sub-folders and files of the given URI; the value **false** means to subscribe to only the change of the given URI.|
793| callback | Callback&lt;[ChangeData](#changedata12)&gt; | Yes  | Callback used to return the changed data.|
794
795**Error codes**
796
797For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
798
799| ID                    | Error Message       |
800| ---------------------------- | ---------- |
801| 202 | Permission verification failed, application which is not a system application uses system API. |
802| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
803| 13900001  | Operation not permitted. |
804| 13900002  | No such file or directory. |
805| 13900012  | Permission denied. |
806| 14000002  | Invalid URI. |
807
808**Example**
809
810  ```ts
811  import { fileUri } from '@kit.CoreFileKit';
812  let path = "/data/storage/el2/cloud/1.txt";
813  let uri = fileUri.getUriFromPath(path);
814  let onCallback1 = (changeData: ChangeData) => {
815    if (changeData.type == cloudSync.NotifyType.NOTIFY_ADDED) {
816      //file had added, do something
817    } else if (changeData.type== cloudSync.NotifyType.NOTIFY_DELETED) {
818      //file had removed, do something
819    }
820  }
821  cloudSync.registerChange(uri, false, onCallback1);
822  // Unregister the listener.
823  cloudSync.unRegisterChange(uri);
824  ```
825
826## cloudSync.unregisterChange<sup>12+</sup>
827
828unregisterChange(uri: string): void
829
830Unsubscribes from the change of a file.
831
832**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
833
834**Parameters**
835
836| Name    | Type  | Mandatory| Description|
837| ---------- | ------ | ---- | ---- |
838| uri | string | Yes  | URI of the file to download.|
839
840**Error codes**
841
842For details about the error codes, see [File Management Error Codes](errorcode-filemanagement.md).
843
844| ID                    | Error Message       |
845| ---------------------------- | ---------- |
846| 202 | Permission verification failed, application which is not a system application uses system API. |
847| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
848| 13900001  | Operation not permitted. |
849| 13900002  | No such file or directory. |
850| 13900012  | Permission denied. |
851| 14000002  | Invalid URI. |
852
853**Example**
854
855  ```ts
856  import { fileUri } from '@kit.CoreFileKit';
857  let path = "/data/storage/el2/cloud/1.txt";
858  let uri = fileUri.getUriFromPath(path);
859  let onCallback1 = (changeData: ChangeData) => {
860    if (changeData.type == cloudSync.NotifyType.NOTIFY_ADDED) {
861      //file had added, do something
862    } else if (changeData.type== cloudSync.NotifyType.NOTIFY_DELETED) {
863      //file had removed, do something
864    }
865  }
866  cloudSync.registerChange(uri, false, onCallback1);
867  // Unregister the listener.
868  cloudSync.unRegisterChange(uri);
869  ```
870
871## NotifyType<sup>12+</sup>
872
873Enumerates the data change types.
874
875**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
876
877| Name|  Value|  Description|
878| ----- |  ---- |  ---- |
879| NOTIFY_ADDED |  0 | A file is created.|
880| NOTIFY_MODIFIED |  1 | The file is modified.|
881| NOTIFY_DELETED |  2 | The file is deleted.|
882| NOTIFY_RENAMED |  3 | The file is renamed or moved.|
883
884## ChangeData<sup>12+</sup>
885
886Represents the data change information.
887
888**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
889
890| Name    | Type  | Mandatory| Description|
891| ---------- | ------ | ---- | ---- |
892| type | [NotifyType](#notifytype12) | Yes  | Type of the data change.|
893| isDirectory | Array&lt;boolean&gt; | Yes  | Whether the URIs with data changed are of folders.|
894| uris | Array&lt;string&gt; | Yes  | URIs of the files changed.|
895