# @ohos.file.cloudSync (Device-Cloud Synchronization)
The **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.
> **NOTE**
>
> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
## Modules to Import
```ts
import cloudSync from '@ohos.file.cloudSync';
```
## SyncState
Enumerates the device-cloud synchronization states.
> **NOTE**
>
> If a synchronization progress event listener is registered for an application, a callback will be invoked to notify the application when the device-cloud synchronization state is changed.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
| Name| Value| Description|
| ----- | ---- | ---- |
| UPLOADING | 0 | Uploading.|
| UPLOAD_FAILED | 1 | Upload failed.|
| DOWNLOADING | 2 | Downloading.|
| DOWNLOAD_FAILED | 3 | Download failed.|
| COMPLETED | 4 | Synchronization completed.|
| STOPPED | 5 | Synchronization stopped.|
## ErrorType
Enumerates the device-cloud synchronization errors.
- Currently, **NETWORK_UNAVAILABLE** is returned only when both the mobile network and Wi-Fi are unavailable during synchronization. If either network is available, synchronization can be performed normally.
- During the synchronization process, if the battery level is lower than 15% in non-charging scenarios, **BATTERY_LEVEL_LOW** will be return when the current upload is complete; if the battery level is lower than 10% in non-charging scenarios, **BATTERY_LEVEL_WARNING** will be returned when the current upload is complete.
- When synchronization is being triggered, if the battery level is lower than 15% in non-charging scenarios, synchronization is not allowed and an error code will be returned by **start()**.
- 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.
- If the local space is insufficient when a file is downloaded, the download will fail. After the local space is released, the file will be downloaded again when synchronization starts.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
| Name| Value| Description|
| ----- | ---- | ---- |
| NO_ERROR | 0 | No error.|
| NETWORK_UNAVAILABLE | 1 | No network is available.|
| WIFI_UNAVAILABLE | 2 | Wi-Fi is unavailable.|
| BATTERY_LEVEL_LOW | 3 | The battery level is lower than 15%.|
| BATTERY_LEVEL_WARNING | 4 | The battery level is lower than 10%.|
| CLOUD_STORAGE_FULL | 5 | The cloud space is insufficient.|
| LOCAL_STORAGE_FULL | 6 | The local space is insufficient.|
## SyncProgress
Represents information about the device-cloud synchronization progress.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| state | [SyncState](#syncstate) | Yes | Device-cloud synchronization state.|
| error | [ErrorType](#errortype) | Yes | Synchronization error.|
## GallerySync
Provides APIs to implement device-cloud synchronization of media assets in **Gallery**. Before using the APIs of **GallerySync**, you need to create a **GallerySync** instance.
### constructor
constructor()
A constructor used to create a **GallerySync** instance.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Example**
```ts
let gallerySync = new cloudSync.GallerySync()
```
### on
on(evt: 'progress', callback: (pg: SyncProgress) => void): void
Registers a listener for the device-cloud synchronization progress.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| evt | string | Yes | Event type. The value is **progress**, which indicates the synchronization progress event.|
| callback | (pg: SyncProgress) => void | Yes | Callback invoked to return the synchronization progress event. The input parameter is [SyncProgress](#syncprogress), and the return value is **void**.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let gallerySync = new cloudSync.GallerySync();
gallerySync.on('progress', (pg: cloudSync.SyncProgress) => {
console.info("syncState: " + pg.state);
});
```
### off
off(evt: 'progress', callback: (pg: SyncProgress) => void): void
Unregisters a listener for the device-cloud synchronization progress.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| evt | string | Yes | Event type. The value is **progress**, which indicates the synchronization progress event.|
| callback | (pg: SyncProgress) => void | Yes | Callback for the synchronization progress event. The input parameter is [SyncProgress](#syncprogress), and the return value is **void**.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let gallerySync = new cloudSync.GallerySync();
let callback = (pg: cloudSync.SyncProgress) => {
console.info("gallery sync state: " + pg.state + "error type:" + pg.error);
}
gallerySync.on('progress', callback);
gallerySync.off('progress', callback);
```
### off
off(evt: 'progress'): void
Unregisters all listeners for the device-cloud synchronization progress.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| evt | string | Yes | Event type. The value is **progress**, which indicates the synchronization progress event.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let gallerySync = new cloudSync.GallerySync();
gallerySync.on('progress', (pg: cloudSync.SyncProgress) => {
console.info("syncState: " + pg.state);
});
gallerySync.off('progress');
```
### start
start(): Promise<void>
Starts device-cloud synchronization. This API uses a promise to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 22400001 | Cloud status not ready. |
| 22400002 | Network unavailable. |
| 22400003 | Battery level warning. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let gallerySync = new cloudSync.GallerySync();
gallerySync.on('progress', (pg: cloudSync.SyncProgress) => {
console.info("syncState: " + pg.state);
});
gallerySync.start().then(() => {
console.info("start sync successfully");
}).catch((err: BusinessError) => {
console.info("start sync failed with error message: " + err.message + ", error code: " + err.code);
});
```
### start
start(callback: AsyncCallback<void>): void
Starts device-cloud synchronization. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 22400001 | Cloud status not ready. |
| 22400002 | Network unavailable. |
| 22400003 | Battery level warning. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let gallerySync = new cloudSync.GallerySync();
gallerySync.start((err: BusinessError) => {
if (err) {
console.info("start sync failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("start sync successfully");
}
});
```
### stop
stop(): Promise<void>
Stops device-cloud synchronization. This API uses a promise to return the result.
> **NOTE**
>
> Calling **stop** will stop the synchronization process. To resume the synchronization, call [start](#start).
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let gallerySync = new cloudSync.GallerySync();
gallerySync.stop().then(() => {
console.info("stop sync successfully");
}).catch((err: BusinessError) => {
console.info("stop sync failed with error message: " + err.message + ", error code: " + err.code);
});
```
### stop
stop(callback: AsyncCallback<void>): void
Stops device-cloud synchronization. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> Calling **stop** will stop the synchronization process. To resume the synchronization, call [start](#start-1).
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let gallerySync = new cloudSync.GallerySync();
gallerySync.stop((err: BusinessError) => {
if (err) {
console.info("stop sync failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("stop sync successfully");
}
});
```
## State11+
Enumerates the download states of a cloud file.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
| Name| Value| Description|
| ----- | ---- | ---- |
| RUNNING | 0 | The cloud file is being downloaded.|
| COMPLETED | 1 | The cloud file download is complete.|
| FAILED | 2 | The cloud file download failed.|
| STOPPED | 3 | The cloud file download is stopped.|
## DownloadProgress11+
Represents information about the download progress of a cloud file.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| state | [State](#state11) | Yes | File download state.|
| processed | number | Yes | Size of the data downloaded.|
| size | number | Yes | Size of the cloud file.|
| uri | string | Yes | URI of the cloud file.|
| error | [DownloadErrorType](#downloaderrortype11) | Yes | Download error type.|
## Download
Provides APIs for downloading image files to **Gallery**. Before using the APIs of **Download**, you need to create a **Download** instance.
### constructor
constructor()
A constructor used to create a **Download** instance.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Example**
```ts
let download = new cloudSync.Download()
```
### on
on(evt: 'progress', callback: (pg: DownloadProgress) => void): void
Registers a listener for the download progress of a cloud file.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| evt | string | Yes | Event. The value is **progress**, which indicates the download progress event of a cloud file.|
| callback | (pg: DownloadProgress) => void | Yes | Callback invoked to return the registered event. The input parameter is [DownloadProgress](#downloadprogress11), and the return value is **void**.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let download = new cloudSync.Download();
download.on('progress', (pg: cloudSync.DownloadProgress) => {
console.info("download state: " + pg.state);
});
```
### off
off(evt: 'progress', callback: (pg: DownloadProgress) => void): void
Unregisters a listener for the download progress of a cloud file.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| evt | string | Yes | Event type. The value is **progress**, which indicates the download progress event of a cloud file.|
| callback | (pg: DownloadProgress) => void | Yes | Callback for the download progress event of a cloud file. The input parameter is [DownloadProgress](#downloadprogress11), and the return value is **void**.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let download = new cloudSync.Download();
let callback = (pg: cloudSync.DownloadProgress) => {
console.info("download state: " + pg.state);
}
download.on('progress', callback);
download.off('progress', callback);
```
### off
off(evt: 'progress'): void
Unregisters all listeners for the download progress event of a cloud file.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| evt | string | Yes | Event type. The value is **progress**, which indicates the download progress event of a cloud file.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let download = new cloudSync.Download();
download.on('progress', (pg: cloudSync.DownloadProgress) => {
console.info("download state:" + pg.state);
});
download.off('progress');
```
### start
start(uri: string): Promise<void>
Starts to download a cloud file. This API uses a promise to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the file to download.|
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<void> | Promise used to return the result.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
let download = new cloudSync.Download();
let uri: string = "file:///media/Photo/1";
download.on('progress', (pg: cloudSync.DownloadProgress) => {
console.info("download state:" + pg.state);
});
download.start(uri).then(() => {
console.info("start download successfully");
}).catch((err: BusinessError) => {
console.info("start download failed with error message: " + err.message + ", error code: " + err.code);
});
```
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 13900002 | No such file or directory. |
| 13900025 | No space left on device. |
### start
start(uri: string, callback: AsyncCallback<void>): void
Starts to download a cloud file. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the file to download.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
| 13900002 | No such file or directory. |
| 13900025 | No space left on device. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let download = new cloudSync.Download();
let uri: string = "file:///media/Photo/1";
download.start(uri, (err: BusinessError) => {
if (err) {
console.info("start download failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("start download successfully");
}
});
```
### stop
stop(uri: string): Promise<void>
Stops downloading a cloud file. This API uses a promise to return the result.
> **NOTE**
>
> Calling **stop** will terminate the download of the current file and clear the cache file. You can use **start** to start the download again.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the file to download.|
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let download = new cloudSync.Download();
let uri: string = "file:///media/Photo/1";
download.stop(uri).then(() => {
console.info("stop download successfully");
}).catch((err: BusinessError) => {
console.info("stop download failed with error message: " + err.message + ", error code: " + err.code);
});
```
### stop
stop(uri: string, callback: AsyncCallback<void>): void
Stops downloading a cloud file. This API uses an asynchronous callback to return the result.
> **NOTE**
>
> Calling **stop** will terminate the download of the current file and clear the cache file. You can use **start** to start the download again.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the file to download.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let download = new cloudSync.Download();
let uri: string = "file:///media/Photo/1";
download.stop(uri, (err: BusinessError) => {
if (err) {
console.info("stop download failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("stop download successfully");
}
});
```
## FileSync11+
Provides APIs for the file manager application to perform device-cloud synchronization of the files stored in the Drive Kit. Before using the APIs of this class, you need to create a **FileSync** instance.
### constructor11+
constructor()
A constructor used to create a **FileSync** instance.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
**Example**
```ts
let fileSync = new cloudSync.FileSync()
```
### on11+
on(event: 'progress', callback: Callback\): void
Registers a listener for the device-cloud synchronization progress.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| event | string | Yes | Event type. The value is **progress**, which indicates the synchronization progress event.|
| callback | Callback\<[SyncProgress](#syncprogress)> | Yes | Callback invoked to return the synchronization progress information.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let fileSync = new cloudSync.FileSync();
let callback = (pg: cloudSync.SyncProgress) => {
console.info("file sync state: " + pg.state + "error type:" + pg.error);
}
fileSync.on('progress', callback);
```
### off11+
off(event: 'progress', callback?: Callback\): void
Unregisters a listener for the device-cloud synchronization progress.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| event | string | Yes | Event type. The value is **progress**, which indicates the synchronization progress event.|
| callback | Callback\<[SyncProgress](#syncprogress)> | No | Callback for the synchronization progress event.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let fileSync = new cloudSync.FileSync();
let callback = (pg: cloudSync.SyncProgress) => {
console.info("file sync state: " + pg.state + "error type:" + pg.error);
}
fileSync.on('progress', callback);
fileSync.off('progress', callback);
```
### start11+
start(): Promise<void>
Starts device-cloud synchronization of a file in the Drive Kit. This API uses a promise to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<void> | Promise that returns no value.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 22400001 | Cloud status not ready. |
| 22400002 | Network unavailable. |
| 22400003 | Battery level warning. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let fileSync = new cloudSync.FileSync();
let callback = (pg: cloudSync.SyncProgress) => {
console.info("file sync state: " + pg.state + "error type:" + pg.error);
}
fileSync.on('progress', callback);
fileSync.start().then(() => {
console.info("start sync successfully");
}).catch((err: BusinessError) => {
console.info("start sync failed with error message: " + err.message + ", error code: " + err.code);
});
```
### start11+
start(callback: AsyncCallback<void>): void
Starts device-cloud synchronization of a file in the Drive Kit. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 22400001 | Cloud status not ready. |
| 22400002 | Network unavailable. |
| 22400003 | Battery level warning. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let fileSync = new cloudSync.FileSync();
fileSync.start((err: BusinessError) => {
if (err) {
console.info("start sync failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("start sync successfully");
}
});
```
### stop11+
stop(): Promise<void>
Stops device-cloud synchronization of the file in the Drive Kit. This API uses a promise to return the result.
Calling **stop** will stop the synchronization process. To resume the synchronization, call [start](#start11).
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let fileSync = new cloudSync.FileSync();
fileSync.stop().then(() => {
console.info("stop sync successfully");
}).catch((err: BusinessError) => {
console.info("stop sync failed with error message: " + err.message + ", error code: " + err.code);
});
```
### stop11+
stop(callback: AsyncCallback<void>): void
Stops device-cloud synchronization of the file in the Drive Kit. This API uses an asynchronous callback to return the result.
Calling **stop** will stop the synchronization process. To resume the synchronization, call [start](#start11-1).
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let fileSync = new cloudSync.FileSync();
fileSync.stop((err: BusinessError) => {
if (err) {
console.info("stop sync failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("stop sync successfully");
}
});
```
### getLastSyncTime11+
getLastSyncTime(): Promise<number>
Obtains the last synchronization time. This API uses a promise to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<number> | Promise used to return the last synchronization time obtained.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let fileSync = new cloudSync.FileSync();
fileSync.getLastSyncTime().then((timeStamp: number) => {
let date = new Date(timeStamp);
console.info("get last sync time successfully:"+ date);
}).catch((err: BusinessError) => {
console.info("get last sync time failed with error message: " + err.message + ", error code: " + err.code);
});
```
### getLastSyncTime11+
getLastSyncTime(callback: AsyncCallback<number>): void;
Obtains the last synchronization time. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| callback | AsyncCallback<number> | Yes | Callback invoked to return the last synchronization time obtained.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let fileSync = new cloudSync.FileSync();
fileSync.getLastSyncTime((err: BusinessError, timeStamp: number) => {
if (err) {
console.info("get last sync time with error message: " + err.message + ", error code: " + err.code);
} else {
let date = new Date(timeStamp);
console.info("get last sync time successfully:"+ date);
}
});
```
## CloudFileCache11+
Provides APIs for the file manager application to download files from the Drive Kit to a local device.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
### construct11+
constructor()
A constructor used to create a **CloudFileCache** instance.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**Error codes**
| ID | Error Message |
| ---------------------------- | ---------- |
| 401 | The input parameter is invalid. |
**Example**
```ts
let fileCache = new cloudSync.CloudFileCache();
```
### on11+
on(event: 'progress', callback: Callback\): void
Registers a listener for the download progress of a file from the Drive Kit.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| event | string | Yes | Event type. The value is **progress**, which indicates the download progress event of a file from the Drive Kit.|
| callback | Callback\<[DownloadProgress](#downloadprogress11)> | Yes | Callback invoked to return the download progress information.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
**Example**
```ts
let fileCache = new cloudSync.CloudFileCache();
let callback = (pg: cloudSync.DownloadProgress) => {
console.info("download state: " + pg.state);
};
fileCache.on('progress', callback);
```
### off11+
off(event: 'progress', callback?: Callback\): void
Unregisters a listener for the download progress of a file from the Drive Kit.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| event | string | Yes | Event type. The value is **progress**, which indicates the download progress event of a file from the Drive Kit.|
| callback | Callback\<[DownloadProgress](#downloadprogress11)> | No | Callback for the download progress event of a file from the Drive Kit.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 13900002 | No such file or directory. |
| 13900025 | No space left on device. |
| 14000002 | Invalid uri. |
**Example**
```ts
let fileCache = new cloudSync.CloudFileCache();
let callback = (pg: cloudSync.DownloadProgress) => {
console.info("download state: " + pg.state);
}
fileCache.on('progress', callback);
fileCache.off('progress', callback);
```
### start11+
start(uri: string): Promise<void>
Starts to download a file from the Drive Kit to the local device. This API uses a promise to return the result.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the file to download.|
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<void> | Promise used to return the result.|
**Example**
```ts
import { BusinessError } from '@ohos.base';
import fileUri from '@ohos.file.fileuri';
let fileCache = new cloudSync.CloudFileCache();
let path = "/data/storage/el2/cloud/1.txt";
let uri = fileUri.getUriFromPath(path);
fileCache.on('progress', (pg: cloudSync.DownloadProgress) => {
console.info("download state:" + pg.state);
});
fileCache.start(uri).then(() => {
console.info("start download successfully");
}).catch((err: BusinessError) => {
console.info("start download failed with error message: " + err.message + ", error code: " + err.code);
});
```
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 13900002 | No such file or directory. |
| 13900025 | No space left on device. |
| 14000002 | Invalid uri. |
### start11+
start(uri: string, callback: AsyncCallback<void>): void
Starts to download a file from the Drive Kit to the local device. This API uses an asynchronous callback to return the result.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the file to download.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 13900002 | No such file or directory. |
| 13900025 | No space left on device. |
| 14000002 | Invalid uri. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
import fileUri from '@ohos.file.fileuri';
let fileCache = new cloudSync.CloudFileCache();
let path = "/data/storage/el2/cloud/1.txt";
let uri = fileUri.getUriFromPath(path);
fileCache.start(uri, (err: BusinessError) => {
if (err) {
console.info("start download failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("start download successfully");
}
});
```
### stop11+
stop(uri: string): Promise<void>
Stops downloading a file from the Drive Kit to the local device. This API uses a promise to return the result.
Calling **stop** will terminate the download of the current file and clear the cache file. You can use **start** to start the download again.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the file to download.|
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<void> | Promise used to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 13900002 | No such file or directory. |
| 14000002 | Invalid uri. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
import fileUri from '@ohos.file.fileuri';
let fileCache = new cloudSync.CloudFileCache();
let path = "/data/storage/el2/cloud/1.txt";
let uri = fileUri.getUriFromPath(path);
fileCache.stop(uri).then(() => {
console.info("stop download successfully");
}).catch((err: BusinessError) => {
console.info("stop download failed with error message: " + err.message + ", error code: " + err.code);
});
```
### stop11+
stop(uri: string, callback: AsyncCallback<void>): void
Stops downloading a file from the Drive Kit to the local device. This API uses an asynchronous callback to return the result.
Calling **stop** will terminate the download of the current file and clear the cache file. You can use **start** to start the download again.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the file to download.|
| callback | AsyncCallback<void> | Yes | Callback invoked to return the result.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 13900002 | No such file or directory. |
| 14000002 | Invalid uri. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
import fileUri from '@ohos.file.fileuri';
let fileCache = new cloudSync.CloudFileCache();
let path = "/data/storage/el2/cloud/1.txt";
let uri = fileUri.getUriFromPath(path);
fileCache.stop(uri, (err: BusinessError) => {
if (err) {
console.info("stop download failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("stop download successfully");
}
});
```
### cleanCache11+
cleanCache(uri: string): void;
Deletes a cache file. This API returns the result synchronously.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | string | Yes | URI of the cache file to delete.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 13900002 | No such file or directory. |
| 14000002 | Invalid uri. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
import fileUri from '@ohos.file.fileuri';
let fileCache = new cloudSync.CloudFileCache();
let path = "/data/storage/el2/cloud/1.txt";
let uri = fileUri.getUriFromPath(path);
try {
fileCache.cleanCache(uri);
} catch (err) {
let error:BusinessError = err as BusinessError;
console.info("clean cache failed with error message: " + err.message + ", error code: " + err.code);
}
```
## cloudSync.getFileSyncState11+
getFileSyncState(uri: Array<string>): Promise<Array<FileSyncState>>
Obtains the file synchronization state. This API uses a promise to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | Array<string> | Yes | URI of the file whose synchronization state is to be obtained.|
**Return value**
| Type | Description |
| --------------------- | ---------------- |
| Promise<Array<FileSyncState>> | Promise used to return the synchronization state obtained.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 13900002 | No such file or directory. |
| 14000002 | Invalid uri. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let uris: Array = ["file://uri"];
cloudSync.getFileSyncState(uris).then(function(syncStates: Array) {
for(let i = 0, len = syncStates.length; i < len; i++){
console.info("get file sync state successfully" + syncStates[i]);
}
}).catch((err: BusinessError) => {
console.info("get file sync state failed with error message: " + err.message + ", error code: " + err.code);
});
```
## cloudSync.getFileSyncState11+
getFileSyncState(uri: Array<string>, callback: AsyncCallback<Array<FileSyncState>>): void
Obtains the file synchronization state. This API uses an asynchronous callback to return the result.
**Required permissions**: ohos.permission.CLOUDFILE_SYNC
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
**Parameters**
| Name | Type | Mandatory| Description|
| ---------- | ------ | ---- | ---- |
| uri | Array<string> | Yes | URI of the file whose synchronization state is to be obtained.|
| callback | AsyncCallback<Array<FileSyncState>> | Yes | Callback invoked to return the file synchronization state.|
**Error codes**
For details about the error codes, see [File Management Error Codes](../errorcodes/errorcode-filemanagement.md).
| ID | Error Message |
| ---------------------------- | ---------- |
| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. |
| 202 | Permission verification failed, application which is not a system application uses system API. |
| 401 | The input parameter is invalid. |
| 13600001 | IPC error. |
| 13900002 | No such file or directory. |
| 14000002 | Invalid uri. |
**Example**
```ts
import { BusinessError } from '@ohos.base';
let uris: Array = ["file://uri"];
cloudSync.getFileSyncState(uris, (err: BusinessError, syncStates: Array) => {
if (err) {
console.info("get file sync state with error message: " + err.message + ", error code: " + err.code);
} else {
for(let i = 0, len = syncStates.length; i < len; i++){
console.info("get file sync state successfully" + syncStates[i]);
}
}
});
```
## FileSyncState11+
Enumerates the device-cloud file synchronization states.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
**System API**: This is a system API.
| Name| Value| Description|
| ----- | ---- | ---- |
| UPLOADING | 0 | Uploading.|
| DOWNLOADING | 1 | Downloading.|
| COMPLETED | 2 | Synchronization completed.|
| STOPPED | 3 | Synchronization stopped.|
## DownloadErrorType11+
Enumerates the device-cloud download error types.
**System capability**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core
| Name| Value| Description|
| ----- | ---- | ---- |
| NO_ERROR | 0 | No error.|
| UNKNOWN_ERROR | 1 | Unknown error.|
| NETWORK_UNAVAILABLE | 2 | The network is unavailable.|
| LOCAL_STORAGE_FULL | 3 | The local space is insufficient.|
| CONTENT_NOT_FOUND | 4 | The file is not found in the cloud space.|
| FREQUENT_USER_REQUESTS | 5 | The user requests are too frequent to respond.|