1# @ohos.file.cloudSync (端云同步能力)(系统接口) 2 3该模块向应用提供端云同步能力,包括启动/停止端云同步以及启动/停止原图下载功能。 4 5> **说明:** 6> 7> - 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> - 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.file.cloudSync (端云同步能力)](js-apis-file-cloudsync.md)。 9 10## 导入模块 11 12```ts 13import { cloudSync } from '@kit.CoreFileKit'; 14``` 15 16## GallerySync 17 18云图同步对象,用来支撑图库应用媒体资源端云同步流程。在使用前,需要先创建GallerySync实例。 19 20### constructor 21 22constructor() 23 24端云同步流程的构造函数,用于获取GallerySync类的实例。 25 26**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 27 28**系统接口**:该接口为系统接口。 29 30**示例:** 31 32 ```ts 33 let gallerySync = new cloudSync.GallerySync() 34 ``` 35 36### on 37 38on(evt: 'progress', callback: (pg: SyncProgress) => void): void 39 40添加同步过程事件监听。 41 42**需要权限**:ohos.permission.CLOUDFILE_SYNC 43 44**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 45 46**系统接口**:该接口为系统接口。 47 48**参数:** 49 50| 参数名 | 类型 | 必填 | 说明 | 51| ---------- | ------ | ---- | ---- | 52| evt | string | 是 | 订阅的事件类型,取值为'progress'(同步过程事件)。 | 53| callback | (pg: SyncProgress) => void | 是 | 同步过程事件回调,回调入参为[SyncProgress](./js-apis-file-cloudsync.md#syncprogress12),返回值为void。| 54 55**错误码:** 56 57以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 58 59| 错误码ID | 错误信息 | 60| ---------------------------- | ---------- | 61| 201 | Permission verification failed. | 62| 202 | The caller is not a system application. | 63| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 64| 13600001 | IPC error. | 65 66**示例:** 67 68 ```ts 69 let gallerySync = new cloudSync.GallerySync(); 70 71 gallerySync.on('progress', (pg: cloudSync.SyncProgress) => { 72 console.info("syncState:" + pg.state); 73 }); 74 ``` 75 76### off 77 78off(evt: 'progress', callback: (pg: SyncProgress) => void): void 79 80移除同步过程事件监听。 81 82**需要权限**:ohos.permission.CLOUDFILE_SYNC 83 84**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 85 86**系统接口**:该接口为系统接口。 87 88**参数:** 89 90| 参数名 | 类型 | 必填 | 说明 | 91| ---------- | ------ | ---- | ---- | 92| evt | string | 是 | 取消订阅的事件类型,取值为'progress'(同步过程事件)。| 93| callback | (pg: SyncProgress) => void | 是 | 同步过程事件回调,回调入参为[SyncProgress](./js-apis-file-cloudsync.md#syncprogress12),返回值为void。| 94 95**错误码:** 96 97以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 98 99| 错误码ID | 错误信息 | 100| ---------------------------- | ---------- | 101| 201 | Permission verification failed. | 102| 202 | The caller is not a system application. | 103| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 104| 13600001 | IPC error. | 105 106**示例:** 107 108 ```ts 109 let gallerySync = new cloudSync.GallerySync(); 110 111 let callback = (pg: cloudSync.SyncProgress) => { 112 console.info("gallery sync state:" + pg.state + "error type:" + pg.error); 113 } 114 115 gallerySync.on('progress', callback); 116 117 gallerySync.off('progress', callback); 118 ``` 119 120### off 121 122off(evt: 'progress'): void 123 124移除同步过程事件监听。 125 126**需要权限**:ohos.permission.CLOUDFILE_SYNC 127 128**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 129 130**系统接口**:该接口为系统接口。 131 132**参数:** 133 134| 参数名 | 类型 | 必填 | 说明 | 135| ---------- | ------ | ---- | ---- | 136| evt | string | 是 | 取消订阅的事件类型,取值为'progress'(同步过程事件)。| 137 138**错误码:** 139 140以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 141 142| 错误码ID | 错误信息 | 143| ---------------------------- | ---------- | 144| 201 | Permission verification failed. | 145| 202 | The caller is not a system application. | 146| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 147| 13600001 | IPC error. | 148 149**示例:** 150 151 ```ts 152 let gallerySync = new cloudSync.GallerySync(); 153 154 gallerySync.on('progress', (pg: cloudSync.SyncProgress) => { 155 console.info("syncState:" + pg.state); 156 }); 157 158 gallerySync.off('progress'); 159 ``` 160 161### start 162 163start(): Promise<void> 164 165异步方法启动端云同步,以Promise形式返回结果。 166 167**需要权限**:ohos.permission.CLOUDFILE_SYNC 168 169**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 170 171**系统接口**:该接口为系统接口。 172 173**返回值:** 174 175| 类型 | 说明 | 176| --------------------- | ---------------- | 177| Promise<void> | 使用Promise形式返回启动端云同步的结果。 | 178 179**错误码:** 180 181以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 182 183| 错误码ID | 错误信息 | 184| ---------------------------- | ---------- | 185| 201 | Permission verification failed. | 186| 202 | The caller is not a system application. | 187| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. | 188| 22400001 | Cloud status not ready. | 189| 22400002 | Network unavailable. | 190| 22400003 | Low battery level. | 191 192**示例:** 193 194 ```ts 195 import { BusinessError } from '@kit.BasicServicesKit'; 196 let gallerySync = new cloudSync.GallerySync(); 197 198 gallerySync.on('progress', (pg: cloudSync.SyncProgress) => { 199 console.info("syncState:" + pg.state); 200 }); 201 202 gallerySync.start().then(() => { 203 console.info("start sync successfully"); 204 }).catch((err: BusinessError) => { 205 console.error("start sync failed with error message: " + err.message + ", error code: " + err.code); 206 }); 207 ``` 208 209### start 210 211start(callback: AsyncCallback<void>): void 212 213异步方法启动端云同步, 以callback形式返回结果。 214 215**需要权限**:ohos.permission.CLOUDFILE_SYNC 216 217**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 218 219**系统接口**:该接口为系统接口。 220 221**参数:** 222 223| 参数名 | 类型 | 必填 | 说明 | 224| ---------- | ------ | ---- | ---- | 225| callback | AsyncCallback<void> | 是 | 异步启动端云同步的回调。 | 226 227**错误码:** 228 229以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 230 231| 错误码ID | 错误信息 | 232| ---------------------------- | ---------- | 233| 201 | Permission verification failed. | 234| 202 | The caller is not a system application. | 235| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 236| 22400001 | Cloud status not ready. | 237| 22400002 | Network unavailable. | 238| 22400003 | Low battery level. | 239 240**示例:** 241 242 ```ts 243 import { BusinessError } from '@kit.BasicServicesKit'; 244 let gallerySync = new cloudSync.GallerySync(); 245 246 gallerySync.start((err: BusinessError) => { 247 if (err) { 248 console.error("start sync failed with error message: " + err.message + ", error code: " + err.code); 249 } else { 250 console.info("start sync successfully"); 251 } 252 }); 253 ``` 254 255### stop 256 257stop(): Promise<void> 258 259异步方法停止端云同步,以Promise形式返回结果。 260 261> **说明:** 262> 263> 调用stop接口,同步流程会停止。再次调用[start](#start)接口会继续同步。 264 265**需要权限**:ohos.permission.CLOUDFILE_SYNC 266 267**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 268 269**系统接口**:该接口为系统接口。 270 271**返回值:** 272 273| 类型 | 说明 | 274| --------------------- | ---------------- | 275| Promise<void> | 使用Promise形式返回停止端云同步的结果。 | 276 277**错误码:** 278 279以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 280 281| 错误码ID | 错误信息 | 282| ---------------------------- | ---------- | 283| 201 | Permission verification failed. | 284| 202 | The caller is not a system application. | 285| 401 | The input parameter is invalid. Possible causes:Incorrect parameter types. | 286 287**示例:** 288 289 ```ts 290 import { BusinessError } from '@kit.BasicServicesKit'; 291 let gallerySync = new cloudSync.GallerySync(); 292 293 gallerySync.stop().then(() => { 294 console.info("stop sync successfully"); 295 }).catch((err: BusinessError) => { 296 console.error("stop sync failed with error message: " + err.message + ", error code: " + err.code); 297 }); 298 ``` 299 300### stop 301 302stop(callback: AsyncCallback<void>): void 303 304异步方法停止端云同步,以callback形式返回结果。 305 306> **说明:** 307> 308> 调用stop接口,同步流程会停止。再次调用[start](#start)接口会继续同步。 309 310**需要权限**:ohos.permission.CLOUDFILE_SYNC 311 312**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 313 314**系统接口**:该接口为系统接口。 315 316**参数:** 317 318| 参数名 | 类型 | 必填 | 说明 | 319| ---------- | ------ | ---- | ---- | 320| callback | AsyncCallback<void> | 是 | 异步停止端云同步的回调。 | 321 322**错误码:** 323 324以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 325 326| 错误码ID | 错误信息 | 327| ---------------------------- | ---------- | 328| 201 | Permission verification failed. | 329| 202 | The caller is not a system application. | 330| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 331 332**示例:** 333 334 ```ts 335 import { BusinessError } from '@kit.BasicServicesKit'; 336 let gallerySync = new cloudSync.GallerySync(); 337 338 gallerySync.stop((err: BusinessError) => { 339 if (err) { 340 console.error("stop sync failed with error message: " + err.message + ", error code: " + err.code); 341 } else { 342 console.info("stop sync successfully"); 343 } 344 }); 345 ``` 346 347## Download 348 349云文件下载对象,用来支撑图库应用原图文件下载流程。在使用前,需要先创建Download实例。 350 351### constructor 352 353constructor() 354 355云文件下载流程的构造函数,用于获取Download类的实例。 356 357**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 358 359**系统接口**:该接口为系统接口。 360 361**示例:** 362 363 ```ts 364 let download = new cloudSync.Download() 365 ``` 366 367### on 368 369on(evt: 'progress', callback: (pg: DownloadProgress) => void): void 370 371添加云文件下载过程事件监听。 372 373**需要权限**:ohos.permission.CLOUDFILE_SYNC 374 375**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 376 377**系统接口**:该接口为系统接口。 378 379**参数:** 380 381| 参数名 | 类型 | 必填 | 说明 | 382| ---------- | ------ | ---- | ---- | 383| evt | string | 是 | 订阅的事件类型,取值为'progress'(下载过程事件)。| 384| callback | (pg: DownloadProgress) => void | 是 | 云文件下载过程事件回调,回调入参为[DownloadProgress](js-apis-file-cloudsync.md#downloadprogress11),返回值为void。| 385 386**错误码:** 387 388以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 389 390| 错误码ID | 错误信息 | 391| ---------------------------- | ---------- | 392| 201 | Permission verification failed. | 393| 202 | The caller is not a system application. | 394| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 395| 13600001 | IPC error. | 396 397**示例:** 398 399 ```ts 400 let download = new cloudSync.Download(); 401 402 download.on('progress', (pg: cloudSync.DownloadProgress) => { 403 console.info("download state:" + pg.state); 404 }); 405 ``` 406 407### off 408 409off(evt: 'progress', callback: (pg: DownloadProgress) => void): void 410 411移除云文件下载过程事件监听。 412 413**需要权限**:ohos.permission.CLOUDFILE_SYNC 414 415**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 416 417**系统接口**:该接口为系统接口。 418 419**参数:** 420 421| 参数名 | 类型 | 必填 | 说明 | 422| ---------- | ------ | ---- | ---- | 423| evt | string | 是 | 取消订阅的事件类型,取值为'progress'(同步过程事件)。| 424| callback | (pg: DownloadProgress) => void | 是 | 云文件下载过程事件回调,回调入参为[DownloadProgress](js-apis-file-cloudsync.md#downloadprogress11), 返回值为void。| 425 426**错误码:** 427 428以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 429 430| 错误码ID | 错误信息 | 431| ---------------------------- | ---------- | 432| 201 | Permission verification failed. | 433| 202 | The caller is not a system application. | 434| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 435| 13600001 | IPC error. | 436 437**示例:** 438 439 ```ts 440 let download = new cloudSync.Download(); 441 442 let callback = (pg: cloudSync.DownloadProgress) => { 443 console.info("download state:" + pg.state); 444 } 445 446 download.on('progress', callback); 447 448 download.off('progress', callback); 449 ``` 450 451### off 452 453off(evt: 'progress'): void 454 455移除云文件下载过程事件监听。 456 457**需要权限**:ohos.permission.CLOUDFILE_SYNC 458 459**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 460 461**系统接口**:该接口为系统接口。 462 463**参数:** 464 465| 参数名 | 类型 | 必填 | 说明 | 466| ---------- | ------ | ---- | ---- | 467| evt | string | 是 | 取消订阅的事件类型,取值为'progress'(下载过程事件)。| 468 469**错误码:** 470 471以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 472 473| 错误码ID | 错误信息 | 474| ---------------------------- | ---------- | 475| 201 | Permission verification failed. | 476| 202 | The caller is not a system application. | 477| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 478| 13600001 | IPC error. | 479 480**示例:** 481 482 ```ts 483 let download = new cloudSync.Download(); 484 485 download.on('progress', (pg: cloudSync.DownloadProgress) => { 486 console.info("download state:" + pg.state); 487 }); 488 489 download.off('progress'); 490 ``` 491 492### start 493 494start(uri: string): Promise<void> 495 496异步方法启动云文件下载,以Promise形式返回结果。 497 498**需要权限**:ohos.permission.CLOUDFILE_SYNC 499 500**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 501 502**系统接口**:该接口为系统接口。 503 504**参数:** 505 506| 参数名 | 类型 | 必填 | 说明 | 507| ---------- | ------ | ---- | ---- | 508| uri | string | 是 | 待下载文件uri。 | 509 510**返回值:** 511 512| 类型 | 说明 | 513| --------------------- | ---------------- | 514| Promise<void> | 使用Promise形式返回启动云文件下载的结果。 | 515 516**示例:** 517 518 ```ts 519 import { BusinessError } from '@kit.BasicServicesKit'; 520 let download = new cloudSync.Download(); 521 let uri: string = "file:///media/Photo/1"; 522 523 download.on('progress', (pg: cloudSync.DownloadProgress) => { 524 console.info("download state:" + pg.state); 525 }); 526 527 download.start(uri).then(() => { 528 console.info("start download successfully"); 529 }).catch((err: BusinessError) => { 530 console.error("start download failed with error message: " + err.message + ", error code: " + err.code); 531 }); 532 ``` 533 534**错误码:** 535 536以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 537 538| 错误码ID | 错误信息 | 539| ---------------------------- | ---------- | 540| 201 | Permission verification failed. | 541| 202 | The caller is not a system application. | 542| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 543| 13900002 | No such file or directory. | 544| 13900025 | No space left on device. | 545 546### start 547 548start(uri: string, callback: AsyncCallback<void>): void 549 550异步方法启动云文件下载,以callback形式返回结果。 551 552**需要权限**:ohos.permission.CLOUDFILE_SYNC 553 554**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 555 556**系统接口**:该接口为系统接口。 557 558**参数:** 559 560| 参数名 | 类型 | 必填 | 说明 | 561| ---------- | ------ | ---- | ---- | 562| uri | string | 是 | 待下载文件uri。 | 563| callback | AsyncCallback<void> | 是 | 异步启动云文件下载的回调。 | 564 565**错误码:** 566 567以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 568 569| 错误码ID | 错误信息 | 570| ---------------------------- | ---------- | 571| 201 | Permission verification failed. | 572| 202 | The caller is not a system application. | 573| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 574| 13900002 | No such file or directory. | 575| 13900025 | No space left on device. | 576 577**示例:** 578 579 ```ts 580 import { BusinessError } from '@kit.BasicServicesKit'; 581 let download = new cloudSync.Download(); 582 let uri: string = "file:///media/Photo/1"; 583 584 download.start(uri, (err: BusinessError) => { 585 if (err) { 586 console.error("start download failed with error message: " + err.message + ", error code: " + err.code); 587 } else { 588 console.info("start download successfully"); 589 } 590 }); 591 ``` 592 593### stop 594 595stop(uri: string): Promise<void> 596 597异步方法停止云文件下载,以Promise形式返回结果。 598 599> **说明:** 600> 601> 调用stop接口,当前文件下载流程会终止,缓存文件会被删除,再次调用start接口会重新开始下载。 602 603**需要权限**:ohos.permission.CLOUDFILE_SYNC 604 605**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 606 607**系统接口**:该接口为系统接口。 608 609**参数:** 610 611| 参数名 | 类型 | 必填 | 说明 | 612| ---------- | ------ | ---- | ---- | 613| uri | string | 是 | 待下载文件uri。 | 614 615**返回值:** 616 617| 类型 | 说明 | 618| --------------------- | ---------------- | 619| Promise<void> | 使用Promise形式返回停止云文件下载的结果。 | 620 621**错误码:** 622 623以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 624 625| 错误码ID | 错误信息 | 626| ---------------------------- | ---------- | 627| 201 | Permission verification failed. | 628| 202 | The caller is not a system application. | 629| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 630 631**示例:** 632 633 ```ts 634 import { BusinessError } from '@kit.BasicServicesKit'; 635 let download = new cloudSync.Download(); 636 let uri: string = "file:///media/Photo/1"; 637 638 download.stop(uri).then(() => { 639 console.info("stop download successfully"); 640 }).catch((err: BusinessError) => { 641 console.error("stop download failed with error message: " + err.message + ", error code: " + err.code); 642 }); 643 ``` 644 645### stop 646 647stop(uri: string, callback: AsyncCallback<void>): void 648 649异步方法停止云文件下载,以callback形式返回结果。 650 651> **说明:** 652> 653> 调用stop接口,当前文件下载流程会终止,缓存文件会被删除,再次调用start接口会重新开始下载。 654 655**需要权限**:ohos.permission.CLOUDFILE_SYNC 656 657**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 658 659**系统接口**:该接口为系统接口。 660 661**参数:** 662 663| 参数名 | 类型 | 必填 | 说明 | 664| ---------- | ------ | ---- | ---- | 665| uri | string | 是 | 待下载文件uri。 | 666| callback | AsyncCallback<void> | 是 | 异步停止云文件下载的回调。 | 667 668**错误码:** 669 670以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 671 672| 错误码ID | 错误信息 | 673| ---------------------------- | ---------- | 674| 201 | Permission verification failed. | 675| 202 | The caller is not a system application. | 676| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 677 678**示例:** 679 680 ```ts 681 import { BusinessError } from '@kit.BasicServicesKit'; 682 let download = new cloudSync.Download(); 683 let uri: string = "file:///media/Photo/1"; 684 685 download.stop(uri, (err: BusinessError) => { 686 if (err) { 687 console.error("stop download failed with error message: " + err.message + ", error code: " + err.code); 688 } else { 689 console.info("stop download successfully"); 690 } 691 }); 692 ``` 693 694## FileSync<sup>12+</sup> 695 696云盘同步对象,用于支撑文件管理器应用完成云盘文件的端云同步流程。在使用前,需要先创建FileSync实例。 697 698### constructor<sup>12+</sup> 699 700constructor(bundleName: string) 701 702端云同步流程的构造函数,用于获取FileSync类的实例。 703 704**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 705 706**系统接口**:该接口为系统接口。 707 708**参数:** 709 710| 参数名 | 类型 | 必填 | 说明 | 711| ---------- | ------ | ---- | ---- | 712| bundleName | string | 是 | 应用包名。| 713 714**错误码:** 715 716以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 717 718| 错误码ID | 错误信息 | 719| ---------------------------- | ---------- | 720| 202 | Permission verification failed, application which is not a system application uses system API. | 721| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 722 723**示例:** 724 725 ```ts 726 let fileSync = new cloudSync.FileSync("com.ohos.demo") 727 ``` 728 729## CloudFileCache<sup>11+</sup> 730 731云盘文件缓存对象,用来支撑文件管理应用原文件下载流程。 732 733**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 734 735### cleanCache<sup>11+</sup> 736 737cleanCache(uri: string): void 738 739同步方法删除文件缓存。 740 741**需要权限**:ohos.permission.CLOUDFILE_SYNC 742 743**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 744 745**系统接口**:该接口为系统接口。 746 747**参数:** 748 749| 参数名 | 类型 | 必填 | 说明 | 750| ---------- | ------ | ---- | ---- | 751| uri | string | 是 | 待删除缓存文件的uri。| 752 753**错误码:** 754 755以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 756 757| 错误码ID | 错误信息 | 758| ---------------------------- | ---------- | 759| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 760| 202 | Permission verification failed, application which is not a system application uses system API. | 761| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 762| 13600001 | IPC error. | 763| 13900002 | No such file or directory. | 764| 14000002 | Invalid URI. | 765 766**示例:** 767 768 ```ts 769 import { BusinessError } from '@kit.BasicServicesKit'; 770 import { fileUri } from '@kit.CoreFileKit'; 771 let fileCache = new cloudSync.CloudFileCache(); 772 let path = "/data/storage/el2/cloud/1.txt"; 773 let uri = fileUri.getUriFromPath(path); 774 775 try { 776 fileCache.cleanCache(uri); 777 } catch (err) { 778 let error:BusinessError = err as BusinessError; 779 console.error("clean cache failed with error message: " + err.message + ", error code: " + err.code); 780 } 781 782 ``` 783 784## cloudSync.getFileSyncState<sup>11+</sup> 785 786getFileSyncState(uri: Array<string>): Promise<Array<FileSyncState>> 787 788异步方法获取文件同步状态,以promise形式返回结果。 789 790**需要权限**:ohos.permission.CLOUDFILE_SYNC 791 792**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 793 794**系统接口**:该接口为系统接口。 795 796**参数:** 797 798| 参数名 | 类型 | 必填 | 说明 | 799| ---------- | ------ | ---- | ---- | 800| uri | Array<string> | 是 | 待获取同步状态的uri。 | 801 802**返回值:** 803 804| 类型 | 说明 | 805| --------------------- | ---------------- | 806| Promise<Array<FileSyncState>> | 使用Promise形式返回文件同步状态的结果。 | 807 808**错误码:** 809 810以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 811 812| 错误码ID | 错误信息 | 813| ---------------------------- | ---------- | 814| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 815| 202 | Permission verification failed, application which is not a system application uses system API. | 816| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 817| 13600001 | IPC error. | 818| 13900002 | No such file or directory. | 819| 14000002 | Invalid URI. | 820 821**示例:** 822 823 ```ts 824 import { BusinessError } from '@kit.BasicServicesKit'; 825 826 let uris: Array<string> = ["file://uri"]; 827 cloudSync.getFileSyncState(uris).then((syncStates: Array<cloudSync.FileSyncState>) => { 828 for(let i = 0, len = syncStates.length; i < len; i++){ 829 console.info("get file sync state successfully" + syncStates[i]); 830 } 831 }).catch((err: BusinessError) => { 832 console.error("get file sync state failed with error message: " + err.message + ", error code: " + err.code); 833 }); 834 835 ``` 836 837## cloudSync.getFileSyncState<sup>11+</sup> 838 839getFileSyncState(uri: Array<string>, callback: AsyncCallback<Array<FileSyncState>>): void 840 841异步方法获取文件同步状态,以callback形式返回结果。 842 843**需要权限**:ohos.permission.CLOUDFILE_SYNC 844 845**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 846 847**系统接口**:该接口为系统接口。 848 849**参数:** 850 851| 参数名 | 类型 | 必填 | 说明 | 852| ---------- | ------ | ---- | ---- | 853| uri | Array<string> | 是 | 待获取同步状态的uri。 | 854| callback | AsyncCallback<Array<FileSyncState>> | 是 | 异步获取文件状态的回调。| 855 856**错误码:** 857 858以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 859 860| 错误码ID | 错误信息 | 861| ---------------------------- | ---------- | 862| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 863| 202 | Permission verification failed, application which is not a system application uses system API. | 864| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 865| 13600001 | IPC error. | 866| 13900002 | No such file or directory. | 867| 14000002 | Invalid URI. | 868 869**示例:** 870 871 ```ts 872 import { BusinessError } from '@kit.BasicServicesKit'; 873 874 let uris: Array<string> = ["file://uri"]; 875 cloudSync.getFileSyncState(uris, (err: BusinessError, syncStates: Array<cloudSync.FileSyncState>) => { 876 if (err) { 877 console.error("get file sync state with error message: " + err.message + ", error code: " + err.code); 878 } else { 879 for(let i = 0, len = syncStates.length; i < len; i++){ 880 console.info("get file sync state successfully" + syncStates[i]); 881 } 882 } 883 }); 884 ``` 885 886## cloudSync.getFileSyncState<sup>12+</sup> 887 888getFileSyncState(uri: string): FileSyncState 889 890获取文件同步状态。 891 892**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 893 894**系统接口**:该接口为系统接口。 895 896**参数:** 897 898| 参数名 | 类型 | 必填 | 说明 | 899| ---------- | ------ | ---- | ---- | 900| uri | string | 是 | 待下载文件uri。 | 901 902**返回值:** 903 904| 类型 | 说明 | 905| --------------------- | ---------------- | 906| [FileSyncState](#filesyncstate11) | 返回给定文件的同步状态。 | 907 908**错误码:** 909 910以下错误码的详细介绍请参见[文件管理子系统错误码](errorcode-filemanagement.md)。 911 912| 错误码ID | 错误信息 | 913| ---------------------------- | ---------- | 914| 202 | Permission verification failed, application which is not a system application uses system API. | 915| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 916| 13900002 | No such file or directory. | 917| 13900004 | Interrupted system call. | 918| 13900010 | Try again. | 919| 13900012 | Permission denied by the file system. | 920| 13900031 | Function not implemented. | 921| 13900042 | Unknown error. | 922| 14000002 | Invalid URI. | 923 924**示例:** 925 926 ```ts 927 import { BusinessError } from '@kit.BasicServicesKit'; 928 import { fileUri } from '@kit.CoreFileKit'; 929 let path = "/data/storage/el2/cloud/1.txt"; 930 let uri = fileUri.getUriFromPath(path); 931 try { 932 let state = fileSync.getFileSyncState(uri) 933 }.catch(err) { 934 let error:BusinessError = err as BusinessError; 935 console.error("getFileSyncStatefailed with error:" + JSON.stringify(error)); 936 } 937 ``` 938 939## FileSyncState<sup>11+</sup> 940 941端云文件同步状态,为枚举类型。 942 943**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 944 945**系统接口**:该接口为系统接口。 946 947| 名称 | 值| 说明 | 948| ----- | ---- | ---- | 949| UPLOADING | 0 | 上行同步中。 | 950| DOWNLOADING | 1 | 下行同步中。 | 951| COMPLETED | 2 | 同步成功。 | 952| STOPPED | 3 | 同步已停止。 | 953| TO_BE_UPLOADED<sup>12+</sup> | 4 | 正在等待上行。 | 954| UPLOAD_SUCCESS<sup>12+</sup> | 5 | 文件已成功上行。 | 955| UPLOAD_FAILURE<sup>12+</sup> | 6 | 文件上行失败。 | 956 957## cloudSync.optimizeStorage<sup>17+</sup> 958 959optimizeStorage(): Promise<void> 960 961优化图库已同步云空间的本地资源,按照本地剩余空间执行自动老化策略。使用Promise异步回调。 962 963**需要权限**:ohos.permission.CLOUDFILE_SYNC 964 965**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 966 967**系统接口**:此接口为系统接口。 968 969**返回值:** 970 971 | 类型 | 说明 | 972 | ------------------- | ---------------------------- | 973 | Promise<void> | 无返回结果的Promise对象。 | 974 975**错误码:** 976 977以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理子系统错误码](errorcode-filemanagement.md)。 978 979| 错误码ID | 错误信息 | 980| ---------------------------- | ---------- | 981| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 982| 202 | Permission verification failed, application which is not a system application uses system API. | 983| 13600001 | IPC error. | 984| 13900042 | Unknown error. | 985 986**示例:** 987 988 ```ts 989 import { BusinessError } from '@kit.BasicServicesKit'; 990 991 cloudSync.optimizeStorage().then(() => { 992 console.info("optimize storage successfully"); // 前台UX按需阻塞等待 993 }).catch((err: BusinessError) => { 994 console.error("optimize storage failed with error message: " + err.message + ", error code: " + err.code); 995 }); 996 ``` 997 998## cloudSync.startOptimizeSpace<sup>17+</sup> 999 1000startOptimizeSpace(optimizePara: OptimizeSpacePara, callback?: Callback\<OptimizeSpaceProgress>): Promise<void> 1001 1002优化图库已同步云空间的本地资源,执行立即优化空间策略,对老化天数前未访问的本地图片/视频进行优化。使用Promise异步回调。 1003 1004startOptimizeSpace的使用和stopOptimizeSpace方法调用一一对应,重复开启将返回其他任务正在执行的错误信息(22400006)。 1005 1006**需要权限**:ohos.permission.CLOUDFILE_SYNC 1007 1008**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 1009 1010**系统接口**:此接口为系统接口。 1011 1012**参数:** 1013 1014| 参数名 | 类型 | 必填 | 说明 | 1015| ---------- | ------ | ---- | ---- | 1016| para | [OptimizeSpacePara](#optimizespacepara17) | 是 | 优化参数。 | 1017| callback | Callback<[OptimizeSpaceProgress](#optimizespaceprogress17)> | 否 | 返回优化进度。缺省情况下返回401错误,不执行清理任务 | 1018 1019**返回值:** 1020 1021 | 类型 | 说明 | 1022 | ------------------- | ---------------------------- | 1023 | Promise<void> | 无返回结果的Promise对象。 | 1024 1025**错误码:** 1026 1027以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理子系统错误码](errorcode-filemanagement.md)。 1028 1029| 错误码ID | 错误信息 | 1030| ---------------------------- | ---------- | 1031| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 1032| 202 | Permission verification failed, application which is not a system application uses system API. | 1033| 401 | The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | 1034| 13600001 | IPC error. | 1035| 22400005 | Inner error. | 1036| 22400006 | Other optimize task is running. | 1037 1038**示例:** 1039 1040 ```ts 1041 import { BusinessError } from '@kit.BasicServicesKit'; 1042 let para:cloudSync.OptimizeSpacePara = {totalSize: 1073741824, agingDays: 30}; 1043 let callback = (data:cloudSync.OptimizeSpaceProgress) => { 1044 if (data.state == cloudSync.OptimizeState.FAILED) { 1045 console.info("optimize space failed"); 1046 } else if (data.state == cloudSync.OptimizeState.COMPLETED && data.progress == 100) { 1047 console.info("optimize space successfully"); 1048 } else if (data.state == cloudSync.OptimizeState.RUNNING) { 1049 console.info("optimize space progress:" + data.progress); 1050 } 1051 } 1052 cloudSync.startOptimizeSpace(para, callback).then(() => { 1053 console.info("start optimize space"); 1054 }).catch((err: BusinessError) => { 1055 console.error("start optimize space failed with error message: " + err.message + ", error code: " + err.code); 1056 }); 1057 ``` 1058 1059## cloudSync.stopOptimizeSpace<sup>17+</sup> 1060 1061stopOptimizeSpace(): void 1062 1063同步方法停止图库云图资源空间优化,和startOptimizeSpace配对使用。 1064 1065**需要权限**:ohos.permission.CLOUDFILE_SYNC 1066 1067**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 1068 1069**系统接口**:此接口为系统接口。 1070 1071**错误码:** 1072 1073以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[文件管理子系统错误码](errorcode-filemanagement.md)。 1074 1075| 错误码ID | 错误信息 | 1076| ---------------------------- | ---------- | 1077| 201 | Permission verification failed, usually the result returned by VerifyAccessToken. | 1078| 202 | Permission verification failed, application which is not a system application uses system API. | 1079| 13600001 | IPC error. | 1080| 22400005 | Inner error. | 1081 1082**示例:** 1083 1084 ```ts 1085 import { BusinessError } from '@kit.BasicServicesKit'; 1086 let para:cloudSync.OptimizeSpacePara = {totalSize: 1073741824, agingDays: 30}; 1087 let callback = (data:cloudSync.OptimizeSpaceProgress) => { 1088 if (data.state == cloudSync.OptimizeState.FAILED) { 1089 console.info("optimize space failed"); 1090 } else if (data.state == cloudSync.OptimizeState.RUNNING) { 1091 console.info("optimize space progress:" + data.progress); 1092 } 1093 } 1094 cloudSync.startOptimizeSpace(para, callback); 1095 cloudSync.stopOptimizeSpace(); // 停止空间优化 1096 ``` 1097 1098## OptimizeState<sup>17+</sup> 1099 1100优化空间状态,为枚举类型。 1101 1102**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 1103 1104**系统接口**:此接口为系统接口。 1105 1106| 名称 | 值| 说明 | 1107| ----- | ---- | ---- | 1108| RUNNING | 0 | 正在优化空间。 | 1109| COMPLETED | 1 | 优化空间成功结束。 | 1110| FAILED | 2 | 优化空间失败。 | 1111| STOPPED | 3 | 优化空间停止。 | 1112 1113## OptimizeSpaceProgress<sup>17+</sup> 1114 1115立即优化空间状态和当前进度。 1116 1117**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 1118 1119**系统接口**:此接口为系统接口。 1120 1121| 名称 | 类型 | 必填 | 说明 | 1122| ---------- | ------ | ---- | ---- | 1123| state | [OptimizeState](#optimizestate17) | 是 | 枚举值,优化空间状态。| 1124| progress | number | 是 | 优化进度百分比,范围[0,100]。| 1125 1126## OptimizeSpacePara<sup>17+</sup> 1127 1128立即优化空间设置参数,设置优化总空间和老化天数。 1129 1130**系统能力**:SystemCapability.FileManagement.DistributedFileService.CloudSync.Core 1131 1132**系统接口**:此接口为系统接口。 1133 1134| 名称 | 类型 | 必填 | 说明 | 1135| ---------- | ------ | ---- | ---- | 1136| totalSize | number | 是 | 优化空间总大小。查询媒体库接口获得需要老化的所有文件总大小,由应用传入,单位byte。| 1137| agingDays | number | 是 | 老化天数。系统会以当前时间为基准,优化老化天数前未访问、已同步云空间的本地图片/视频。|