title: cloud

sidebar_label: cloud

云开发 SDK 实例

参考文档

类型

  1. typeof cloud

参数

CallFunctionResult

云函数通用返回

参数 类型 说明
result `string Record` 云函数返回的结果
errMsg string 调用结果

IAPIParam

云函数通用参数

参数 类型 必填 说明
config IConfig 配置
success (res: T) => void 接口调用成功的回调函数
fail (err: CallbackResult) => void 接口调用失败的回调函数
complete `(val: CallbackResult T) => void` 接口调用结束的回调函数(调用成功、失败都会执行)

IInitConfig

初始化配置

参数 类型 必填 说明
env `string { database?: string; functions?: string; storage?: string; }` 默认环境配置,传入字符串形式的环境 ID 可以指定所有服务的默认环境,传入对象可以分别指定各个服务的默认环境
traceUser boolean 是否在将用户访问记录到用户管理中,在控制台中可见

IConfig

配置

参数 类型 必填 说明
env string 使用的环境 ID,填写后忽略 init 指定的环境
traceUser boolean 是否在将用户访问记录到用户管理中,在控制台中可见

ICloudAPIParam

云函数 API 通用参数

参数 类型 必填 说明
config IConfig 配置

CallFunctionParam

调用云函数参数

参数 类型 必填 说明
name string 云函数名
data Record<string, any> 传递给云函数的参数,在云函数中可通过 event 参数获取
slow boolean
config IConfig 配置
complete `(res: CallFunctionResult CallbackResult) => void` 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: CallbackResult) => void 接口调用失败的回调函数
success (res: CallFunctionResult) => void 接口调用成功的回调函数

UploadFileResult

上传文件结果

参数 类型 说明
fileID string 文件 ID
statusCode number 服务器返回的 HTTP 状态码
errMsg string 调用结果

UploadFileParam

上传文件参数

参数 类型 必填 说明
cloudPath string 云存储路径,命名限制见文件名命名限制
filePath string 要上传文件资源的路径
header Record<string, any>
config IConfig 配置
complete `(res: CallbackResult UploadFileResult) => void` 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: CallbackResult) => void 接口调用失败的回调函数
success (res: UploadFileResult) => void 接口调用成功的回调函数

DownloadFileResult

下载文件结果

参数 类型 说明
tempFilePath string 临时文件路径
statusCode number 服务器返回的 HTTP 状态码
errMsg string 调用结果

DownloadFileParam

下载文件参数

参数 类型 必填 说明
fileID string 云文件 ID
cloudPath string
config IConfig 配置
complete `(res: CallbackResult DownloadFileResult) => void` 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: CallbackResult) => void 接口调用失败的回调函数
success (res: DownloadFileResult) => void 接口调用成功的回调函数

GetTempFileURLResult

获取临时文件结果

参数 类型 说明
fileList GetTempFileURLResultItem[] 文件列表
errMsg string 调用结果

GetTempFileURLResultItem

临时文件列表

参数 类型 说明
fileID string 云文件 ID
tempFileURL string 临时文件路径
maxAge number
status number 状态码
errMsg string 调用结果

GetTempFileURLParam

获取临时文件参数

参数 类型 必填 说明
fileList string[]
config IConfig 配置
complete `(res: CallbackResult GetTempFileURLResult) => void` 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: CallbackResult) => void 接口调用失败的回调函数
success (res: GetTempFileURLResult) => void 接口调用成功的回调函数

DeleteFileResult

删除文件结果

参数 类型 说明
fileList DeleteFileResultItem[] 文件列表
errMsg string 调用结果

DeleteFileResultItem

删除文件列表

参数 类型 说明
fileID string 云文件 ID
status number 状态码
errMsg string 调用结果

DeleteFileParam

删除文件参数

参数 类型 必填 说明
fileList string[] 文件列表
config IConfig 配置
complete `(res: CallbackResult DeleteFileResult) => void` 接口调用结束的回调函数(调用成功、失败都会执行)
fail (res: CallbackResult) => void 接口调用失败的回调函数
success (res: DeleteFileResult) => void 接口调用成功的回调函数

init

在调用云开发各 API 前,需先调用初始化方法 init 一次(全局只需一次,多次调用时只有第一次生效)

参考文档

  1. (config?: IInitConfig) => void
参数 类型
config IInitConfig

示例代码

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

API 支持度

API 微信小程序 百度小程序 支付宝小程序 字节跳动小程序 QQ 小程序 H5 React Native 快应用
cloud.init ✔️

callFunction

调用云函数

参考文档

  1. { (param: OQ<CallFunctionParam>): void; (param: Pick<CallFunctionParam, "name" | "data" | "slow" | "config">): Promise<CallFunctionResult>; }
参数 类型
param OQ<CallFunctionParam>

示例代码

假设已有一个云函数 add,在小程序端发起对云函数 add 的调用:

  1. Taro.cloud.callFunction({
  2. // 要调用的云函数名称
  3. name: 'add',
  4. // 传递给云函数的event参数
  5. data: {
  6. x: 1,
  7. y: 2,
  8. }
  9. }).then(res => {
  10. // output: res.result === 3
  11. }).catch(err => {
  12. // handle error
  13. })

API 支持度

API 微信小程序 百度小程序 支付宝小程序 字节跳动小程序 QQ 小程序 H5 React Native 快应用
cloud.callFunction ✔️

uploadFile

将本地资源上传至云存储空间,如果上传至同一路径则是覆盖写

参考文档

  1. { (param: OQ<UploadFileParam>): any; (param: Pick<UploadFileParam, "config" | "cloudPath" | "filePath" | "header">): Promise<UploadFileResult>; }
参数 类型
param OQ<UploadFileParam>

示例代码

示例 1
  1. Taro.cloud.uploadFile({
  2. cloudPath: 'example.png',
  3. filePath: '', // 文件路径
  4. success: res => {
  5. // get resource ID
  6. console.log(res.fileID)
  7. },
  8. fail: err => {
  9. // handle error
  10. }
  11. })
示例 2
  1. Taro.cloud.uploadFile({
  2. cloudPath: 'example.png',
  3. filePath: '', // 文件路径
  4. }).then(res => {
  5. // get resource ID
  6. console.log(res.fileID)
  7. }).catch(error => {
  8. // handle error
  9. })

API 支持度

API 微信小程序 百度小程序 支付宝小程序 字节跳动小程序 QQ 小程序 H5 React Native 快应用
cloud.uploadFile ✔️

downloadFile

从云存储空间下载文件

参考文档

  1. { (param: OQ<DownloadFileParam>): any; (param: Pick<DownloadFileParam, "config" | "cloudPath" | "fileID">): Promise<DownloadFileResult>; }
参数 类型
param OQ<DownloadFileParam>

示例代码

示例 1
  1. Taro.cloud.downloadFile({
  2. fileID: 'a7xzcb',
  3. success: res => {
  4. // get temp file path
  5. console.log(res.tempFilePath)
  6. },
  7. fail: err => {
  8. // handle error
  9. }
  10. })
示例 2
  1. Taro.cloud.downloadFile({
  2. fileID: 'a7xzcb'
  3. }).then(res => {
  4. // get temp file path
  5. console.log(res.tempFilePath)
  6. }).catch(error => {
  7. // handle error
  8. })

API 支持度

API 微信小程序 百度小程序 支付宝小程序 字节跳动小程序 QQ 小程序 H5 React Native 快应用
cloud.downloadFile ✔️

getTempFileURL

用云文件 ID 换取真实链接,公有读的文件获取的链接不会过期,私有的文件获取的链接十分钟有效期。一次最多取 50 个。

参考文档

  1. { (param: OQ<GetTempFileURLParam>): void; (param: Pick<GetTempFileURLParam, "config" | "fileList">): Promise<GetTempFileURLResult>; }
参数 类型
param OQ<GetTempFileURLParam>

示例代码

示例 1
  1. Taro.cloud.getTempFileURL({
  2. fileList: [{
  3. fileID: 'a7xzcb',
  4. maxAge: 60 * 60, // one hour
  5. }]
  6. }).then(res => {
  7. // get temp file URL
  8. console.log(res.fileList)
  9. }).catch(error => {
  10. // handle error
  11. })
示例 2
  1. Taro.cloud.getTempFileURL({
  2. fileList: ['cloud://xxx', 'cloud://yyy'],
  3. success: res => {
  4. // get temp file URL
  5. console.log(res.fileList)
  6. },
  7. fail: err => {
  8. // handle error
  9. }
  10. })

API 支持度

API 微信小程序 百度小程序 支付宝小程序 字节跳动小程序 QQ 小程序 H5 React Native 快应用
cloud.getTempFileURL ✔️

deleteFile

从云存储空间删除文件,一次最多 50 个

参考文档

  1. { (param: OQ<DeleteFileParam>): void; (param: Pick<DeleteFileParam, "config" | "fileList">): Promise<DeleteFileResult>; }
参数 类型
param OQ<DeleteFileParam>

示例代码

示例 1
  1. .cloud.deleteFile({
  2. fileList: ['a7xzcb']
  3. }).then(res => {
  4. // handle success
  5. console.log(res.fileList)
  6. }).catch(error => {
  7. // handle error
  8. })
示例 2
  1. Taro.cloud.deleteFile({
  2. fileList: ['a7xzcb'],
  3. success: res => {
  4. // handle success
  5. console.log(res.fileList)
  6. },
  7. fail: err => {
  8. // handle error
  9. },
  10. complete: res => {
  11. // ...
  12. }
  13. })

API 支持度

API 微信小程序 百度小程序 支付宝小程序 字节跳动小程序 QQ 小程序 H5 React Native 快应用
cloud.deleteFile ✔️

database

获取数据库实例

参考文档

  1. (config?: IConfig) => Database
参数 类型
config IConfig

示例代码

示例 1

以下调用获取默认环境的数据库的引用:

  1. const db = Taro.cloud.database()
示例 2

假设有一个环境名为 test-123,用做测试环境,那么可以如下获取测试环境数据库:

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

API 支持度

API 微信小程序 百度小程序 支付宝小程序 字节跳动小程序 QQ 小程序 H5 React Native 快应用
cloud.database ✔️