const config = {
  AccessKeyId: 'IVW21DTGIIUBBAGXKK0Y',      //AK
  SecretKey: 'Y01nEQNcLOATMw7uJwrk3yOdQZ2fqLhSnXcOKVDE',        //SK
  EndPoint: 'https://horastar.obs.cn-east-3.myhuaweicloud.com',         //上传文件的路径
};
// const config ={
//   AccessKeyId: 'OMQVTLJWELHSZGCEQCZQ',      //AK
//   SecretKey: 'MIfn0HRfmYKyQLSP7kemUCoj3ks20fhOB0IuksEp',        //SK
//   EndPoint: 'https://goin.obs.cn-north-4.myhuaweicloud.com',         //上传文件的路径
// };
const getPolicyEncode = require('./getPolicy.js');
const getSignature = require('./GetSignature.js');

const OBSupload = function (filePath){


    const fileName = 'talent_upload/115';   //指定上传到OBS桶中的对象名    
// const fileName = '005';   
    const OBSPolicy = {                   //设定policy内容
      "expiration": "2089-12-31T12:00:00.000Z",
      "conditions": [
        {"bucket": "horastar"},        //Bucket name
		// {"bucket": "goin"}, 
        { 'key': fileName}
      ]
    }

    const policyEncoded = getPolicyEncode(OBSPolicy);                    //计算policy编码值
    const signature = getSignature(policyEncoded, config.SecretKey);     //计算signature

    uni.uploadFile({
      //url: config.EndPoint,
      url: config.EndPoint,
      filePath: filePath,
      name: 'file',
	 
      formData: {
        'AccessKeyID': config.AccessKeyId,
        'policy': policyEncoded,
        'signature': signature,
        'key':fileName,
      },

      success: function(res){
        console.log(res.statusCode);            //打印响应状态码
        if(res.statusCode=='204'){
          console.log('上传图片成功', res)

          wx.showToast({
            title: '上传成功',
            icon: '成功'
          });
        }
        else{
          console.log('上传图片失败', res)
          wx.showToast({
            title: '上传失败',
            icon: '失败'
          });
        }
      },
      fail: function(e){
        console.log(e);
      }
    })
	

  const obs_url = config.EndPoint + '/' + fileName;  //用你自己的 bucket 名替换星号
  console.log(obs_url)
  	  return obs_url
}

module.exports = OBSupload;