云函數(shù)獲取云存儲的數(shù)據(jù)以及客戶端獲取云存儲數(shù)據(jù)方式

一共有兩種方式,一是經(jīng)過云函數(shù)來返回數(shù)據(jù),另外一種是直接在本地獲取數(shù)據(jù),請看以下代碼,
本地客戶端方式:

//  隔一個小時去獲取云id的鏈接并且賦值給audiosrc,這樣頁面就能實時更新鏈接,確保鏈接的有效性
  onLoad:function(options){
    wx.cloud.getTempFileURL({
      fileList: [{
        fileID: 'cloud://eason-63i40.6561-eason-63i40-1303207973/jntm.mp3',//云id
        maxAge: 60 * 60, // one hour
      }]
    }).then(res => {
      // console.log(res.fileList)
      // console.log(res.fileList[0].tempFileURL)
      this.setData({
     //audiosrc為頁面audio標簽綁定的src的值,在onload方法中頁面加載完就獲取,確保鏈接有效
      audiosrc:res.fileList[0].tempFileURL
      })
    })
  }

云函數(shù)方式:
云函數(shù):

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async (event, context) => {
  const fileList = ['   cloud://eason-63i40.6561-eason-63i40-1303207973/jntm.mp3']//云id
  const result = await cloud.getTempFileURL({
    fileList: fileList,
  })
  return result.fileList
}

客戶端請求方式:

  onLoad:function(options){
    // 云函數(shù)方式的本地寫法
    wx.cloud.callFunction({
// 云函數(shù)名叫chaxun
      name: "chaxun",
    }).then(res => {
//  這個時候返回的是result了   凡是在云函數(shù)return的 到客戶端都等于result
      console.log(res.result[0].tempFileURL);
      this.setData({
          audiosrc:res.result[0].tempFileURL
          })
    })

  }
}) 
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容