openharmony 鸿蒙 js-apis-file-cloudsync-sys

2025-06-12 浏览 (1)

@ohos.file.cloudSync (Device-Cloud Sync) (System API)

The cloudSync module provides the device-cloud sync capabilities for applications. You can use the APIs to start or stop device-cloud sync 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.
  • This topic describes only the system APIs provided by the module. For details about its public APIs, see @ohos.file.cloudSync (Device-Cloud Sync Capability).

Modules to Import

import { cloudSync } from '@kit.CoreFileKit';

GallerySync

Provides APIs to implement device-cloud sync 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

let gallerySync = new cloudSync.GallerySync()

on

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

Registers a listener for the device-cloud sync progress.

Required permissions: ohos.permission.CLOUDFILE_SYNC

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
evtstringYesEvent type. The value is progress, which indicates the sync progress event.
callback(pg: SyncProgress) => voidYesCallback of the sync progress event. The input parameter is SyncProgress, and the return value is void.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

Example

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 all listeners for the device-cloud sync progress.

Required permissions: ohos.permission.CLOUDFILE_SYNC

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
evtstringYesEvent type. The value is progress, which indicates the sync progress event.
callback(pg: SyncProgress) => voidYesCallback of the sync progress event. The input parameter is SyncProgress, and the return value is void.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

Example

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 sync progress.

Required permissions: ohos.permission.CLOUDFILE_SYNC

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
evtstringYesEvent type. The value is progress, which indicates the sync progress event.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

Example

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 sync. 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

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes:Incorrect parameter types.
22400001Cloud status not ready.
22400002Network unavailable.
22400003Low battery level.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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.error("start sync failed with error message: " + err.message + ", error code: " + err.code);
});

start

start(callback: AsyncCallback<void>): void

Starts device-cloud sync. 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

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
22400001Cloud status not ready.
22400002Network unavailable.
22400003Low battery level.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let gallerySync = new cloudSync.GallerySync();

gallerySync.start((err: BusinessError) => {
  if (err) {
    console.error("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 sync. This API uses a promise to return the result.

NOTE

Calling stop will stop the sync process. To resume the sync, call start.

Required permissions: ohos.permission.CLOUDFILE_SYNC

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes:Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let gallerySync = new cloudSync.GallerySync();

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

stop

stop(callback: AsyncCallback<void>): void

Stops device-cloud sync. This API uses an asynchronous callback to return the result.

NOTE

Calling stop will stop the sync process. To resume the sync, call start.

Required permissions: ohos.permission.CLOUDFILE_SYNC

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let gallerySync = new cloudSync.GallerySync();

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

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

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

NameTypeMandatoryDescription
evtstringYesEvent. The value is progress, which indicates the download progress event of a cloud file.
callback(pg: DownloadProgress) => voidYesCallback used to return the file download progress. The input parameter is DownloadProgress, and the return value is void.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

Example

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 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

NameTypeMandatoryDescription
evtstringYesEvent type. The value is progress, which indicates the sync progress event.
callback(pg: DownloadProgress) => voidYesCallback used to return the file download progress. The input parameter is DownloadProgress, and the return value is void.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

Example

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

NameTypeMandatoryDescription
evtstringYesEvent 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.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.

Example

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

NameTypeMandatoryDescription
uristringYesURI of the target file.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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.error("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.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13900002No such file or directory.
13900025No 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

NameTypeMandatoryDescription
uristringYesURI of the target file.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13900002No such file or directory.
13900025No space left on device.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let download = new cloudSync.Download();
let uri: string = "file:///media/Photo/1";

download.start(uri, (err: BusinessError) => {
  if (err) {
    console.error("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

NameTypeMandatoryDescription
uristringYesURI of the target file.

Return value

TypeDescription
Promise<void>Promise used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
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.error("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

NameTypeMandatoryDescription
uristringYesURI of the target file.
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed.
202The caller is not a system application.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let download = new cloudSync.Download();
let uri: string = "file:///media/Photo/1";

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

FileSync12+

Provides APIs for the file manager application to perform device-cloud sync of the files stored in the Drive Kit. Before using the APIs of this class, you need to create a FileSync instance.

constructor12+

constructor(bundleName: string)

A constructor used to create a FileSync instance.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
bundleNamestringYesBundle name.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.

Example

let fileSync = new cloudSync.FileSync("com.ohos.demo")

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

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

NameTypeMandatoryDescription
uristringYesURI of the cache file to delete.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13900002No such file or directory.
14000002Invalid URI.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { fileUri } from '@kit.CoreFileKit';
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.error("clean cache failed with error message: " + err.message + ", error code: " + err.code);
} 

cloudSync.getFileSyncState11+

getFileSyncState(uri: Array<string>): Promise<Array<FileSyncState>>

Obtains the file sync 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

NameTypeMandatoryDescription
uriArray<string>YesURI of the file whose sync state is to be obtained.

Return value

TypeDescription
Promise<Array<FileSyncState>>Promise used to return the sync state obtained.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13900002No such file or directory.
14000002Invalid URI.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let uris: Array<string> = ["file://uri"];
cloudSync.getFileSyncState(uris).then((syncStates: Array<cloudSync.FileSyncState>) => {
  for(let i = 0, len = syncStates.length; i < len; i++){
      console.info("get file sync state successfully" + syncStates[i]);
  }
}).catch((err: BusinessError) => {
    console.error("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 sync 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

NameTypeMandatoryDescription
uriArray<string>YesURI of the file whose sync state is to be obtained.
callbackAsyncCallback<Array<FileSyncState>>YesCallback used to return the file sync state.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
13900002No such file or directory.
14000002Invalid URI.

Example

import { BusinessError } from '@kit.BasicServicesKit';

let uris: Array<string> = ["file://uri"];
cloudSync.getFileSyncState(uris, (err: BusinessError, syncStates: Array<cloudSync.FileSyncState>) => {
  if (err) {
    console.error("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]);
  }
  }
});

cloudSync.getFileSyncState12+

getFileSyncState(uri: string): FileSyncState

Obtains the file sync state.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
uristringYesURI of the target file.

Return value

TypeDescription
FileSyncStateSync state of the file.

Error codes

For details about the error codes, see File Management Error Codes.

IDError Message
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13900002No such file or directory.
13900004Interrupted system call.
13900010Try again.
13900012Permission denied by the file system.
13900031Function not implemented.
13900042Unknown error.
14000002Invalid URI.

Example

import { BusinessError } from '@kit.BasicServicesKit';
import { fileUri } from '@kit.CoreFileKit';
let path = "/data/storage/el2/cloud/1.txt";
let uri = fileUri.getUriFromPath(path);
try {
  let state = cloudSync.getFileSyncState(uri);
} catch (err) {
  let error:BusinessError = err as BusinessError;
  console.error("getFileSyncStatefailed with error:" + JSON.stringify(error));
}

FileSyncState11+

Enumerates the device-cloud file sync states.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

NameValueDescription
UPLOADING0The file is being uploaded.
DOWNLOADING1The file is being downloaded.
COMPLETED2Sync completed.
STOPPED3Sync stopped.
TO_BE_UPLOADED12+4The file is going to be uploaded.
UPLOAD_SUCCESS12+5The file has been successfully uploaded.
UPLOAD_FAILURE12+6The file fails to be uploaded.

cloudSync.optimizeStorage17+

optimizeStorage(): Promise<void>

Optimizes the resources from the local Gallery that have been synced to the cloud and executes the automatic aging policy according to the remaining local space. 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

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and File Management Error Codes.

IDError Message
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
13600001IPC error.
13900042Unknown error.

Example

import { BusinessError } from '@kit.BasicServicesKit';

cloudSync.optimizeStorage().then(() => {
    console.info("optimize storage successfully");   // The foreground UX waits for blocking operations to complete.
}).catch((err: BusinessError) => {
    console.error("optimize storage failed with error message: " + err.message + ", error code: " + err.code);
});

cloudSync.startOptimizeSpace17+

startOptimizeSpace(optimizePara: OptimizeSpaceParam, callback?: Callback<OptimizeSpaceProgress>): Promise<void>

Optimizes local resources that have been synced to the cloud and optimizes local images and videos that have not been accessed before the aging period expires. This API uses a promise to return the result.

startOptimizeSpace is used together with stopOptimizeSpace. If startOptimizeSpace is called repeatedly, the error code 22400006 will be returned, indicating that other tasks are being executed.

Required permissions: ohos.permission.CLOUDFILE_SYNC

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Parameters

NameTypeMandatoryDescription
optimizeParaOptimizeSpaceParamYesOptimizes parameters.
callbackCallback<OptimizeSpaceProgress>NoCallback used to return the optimization progress. By default, error 401 is returned and the clearing task is not executed.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and File Management Error Codes.

IDError Message
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
401The input parameter is invalid. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.
13600001IPC error.
22400005Inner error.
22400006The same task is already in progress.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let para:cloudSync.OptimizeSpaceParam = {totalSize: 1073741824, agingDays: 30};
let callback = (data:cloudSync.OptimizeSpaceProgress) => {
  if (data.state == cloudSync.OptimizeState.FAILED) {
    console.info("optimize space failed");
  } else if (data.state == cloudSync.OptimizeState.COMPLETED && data.progress == 100) {
    console.info("optimize space successfully");
  } else if (data.state == cloudSync.OptimizeState.RUNNING) {
    console.info("optimize space progress:" + data.progress);
  }
}
cloudSync.startOptimizeSpace(para, callback).then(() => {
    console.info("start optimize space");
}).catch((err: BusinessError) => {
    console.error("start optimize space failed with error message: " + err.message + ", error code: " + err.code);
});

cloudSync.stopOptimizeSpace17+

stopOptimizeSpace(): void

Synchronously stops optimizing cloud resource space. This method is used with startOptimizeSpace.

Required permissions: ohos.permission.CLOUDFILE_SYNC

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

Error codes

For details about the error codes, see Universal Error Codes and File Management Error Codes.

IDError Message
201Permission verification failed, usually the result returned by VerifyAccessToken.
202Permission verification failed, application which is not a system application uses system API.
13600001IPC error.
22400005Inner error.

Example

import { BusinessError } from '@kit.BasicServicesKit';
let para:cloudSync.OptimizeSpaceParam = {totalSize: 1073741824, agingDays: 30};
let callback = (data:cloudSync.OptimizeSpaceProgress) => {
  if (data.state == cloudSync.OptimizeState.FAILED) {
    console.info("optimize space failed");
  } else if (data.state == cloudSync.OptimizeState.RUNNING) {
    console.info("optimize space progress:" + data.progress);
  }
}
cloudSync.startOptimizeSpace(para, callback);
cloudSync.stopOptimizeSpace();   // Stop space optimization.

OptimizeState17+

Enumerates the space optimization states.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

NameValueDescription
RUNNING0The space is being optimized.
COMPLETED1The space optimization is complete.
FAILED2Space optimization failed.
STOPPED3Space optimization stopped.

OptimizeSpaceProgress17+

Represents the space optimization states and optimization progress.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

NameTypeMandatoryDescription
stateOptimizeStateYesEnumerates the space optimization states.
progressnumberYesOptimization progress percentage. The value ranges from 1 to 100.

OptimizeSpaceParam17+

Sets the total optimization space and aging days.

System capability: SystemCapability.FileManagement.DistributedFileService.CloudSync.Core

System API: This is a system API.

NameTypeMandatoryDescription
totalSizenumberYesTotal size of the optimization space. You can obtain the total size of all files to be aged through the media library API. The size is transferred by the application and is in bytes.
agingDaysnumberYesAging days. The system will optimize the local images/videos that have not been accessed and have been synced to the cloud before the aging days.

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Core File Kit

harmony 鸿蒙Environment

harmony 鸿蒙FileIO

harmony 鸿蒙FileShare_PolicyErrorResult

harmony 鸿蒙FileShare_PolicyInfo

harmony 鸿蒙error_code.h

harmony 鸿蒙File Management Error Codes

harmony 鸿蒙FileShare

harmony 鸿蒙FileUri

harmony 鸿蒙@ohos.application.BackupExtensionAbility (Backup and Restore Extension Capability) (System API)

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/7DqtOH