vue +(阿里雲) oss-ali 上傳文件

安裝 ali-oss

npm install ali-oss

實例


// 封裝方法 oss.js
import OSS from 'ali-oss';
export default (conf) => {
    return new OSS(conf);

}


// 引入方法
import ossClient from '對應文件地址/oss.js';

// 調用

   let uploadConf =   {
        region: null,   // oss-cn-beijing
        accessKeyId: null,
        accessKeySecret: null,
        bucket: null,
        stsToken: null
        
    }
   // file // 上傳文件屬性
uploadHttp (file) {
    ossClient(uploadConf)
    .put(`'上傳目錄'/${new Date().getTime()+'.'+'圖片格式'}`, file, {'ContentType': 'image/jpeg' })
        .then(({res, url, name}) => {
            if (res && res.status == 200) {
                
                console.log(`阿里雲OSS上傳圖片成功回調`, res, url, name);
                console.log(`阿里雲OSS上傳圖片成功回調`, 返回實體, 返回地址, 返回圖片的名稱);
                
                // 獲取oss圖片 臨時授權url
                const url = ossClient(uploadConf).signatureUrl(name, {
                    process: 'image/resize,w_200'
                })
                
            }
        }).catch((err) => {
            console.log(`阿里雲OSS上傳圖片失敗回調`, err);
            
        });
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章