这个是小程序端代码:

  1. async taptapNewPlanBtnForTos() {
  2. const res = await wx.chooseMessageFile({ count: 1 });
  3. const path = res.tempFiles[0].path;
  4. // readFile 不支持 promise
  5. wx.getFileSystemManager().readFile({ filePath: path, success: ({ data }) => {
  6. wx.request({
  7. url: preSignedUrl,
  8. method: 'PUT',
  9. data,
  10. });
  11. }});
  12. },

这个是服务端代码:

  1. func main() {
  2. bucket := "001-bucket"
  3. client, err := tos.NewClient(endpoint, tos.WithRegion(region),
  4. tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))
  5. check(err, "NewClient")
  6. preSignedUrl, err := client.PreSignedURL(http.MethodGet, bucket, "test.txt", time.Hour)
  7. fmt.Println(preSignedUrl) // 这个 preSignedUrl 给到小程序客户端,小程序客户端就可以直播上传文件到TOS了
  8. }

分类: web

标签: