Skip to main content
Version: 3.x

cloud

Instance object of a cloud development SDK.

Reference

Type​

typeof cloud

Parameters​

CallFunctionResult​

Common return for cloud functions

PropertyTypeDescription
result`stringRecord<string, any>`
errMsgstringCall result

IAPIParam​

Common parameters for cloud functions

PropertyTypeRequiredDescription
configIConfigNoConfiguration
success(res: T) => voidNoThe callback function for a successful API call
fail(err: CallbackResult) => voidNoThe callback function for a failed API call
complete`(val: CallbackResultT) => void`No

IInitConfig​

Initial configuration

PropertyTypeRequiredDescription
env`string{ database?: string; functions?: string; storage?: string; }`No
traceUserbooleanNoSpecify whether or not user access is logged to User Management before being visible in the console.

IConfig​

Configuration

PropertyTypeRequiredDescription
envstringNoThe environment ID used, which can be filled in to ignore the environment specified by init.
traceUserbooleanNoSpecify whether or not user access is logged to User Management before being visible in the console.

ICloudAPIParam​

Common parameters for the Cloud Functions API.

PropertyTypeRequiredDescription
configIConfigNoConfiguration

CallFunctionParam​

PropertyTypeRequiredDescription
namestringYesCloud function name
dataRecord<string, any>NoThe parameters passed to the cloud function are available in the cloud function via the event parameter.
slowbooleanNo
configIConfigNoConfiguration
complete`(res: CallFunctionResultCallbackResult) => void`No
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: CallFunctionResult) => voidNoThe callback function for a successful API call

UploadFileResult​

Results of uploaded files

PropertyTypeDescription
fileIDstringFile ID
statusCodenumberThe HTTP status code returned by the server.
errMsgstringCall result

UploadFileParam​

Parameters of uploaded files

PropertyTypeRequiredDescription
cloudPathstringYesCloud Storage Path
filePathstringYesThe path of the file resource to be uploaded.
headerRecord<string, any>No
configIConfigNoConfiguration
complete`(res: CallbackResultUploadFileResult) => void`No
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: UploadFileResult) => voidNoThe callback function for a successful API call

DownloadFileResult​

下载文件结果

PropertyTypeDescription
tempFilePathstringTemporary file path
statusCodenumberThe HTTP status code returned by the server.
errMsgstringCall result

DownloadFileParam​

Parameters for downloading files

PropertyTypeRequiredDescription
fileIDstringYesCloud File ID
cloudPathstringNo
configIConfigNoConfiguration
complete`(res: CallbackResultDownloadFileResult) => void`No
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: DownloadFileResult) => voidNoThe callback function for a successful API call

GetTempFileURLResult​

The result of the acquisition of temporary documents.

PropertyTypeDescription
fileListGetTempFileURLResultItem[]List of files
errMsgstringCall result

GetTempFileURLResultItem​

List of files

PropertyTypeDescription
fileIDstringCloud file ID
tempFileURLstringThe path of the temporary file.
maxAgenumber
statusnumberStatus Code
errMsgstringCall result

GetTempFileURLParam​

Parameters of get temporary file

PropertyTypeRequiredDescription
fileListstring[]Yes
configIConfigNoConfiguration
complete`(res: CallbackResultGetTempFileURLResult) => void`No
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: GetTempFileURLResult) => voidNoThe callback function for a successful API call

DeleteFileResult​

The result of deleting a file

PropertyTypeDescription
fileListDeleteFileResultItem[]List of files
errMsgstringCall result

DeleteFileResultItem​

Delete file list

PropertyTypeDescription
fileIDstringCloud file ID
statusnumberStatus Code
errMsgstringCall result

DeleteFileParam​

PropertyTypeRequiredDescription
fileListstring[]YesList of files
configIConfigNoConfiguration
complete`(res: CallbackResultDeleteFileResult) => void`No
fail(res: CallbackResult) => voidNoThe callback function for a failed API call
success(res: DeleteFileResult) => voidNoThe callback function for a successful API call

init​

The initialisation method init needs to be called once before calling the cloud development APIs (only once globally, only the first time takes effect if called multiple times)

Reference

(config?: IInitConfig) => void
PropertyType
configIInitConfig

Sample Code​

Taro.cloud.init({
env: 'test-x1dzi'
})

API Support​

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Mini-ProgramQQ Mini-ProgramH5React NativeQuick App
cloud.init✔️

callFunction​

Call Cloud funtion

Reference

{ (param: OQ<CallFunctionParam>): void; (param: Pick<CallFunctionParam, "name" | "data" | "slow" | "config">): Promise<CallFunctionResult>; }
PropertyType
paramOQ<CallFunctionParam>

Sample Code​

Assuming there is already a cloud function add, initiate a call to the cloud function add on the mini program side.

Taro.cloud.callFunction({
// Name of the cloud function to be called
name: 'add',
// The event parameter passed to the cloud function.
data: {
x: 1,
y: 2,
}
}).then(res => {
// output: res.result === 3
}).catch(err => {
// handle error
})

API Support​

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Mini-ProgramQQ Mini-ProgramH5React NativeQuick App
cloud.callFunction✔️

uploadFile​

Upload local resources to cloud storage, or overwrite if uploading to the same path.

Reference

{ (param: OQ<UploadFileParam>): any; (param: Pick<UploadFileParam, "config" | "cloudPath" | "filePath" | "header">): Promise<UploadFileResult>; }
PropertyType
paramOQ<UploadFileParam>

Sample Code​

Example 1​
Taro.cloud.uploadFile({
cloudPath: 'example.png',
filePath: '', // file path
success: res => {
// get resource ID
console.log(res.fileID)
},
fail: err => {
// handle error
}
})
Example 2​
Taro.cloud.uploadFile({
cloudPath: 'example.png',
filePath: '', // file path
}).then(res => {
// get resource ID
console.log(res.fileID)
}).catch(error => {
// handle error
})

API Support​

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Mini-ProgramQQ Mini-ProgramH5React NativeQuick App
cloud.uploadFile✔️

downloadFile​

Download files from cloud storage.

Reference

{ (param: OQ<DownloadFileParam>): any; (param: Pick<DownloadFileParam, "config" | "cloudPath" | "fileID">): Promise<DownloadFileResult>; }
PropertyType
paramOQ<DownloadFileParam>

Sample Code​

Example 1​
Taro.cloud.downloadFile({
fileID: 'a7xzcb',
success: res => {
// get temp file path
console.log(res.tempFilePath)
},
fail: err => {
// handle error
}
})
Example 2​
Taro.cloud.downloadFile({
fileID: 'a7xzcb'
}).then(res => {
// get temp file path
console.log(res.tempFilePath)
}).catch(error => {
// handle error
})

API Support​

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Mini-ProgramQQ Mini-ProgramH5React NativeQuick App
cloud.downloadFile✔️

getTempFileURL​

Exchange your cloud file ID for a real link. Links obtained from public and readable files do not expire, while links obtained from private files are valid for ten minutes. Maximum of 50 at a time.

Reference

{ (param: OQ<GetTempFileURLParam>): void; (param: Pick<GetTempFileURLParam, "config" | "fileList">): Promise<GetTempFileURLResult>; }
PropertyType
paramOQ<GetTempFileURLParam>

Sample Code​

Example 1​
Taro.cloud.getTempFileURL({
fileList: [{
fileID: 'a7xzcb',
maxAge: 60 * 60, // one hour
}]
}).then(res => {
// get temp file URL
console.log(res.fileList)
}).catch(error => {
// handle error
})
Example 2​
Taro.cloud.getTempFileURL({
fileList: ['cloud://xxx', 'cloud://yyy'],
success: res => {
// get temp file URL
console.log(res.fileList)
},
fail: err => {
// handle error
}
})

API Support​

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Mini-ProgramQQ Mini-ProgramH5React NativeQuick App
cloud.getTempFileURL✔️

deleteFile​

Delete files from cloud storage, up to 50 at a time.

Reference

{ (param: OQ<DeleteFileParam>): void; (param: Pick<DeleteFileParam, "config" | "fileList">): Promise<DeleteFileResult>; }
PropertyType
paramOQ<DeleteFileParam>

Sample Code​

Example 1​
.cloud.deleteFile({
fileList: ['a7xzcb']
}).then(res => {
// handle success
console.log(res.fileList)
}).catch(error => {
// handle error
})
Example 2​
Taro.cloud.deleteFile({
fileList: ['a7xzcb'],
success: res => {
// handle success
console.log(res.fileList)
},
fail: err => {
// handle error
},
complete: res => {
// ...
}
})

API Support​

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Mini-ProgramQQ Mini-ProgramH5React NativeQuick App
cloud.deleteFile✔️

database​

Get the database instance.

Reference

(config?: IConfig) => Database
PropertyType
configIConfig

Sample Code​

Example 1​

The following call obtains a reference to the database of the default environment:

const db = Taro.cloud.database()
Example 2​

Assuming an environment named test-123 is used as a test environment, the test environment database can be obtained as follows.

const testDB = Taro.cloud.database({
env: 'test-123'
})

API Support​

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Mini-ProgramQQ Mini-ProgramH5React NativeQuick App
cloud.database✔️