Created
January 26, 2019 08:51
-
-
Save giscafer/af3fb444ae91bc73608794c53053d732 to your computer and use it in GitHub Desktop.
微信小程序百度直传
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 百度BOS直传 | |
| * @param {*} filePath | |
| */ | |
| export default function uploadPicture(filePath) { | |
| let filename = uuid() + filePath.substring(filePath.lastIndexOf("/") + 1); | |
| const tokenObj = Taro.getStorageSync('bosUploadToken'); | |
| let policy = `{"conditions":[{"bucket":"yztfile"},{"key":"${commonPath}${filename}"}]}`; | |
| let base64 = Base64.encode(policy) | |
| let signature = HmacSHA256(base64, secretAccessKey).toString(EncHex); | |
| // console.log(filename) | |
| return new Promise((resolve, reject) => { | |
| Taro.uploadFile({ | |
| url: UPLOAD_SERVER_URL, | |
| filePath, | |
| name: 'file', | |
| header: { | |
| 'x-bce-date': new Date().toISOString(), | |
| Authorization: tokenObj.sessionToken | |
| }, | |
| formData: { | |
| accessKey: accessKeyId, | |
| policy: base64, | |
| signature, | |
| key: commonPath + filename, // 注意:这个key必须与policy中的key保持一致,否则会报错 | |
| }, | |
| }).then(res => { | |
| console.log(res); | |
| if (res.statusCode === 200) { | |
| Taro.setClipboardData({ | |
| data: `${UPLOAD_SERVER_URL}${commonPath}${filename}` | |
| }) | |
| return resolve(`${UPLOAD_SERVER_URL}${commonPath}${filename}`); | |
| } | |
| showToast('上传失败'); | |
| return reject(res); | |
| }).catch(err => { | |
| console.log(err); | |
| let error = err || {}; | |
| showToast('上传失败' + JSON.stringify(err)) | |
| return reject(err) | |
| }); | |
| }) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment