js-apis-file-cloudsync.md 19.1 KB
Newer Older
C
c00382521 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# @ohos.file.cloudSync (端云同步能力)

该模块向应用提供端云同步能力,包括启动/停止端云同步以及启动/停止原图下载功能。

> **说明:**
>
> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
> - 本模块接口为系统接口,三方应用不支持调用。

## 导入模块

```js
import cloudSync from '@ohos.file.cloudSync'
```

## SyncState

端云同步状态,为枚举类型。

> **说明:**
>
> 以下同步状态发生变更时,如果应用注册了同步过程事件监听,则通过回调通知应用。

**系统能力**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

26
| 名称 |  值|  说明 |
C
c00382521 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| ----- |  ---- |  ---- |
| UPLOADING |  0 | 上行同步中 |
| UPLOAD_FAILED |  1 | 上行同步失败 |
| DOWNLOADING |  2 | 下行同步中 |
| DOWNLOAD_FAILED |  3 | 下行同步失败 |
| COMPLETED |  4 | 同步成功 |
| STOPPED |  5 | 同步已停止 |

## ErrorType

端云同步失败类型,为枚举类型。

- 当前阶段,同步过程中,当移动数据网络和WIFI均不可用时,才会返回NETWORK_UNAVAILABLE;若有一种类型网络可用,则能正常同步。
- 同步过程中,非充电场景下,电量低于15%,完成当前批上行同步后停止同步,返回低电量;电量低于10%,完成当前批上行同步后停止同步,返回告警电量。
- 触发同步时,非充电场景下,若电量低于15%,则不允许同步,start接口返回对应错误。
- 上行时,若云端空间不足,则文件上行失败,云端无该文件记录。
- 下行时,若本地空间不足,则文件下行失败,本地空间释放后再次同步会重新下行。

**系统能力**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

47
| 名称 |  值|  说明 |
C
c00382521 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
| ----- |  ---- |  ---- |
| NO_ERROR |  0 | 没有错误 |
| NETWORK_UNAVAILABLE |  1 | 所有网络不可用 |
| WIFI_UNAVAILABLE |  2 | WIFI不可用 |
| BATTERY_LEVEL_LOW |  3 | 低电量(低于15%) |
| BATTERY_LEVEL_WARNING |  4 | 告警电量(低于10%) |
| CLOUD_STORAGE_FULL |  5 | 云端空间不足 |
| LOCAL_STORAGE_FULL |  6 | 本地空间不足 |

## SyncProgress

端云同步过程。

**系统能力**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

| 名称     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| state | [SyncState](#syncstate) | 是   | 枚举值,端云同步状态|
| error | [ErrorType](#errortype) | 是   | 枚举值,同步失败错误类型|

## GallerySync

云图同步对象,用来支撑图库应用媒体资源端云同步流程。在使用前,需要先创建GallerySync实例。

### constructor

constructor()

端云同步流程的构造函数,用于获取GallerySync类的实例。

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**示例:**

  ```js
  let gallerySync = new cloudSync.GallerySync()
  ```

### on

on(evt: 'progress', callback: (pg: SyncProgress) => void): void

添加同步过程事件监听。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| evt | string | 是   | 订阅的事件类型,取值为'progress'(同步过程事件) |
| callback | (pg: SyncProgress) => void | 是   | 同步过程事件回调,回调入参为[SyncProgress](#syncprogress), 返回值为void|

**错误码:**

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

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

**示例:**

  ```js
  let gallerySync = new cloudSync.GallerySync();

C
c00382521 已提交
119 120
  gallerySync.on('progress', (pg: cloudSync.SyncProgress) => {
    console.info("syncState:" + pg.state);
C
c00382521 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  });
  ```

### off

off(evt: 'progress'): void

移除同步过程事件监听。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| evt | string | 是   | 取消订阅的事件类型,取值为'progress'(同步过程事件)|

**错误码:**

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

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

**示例:**

  ```js
  let gallerySync = new cloudSync.GallerySync();

C
c00382521 已提交
156 157
  gallerySync.on('progress', (pg: cloudSync.SyncProgress) => {
      console.info("syncState:" + pg.state);
C
c00382521 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  });

  gallerySync.off('progress');
  ```

### start

start(): Promise<void>

异步方法启动端云同步, 以Promise形式返回结果。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| 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. |
| 22400001 | Cloud status not ready. |
| 22400002 | Network unavailable. |
| 22400003  | Battery level warning. |

**示例:**

  ```js
  let gallerySync = new cloudSync.GallerySync();

C
c00382521 已提交
197 198
  gallerySync.on('progress', (pg: cloudSync.SyncProgress) => {
	  console.info("syncState:" + pg.state);
C
c00382521 已提交
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
  });

  gallerySync.start().then(function() {
	  console.info("start sync successfully");
  }).catch(function(err) {
	  console.info("start sync failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

### start

start(callback: AsyncCallback<void>): void

异步方法启动端云同步, 以callback形式返回结果。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| 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. |
| 22400001 | Cloud status not ready. |
| 22400002 | Network unavailable. |
| 22400003  | Battery level warning. |

**示例:**

  ```js
  let gallerySync = new cloudSync.GallerySync();

  gallerySync.start((err) => {
    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>

异步方法停止端云同步, 以Promise形式返回结果。

> **说明:**
>
> 调用stop接口,同步流程会停止。再次调用[start](#start)接口会继续同步。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| 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 gallerySync = new cloudSync.GallerySync();

  gallerySync.stop().then(function() {
	  console.info("stop sync successfully");
  }).catch(function(err) {
	  console.info("stop sync failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

### stop

stop(callback: AsyncCallback<void>): void

异步方法停止端云同步, 以callback形式返回结果。

> **说明:**
>
> 调用stop接口,同步流程会停止。再次调用[start](#start)接口会继续同步。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| 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 gallerySync = new cloudSync.GallerySync();

  gallerySync.stop((err) => {
    if (err) {
      console.info("stop sync failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("stop sync successfully");
    }
  });
  ```

## State

云文件下载状态,为枚举类型。

**系统能力**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

343
| 名称 |  值|  说明 |
C
c00382521 已提交
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
| ----- |  ---- |  ---- |
| RUNNING |  0 | 云文件正在下载中 |
| COMPLETED |  1 | 云文件下载完成 |
| FAILED |  2 | 云文件下载失败 |
| STOPPED |  3 | 云文件下载已停止 |

## DownloadProgress

云文件下载过程。

**系统能力**: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

| 名称     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| state | [State](#state) | 是   | 枚举值,云文件下载状态|
| processed | number | 是   | 已下载数据大小|
| size | number | 是   | 当前云文件大小|
| uri | string | 是   | 当前云文件uri|

## Download

云文件下载对象,用来支撑图库应用原图文件下载流程。在使用前,需要先创建Download实例。

### constructor

constructor()

云文件下载流程的构造函数,用于获取Download类的实例。

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**示例:**

  ```js
  let download = new cloudSync.Download()
  ```

### on

on(evt: 'progress', callback: (pg: DownloadProgress) => void): void

添加云文件下载过程事件监听。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| evt | string | 是   | 订阅的事件类型,取值为'progress'(下载过程事件)|
| callback | (pg: DownloadProgress) => void | 是   | 云文件下载过程事件回调,回调入参为[DownloadProgress](#downloadprogress), 返回值为void|

**错误码:**

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

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

**示例:**

  ```js
  let download = new cloudSync.Download();

C
c00382521 已提交
414
  download.on('progress', (pg: cloudSync.DownloadProgress) => {
C
c00382521 已提交
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
    console.info("download state:" + pg.state);
  });
  ```

### off

off(evt: 'progress'): void

移除云文件下载过程事件监听。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| evt | string | 是   | 取消订阅的事件类型,取值为'progress'(下载过程事件)|

**错误码:**

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

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

**示例:**

  ```js
  let download = new cloudSync.Download();

C
c00382521 已提交
451
  download.on('progress', (pg: cloudSync.DownloadProgress) => {
C
c00382521 已提交
452 453 454 455 456 457 458 459
      console.info("download state:" + pg.state);
  });

  download.off('progress');
  ```

### start

460
start(uri: string): Promise<void>
C
c00382521 已提交
461 462 463 464 465 466 467

异步方法启动云文件下载, 以Promise形式返回结果。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

468 469 470 471 472 473
**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| uri | string | 是   | 待下载文件uri |

C
c00382521 已提交
474 475 476 477 478 479 480 481 482 483
**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| Promise<void> | 使用Promise形式返回启动云文件下载的结果 |

**示例:**

  ```js
  let download = new cloudSync.Download();
C
c00382521 已提交
484
  let uri: string = "file:///media/Photo/1";
C
c00382521 已提交
485

C
c00382521 已提交
486
  download.on('progress', (pg: cloudSync.DownloadProgress) => {
C
c00382521 已提交
487 488 489
	  console.info("download state:" + pg.state);
  });

C
c00382521 已提交
490
  download.start(uri).then(function() {
C
c00382521 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
	  console.info("start download successfully");
  }).catch(function(err) {
	  console.info("start download failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

**错误码:**

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

| 错误码ID                     | 错误信息        |
| ---------------------------- | ---------- |
| 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

511
start(uri: string, callback: AsyncCallback<void>): void
C
c00382521 已提交
512 513 514 515 516 517 518 519 520 521 522

异步方法启动云文件下载, 以callback形式返回结果。

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
523
| uri | string | 是   | 待下载文件uri |
C
c00382521 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
| 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. |
| 13900002 | No such file or directory. |
| 13900025 | No space left on device. |

**示例:**

  ```js
  let download = new cloudSync.Download();
C
c00382521 已提交
542
  let uri: string = "file:///media/Photo/1";
C
c00382521 已提交
543

C
c00382521 已提交
544
  download.start(uri, (err) => {
C
c00382521 已提交
545 546 547 548 549 550 551 552 553 554
    if (err) {
      console.info("start download failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("start download successfully");
    }
  });
  ```

### stop

555
stop(uri: string): Promise<void>
C
c00382521 已提交
556 557 558 559 560 561 562 563 564 565 566

异步方法停止云文件下载, 以Promise形式返回结果。

> **说明:**
>
> 调用stop接口, 当前文件下载流程会终止, 缓存文件会被删除,再次调用start接口会重新开始下载

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

567 568 569 570 571 572
**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
| uri | string | 是   | 待下载文件uri |

C
c00382521 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
**返回值:**

| 类型                  | 说明             |
| --------------------- | ---------------- |
| 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 download = new cloudSync.Download();
C
c00382521 已提交
593
  let uri: string = "file:///media/Photo/1";
C
c00382521 已提交
594

C
c00382521 已提交
595
  download.stop(uri).then(function() {
C
c00382521 已提交
596 597 598 599 600 601 602 603
	  console.info("stop download successfully");
  }).catch(function(err) {
	  console.info("stop download failed with error message: " + err.message + ", error code: " + err.code);
  });
  ```

### stop

604
stop(uri: string, callback: AsyncCallback<void>): void
C
c00382521 已提交
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619

异步方法停止云文件下载, 以callback形式返回结果。

> **说明:**
>
> 调用stop接口, 当前文件下载流程会终止, 缓存文件会被删除, 再次调用start接口会重新开始下载

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

**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

**参数:**

| 参数名     | 类型   | 必填 | 说明 |
| ---------- | ------ | ---- | ---- |
620
| uri | string | 是   | 待下载文件uri |
C
c00382521 已提交
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
| 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 download = new cloudSync.Download();
C
c00382521 已提交
637
  let uri: string = "file:///media/Photo/1";
C
c00382521 已提交
638

C
c00382521 已提交
639
  download.stop(uri, (err) => {
C
c00382521 已提交
640 641 642 643 644 645 646
    if (err) {
      console.info("stop download failed with error message: " + err.message + ", error code: " + err.code);
    } else {
      console.info("stop download successfully");
    }
  });
  ```