js-apis-file-cloudsyncmanager.md 15.5 KB
Newer Older
张文迪 已提交
1 2
# @ohos.file.cloudSyncManager (端云同步管理能力)

C
c00382521 已提交
3
该模块向云空间应用提供端云同步管理能力,包括使能/去使能端云协同能力、修改应用同步开关,云端数据变化通知以及帐号退出清理/保留云相关文件等。
张文迪 已提交
4 5

> **说明:**
C
c00382521 已提交
6
>
Z
zengyawen 已提交
7 8
> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
> - 本模块接口为系统接口,三方应用不支持调用。
张文迪 已提交
9 10 11 12

## 导入模块

```js
C
c00382521 已提交
13
import cloudSyncManager from '@ohos.file.cloudSyncManager'
张文迪 已提交
14 15 16 17
```

## cloudSyncManager.changeAppCloudSwitch

C
c00382521 已提交
18
changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean): Promise<void>
张文迪 已提交
19 20 21 22 23 24 25 26 27

异步方法修改应用的端云文件同步开关,以Promise形式返回结果。

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
C
c00382521 已提交
28
| accountId | string | 是   | 帐号Id |
张文迪 已提交
29 30 31 32 33 34 35 36 37
| bundleName | string | 是   | 应用包名|
| status | boolean | 是   | 修改的应用云同步开关状态,true为打开,false为关闭|

**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| Promise<void> | 使用Promise形式返回修改应用的端云文件同步开关的结果 |

Z
zengyawen 已提交
38 39 40
**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)
C
c00382521 已提交
41

Z
zengyawen 已提交
42 43 44 45 46 47
| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

张文迪 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61
**示例:**

  ```js
  let accountId = "testAccount";
  let bundleName = "com.example.bundle";
  cloudSyncManager.changeAppCloudSwitch(accountId, bundleName, true).then(function() {
      console.info("changeAppCloudSwitch successfully");
  }).catch(function(err) {
      console.info("changeAppCloudSwitch failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

## cloudSyncManager.changeAppCloudSwitch

C
c00382521 已提交
62
changeAppCloudSwitch(accountId: string, bundleName: string, status: boolean, callback: AsyncCallback<void>): void
张文迪 已提交
63 64 65 66 67 68 69 70 71

异步方法修改应用的端云文件同步开关,以callback形式返回结果。

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
C
c00382521 已提交
72
| accountId | string | 是   | 帐号Id|
张文迪 已提交
73 74 75 76
| bundleName | string | 是   | 应用包名|
| status | boolean | 是   | 修改的应用云同步开关状态,true为打开,false为关闭|
| callback | AsyncCallback<void> | 是   | 异步修改应用的端云文件同步开关之后的回调 |

Z
zengyawen 已提交
77 78 79
**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)
C
c00382521 已提交
80

Z
zengyawen 已提交
81 82 83 84 85 86
| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

张文迪 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
**示例:**

  ```js
  let accountId = "testAccount";
  let bundleName = "com.example.bundle";
  cloudSyncManager.changeAppCloudSwitch(accountId, bundleName, true, (err) => {
    if (err) {
      console.info("changeAppCloudSwitch failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("changeAppCloudSwitch successfully");
    }
  });
  ```
Z
zengyawen 已提交
100

张文迪 已提交
101 102
## cloudSyncManager.notifyDataChange

C
c00382521 已提交
103
notifyDataChange(accountId: string, bundleName: string): Promise<void>
张文迪 已提交
104 105 106 107 108 109 110 111 112

异步方法通知端云服务应用的云数据变更,以Promise形式返回结果。

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
C
c00382521 已提交
113
| accountId | string | 是   | 帐号Id|
张文迪 已提交
114 115 116 117 118 119 120 121
| bundleName | string | 是   | 应用包名|

**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| Promise<void> | 使用Promise形式返回通知端云服务应用的云数据变更的结果 |

Z
zengyawen 已提交
122 123 124
**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)
C
c00382521 已提交
125

Z
zengyawen 已提交
126 127 128 129 130 131
| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

张文迪 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145
**示例:**

  ```js
  let accountId = "testAccount";
  let bundleName = "com.example.bundle";
  cloudSyncManager.notifyDataChange(accountId, bundleName).then(function() {
      console.info("notifyDataChange successfully");
  }).catch(function(err) {
      console.info("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

## cloudSyncManager.notifyDataChange

C
c00382521 已提交
146
notifyDataChange(accountId: string, bundleName: string, callback: AsyncCallback<void>): void
张文迪 已提交
147 148 149 150 151 152 153 154 155

异步方法通知端云服务应用的云数据变更,以callback形式返回结果。

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
C
c00382521 已提交
156
| accountId | string | 是   | 帐号Id|
张文迪 已提交
157 158 159
| bundleName | string | 是   | 应用包名|
| callback | AsyncCallback<void> | 是   | 异步通知端云服务应用的云数据变更之后的回调 |

Z
zengyawen 已提交
160 161 162
**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)
C
c00382521 已提交
163

Z
zengyawen 已提交
164 165 166 167 168 169
| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

张文迪 已提交
170 171 172 173 174 175 176 177 178 179 180 181
**示例:**

  ```js
  let accountId = "testAccount";
  let bundleName = "com.example.bundle";
  cloudSyncManager.notifyDataChange(accountId, bundleName, (err) => {
    if (err) {
      console.info("notifyDataChange failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("notifyDataChange successfully");
    }
  });
C
c00382521 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
  ```

## cloudSyncManager.enableCloud

enableCloud(accountId: string, switches: { [bundleName: string]: boolean }): Promise<void>

异步方法使能端云协同能力,以Promise形式返回结果。

**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| accountId | string | 是   | 帐号Id|
| switches | object | 是   | 应用的端云协同特性使能开关,bundleName为应用包名,开关状态是个boolean类型|

**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| Promise<void> | 使用Promise形式返回使能端云协同能力的结果 |

**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)

| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

**示例:**

  ```js
  let accountId = "testAccount";
  let switches = {"com.example.bundleName1": true, "com.example.bundleName2": false};
  cloudSyncManager.enableCloud(accountId, switches).then(function() {
      console.info("enableCloud successfully");
  }).catch(function(err) {
      console.info("enableCloud failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

## cloudSyncManager.enableCloud

enableCloud(accountId: string, switches: { [bundleName: string]: boolean }, callback: AsyncCallback<void>): void

异步方法使能端云协同能力,以callback形式返回结果。

**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| accountId | string | 是   | 帐号Id|
| switches | object | 是   | 应用的端云协同特性使能开关,bundleName为应用包名,开关状态是个boolean类型|
| callback | AsyncCallback<void> | 是   | 异步使能端云协同能力之后的回调 |

**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)

| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

**示例:**

  ```js
  let accountId = "testAccount";
  let switches = {"com.example.bundleName1": true, "com.example.bundleName2": false};
  cloudSyncManager.enableCloud(accountId, switches, (err) => {
    if (err) {
      console.info("enableCloud failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("enableCloud successfully");
    }
  });
  ```

## cloudSyncManager.disableCloud

disableCloud(accountId: string): Promise<void>

异步方法去使能端云协同能力,以Promise形式返回结果。

**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| accountId | string | 是   | 帐号Id|

**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| Promise<void> | 使用Promise形式返回去使能端云协同能力的结果 |

**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)

| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

**示例:**

  ```js
  let accountId = "testAccount";
  cloudSyncManager.disableCloud(accountId).then(function() {
      console.info("disableCloud successfully");
  }).catch(function(err) {
      console.info("disableCloud failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

## cloudSyncManager.disableCloud

disableCloud(accountId: string, callback: AsyncCallback<void>): void

异步方法去使能端云协同能力,以callback形式返回结果。

**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| accountId | string | 是   | 帐号Id|
| callback | AsyncCallback<void> | 是   | 异步去使能端云协同能力之后的回调 |

**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)

| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

**示例:**

  ```js
  let accountId = "testAccount";
  cloudSyncManager.disableCloud(accountId, (err) => {
    if (err) {
      console.info("disableCloud failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("disableCloud successfully");
    }
  });
  ```

## Action

清理本地云相关数据时的Action,为枚举类型。

**系统能力**: SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

| 名称 |  值|  描述|
| ----- |  ---- |  ---- |
| RETAIN_DATA |  0 |  仅清除云端标识,保留本地缓存文件|
| CLEAR_DATA |  1 |  清除云端标识信息,若存在本地缓存文件,一并删除|

## cloudSyncManager.clean

clean(accountId: string, appActions: { [bundleName: string]: Action }): Promise<void>

异步方法清理本地云相关数据,以Promise形式返回结果。

**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| accountId | string | 是   | 帐号Id|
| appActions | object | 是   | 清理动作类型,bundleName为待清理应用包名, [Action](#action)为清理动作类型|

**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| Promise<void> | 使用Promise形式返回清理本地云相关数据的结果 |

**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)

| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

**示例:**

  ```js
  let accountId = "testAccount";
  let appActions = {"com.example.bundleName1": cloudSyncManager.Action.RETAIN_DATA,
                    "com.example.bundleName2": cloudSyncManager.Action.CLEAR_DATA};
  cloudSyncManager.clean(accountId, appActions).then(function() {
      console.info("clean successfully");
  }).catch(function(err) {
      console.info("clean failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

## cloudSyncManager.clean

clean(accountId: string, appActions: { [bundleName: string]: Action }, callback: AsyncCallback<void>): void

异步方法去使能端云协同能力,以callback形式返回结果。

**需要权限**:ohos.permission.CLOUDFILE_SYNC_MANAGER

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSyncManager

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| accountId | string | 是   | 帐号Id|
| appActions | object | 是   | 清理动作类型,bundleName为待清理应用包名, [Action](#action)为清理动作类型|
| callback | AsyncCallback<void> | 是   | 异步去使能端云协同能力之后的回调 |

**错误码:**

以下错误码的详细介绍请参见[文件管理子系统错误码](../errorcodes/errorcode-filemanagement.md)

| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 201 | Permission verification failed. |
| 202 | The caller is not a system application. |
| 401 | The input parameter is invalid. |

**示例:**

  ```js
  let accountId = "testAccount";
  let appActions = {"com.example.bundleName1": cloudSyncManager.Action.RETAIN_DATA,
                    "com.example.bundleName2": cloudSyncManager.Action.CLEAR_DATA};
  cloudSyncManager.clean(accountId, appActions, (err) => {
    if (err) {
      console.info("clean failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("clean successfully");
    }
  });
  ```