• 小程序开发直传腾讯云操作步骤


    1、拿到腾讯云SecretId和SecretKey

    https://console.cloud.tencent.com/cam/capi

    在这里插入图片描述

    2、通过SecretId和SecretKey请求tmpSecretId、tmpSecretKey和sessionToken字段

    此处以node.js代码示例
    官方demo:https://github.com/tencentyun/cos-js-sdk-v5/blob/master/server/sts.js
    需要修改下面几个地方,之后运行npm run sts-server,打开浏览器请求http://localhost:3000/sts 即可拿到tmpSecretId、tmpSecretKey等
    在这里插入图片描述
    在这里插入图片描述

    3、第三步就该生成签名

    如果需要前端生成签名,需要后端把获取到的tmpSecretId、tmpSecretKey传给前端
    前端将生成签名工具保存本地:签名工具地址
    在这里插入图片描述
    调用签名工具api生成签名,authData即是签名
    在这里插入图片描述

    ![在这里插入图片描述](https://img-blog.csdnimg.cn/44dbc40023ef4fc4ba4a9eeb74d1b77e.png
    这个时候就该前端去选择文件上传,示例

    uploadFile = (filePath: string): void => {
      var Key = filePath.substr(filePath.lastIndexOf("/") + 1); // 这里指定上传的文件名
      var signPathname = "/" + Bucket + "/"; // PostObject 接口 Key 是放在 Body 传输,所以请求路径和签名路径是 /
      const AuthData = CosAuth({
        SecretId: "",
        SecretKey: "",
        Method: "POST",
        Pathname: signPathname
      });
      // const prefix = "https://" + Bucket + ".cos." + Region + ".myqcloud.com/";
      const prefix = "https://cos." + Region + ".myqcloud.com/" + Bucket + "/"; // 上传地址固定
      console.log(AuthData, Key);
      const requestTask = Taro.uploadFile({
        url: prefix,
        name: "file",
        filePath: filePath,
        formData: {
          key: Key, // 文件名
          Signature: AuthData, // 签名
          success_action_status: 200,
          "x-cos-security-token":"第一步生成的sessionToken",
          "Content-Type": ""
        },
        success: function (res) {
          console.log("上传成功", res);
        }
      });
      requestTask.onProgressUpdate(function (res) {
        console.log("进度:", res);
      });
    }
    wx.chooseImage({
       count: 1, // 默认9
       sizeType: ["original"], // 可以指定是原图还是压缩图,这里默认用原图
       sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
       success: function (res: any) {
         uploadFile(res.tempFiles[0].path);
       }
     });
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    在这里插入图片描述
    参考文档:小程序直传实践
    错误码参考文档:常见403错误文档

  • 相关阅读:
    前沿重器[29] | ERNIE-Search:向交互式学习的表征式语义匹配代表作
    面渣逆袭:二十二图、八千字、二十问,彻底搞定MyBatis
    递归与lamdba与高阶函数
    iOS 展示网络GIF 图片
    反射型XSS靶场练习
    1.4_1 Axure RP 9 for mac 入门
    【网络篇】如何在服务器之间建立互信
    班级网站的设计与实现
    Jitpack发布Android库带文档和源码
    NR SRS(一)时频域位置
  • 原文地址:https://blog.csdn.net/weixin_44868881/article/details/126234508