May 20, 2021 WeChat Mini Program Development Document
Cloud storage provides highly available, stable, and secure cloud storage services that support any number and form of unstructured data storage, such as video and pictures, and visual management in the console. Cloud storage includes the following features:
Next, let's look at what APIs cloud file management provides and how to manage cloud files in the console:
The wx.cloud.uploadFile method can be called on the small terminal to upload:
wx.cloud.uploadFile({
cloudPath: 'example.png', // 上传至云端的路径
filePath: '', // 小程序临时文件路径
success: res => {
// 返回文件 ID
console.log(res.fileID)
},
fail: console.error
})
When the upload is successful, you get a unique identifier for the file, the file ID, and subsequent actions are based on the file ID rather than the URL.
Files can be downloaded based on the file ID, and users can only download files that they have access to:
wx.cloud.downloadFile({
fileID: '', // 文件 ID
success: res => {
// 返回临时文件路径
console.log(res.tempFilePath)
},
fail: console.error
})
You can delete the file by wx.cloud.deleteFile:
wx.cloud.deleteFile({
fileList: ['a7xzcb'],
success: res => {
// handle success
console.log(res.fileList)
},
fail: console.error
})
A more detailed API can refer to the smaller and back-end storage API files.
Support in components such as Image, Audio, etc., for specific support lists Documentation
You can exchange the temporary file network link according to the file ID, the file link is valid for two hours:
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
})
You can refer here to the detailed small terminal storage API documentation and the service side API documentation
In the console, select the Storage Management tab, where you can see all the files in the cloud storage space, and you can view the details of the files and control the read and write rights to the storage space.
The gadget component supports the incoming cloud file ID, and the list of support is as follows:
Component | Property |
---|---|
image | Src |
video | src、poster |
cover-image | Src |
Interface | Parameters |
---|---|
getBackgroundAudioManager | Src |
createInnerAudioContext | Src |
previewImage | urls、current |