云存储提供高可用、高稳定、强安全的云端存储服务,支持任意数量和形式的非结构化数据存储,如视频和图片,并在控制台进行可视化管理。云存储包含以下功能:
接下来,我们看看云文件管理提供了哪些 api、及如何在控制台中管理云文件:
在小程序端可调用 wx.cloud.uploadfile 方法进行上传:
wx.cloud.uploadfile({
cloudpath: 'example.png', // 上传至云端的路径
filepath: '', // 小程序临时文件路径
success: res => {
// 返回文件 id
console.log(res.fileid)
},
fail: console.error
})
上传成功后会获得文件唯一标识符,即文件 id,后续操作都基于文件 id 而不是 url。
可以根据文件 id 下载文件,用户仅可下载其有访问权限的文件:
wx.cloud.downloadfile({
fileid: '', // 文件 id
success: res => {
// 返回临时文件路径
console.log(res.tempfilepath)
},
fail: console.error
})
可以通过 wx.cloud.deletefile 删除文件:
wx.cloud.deletefile({
filelist: ['a7xzcb'],
success: res => {
// handle success
console.log(res.filelist)
},
fail: console.error
})
更详细的 api 可参考小程序端及后端存储 api 文件。
支持在 image、audio 等组件中传入云文件 id,具体支持列表见文档
可以根据文件 id 换取临时文件网络链接,文件链接有有效期为两个小时:
wx.cloud.gettempfileurl({
filelist: ['cloud://xxx.png'],
success: res => {
// filelist 是一个有如下结构的对象数组
// [{
// fileid: 'cloud://xxx.png', // 文件 id
// tempfileurl: '', // 临时文件网络链接
// maxage: 120 * 60 * 1000, // 有效期
// }]
console.log(res.filelist)
},
fail: console.error
})
可以在此参考详细的小程序端存储 api 文档和服务端 api 文档
在控制台中,选择存储管理标签页,可以在此看到云存储空间中所有的文件,还可以查看文件的详细信息、控制存储空间的读写权限。