vue 使用cos-js-sdk-v5

npm install cos-js-sdk-v5
import COS from 'cos-js-sdk-v5'

//定義一個cos 對象
const cos = new COS ({
        getAuthorization: function (options, callback) {
          callback ({
                //騰訊云賬號上傳信息
           TmpSecretId: tmpSecretId, 
          TmpSecretKey: tmpSecretKey,
          XCosSecurityToken: sessionToken,
          ExpiredTime: expiredTime
          });
        },
      });
//定義一個function
uploadFile (cos, file) {
      const _this=this
      cos.putObject (
        {
          Bucket: "",  // 存儲桶名稱
          Region: 'ap-chengdu', // 地區(qū)
          Key:`/uploads/zizhi/${new Date().getTime()}${file.name}` , // 圖片名稱
          Body: file,
          onHashProgress: function (progressData) {
            console.log ('校驗中', JSON.stringify (progressData));
          },
          onProgress: function (progressData) {
            console.log ('上傳中', JSON.stringify (progressData));
          },
        },
        function (err, data) {
          if (err) {
           this.error(file)
            return;
          }
          console.log(data)
          _this.success({url:`http://${data.Location}`})
        }
      );
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容