var co = require('co');
var OSS = require('ali-oss');
class OSSClient{
  constructor(){
    this.client=new OSS({
      endpoint: 'https://oss-cn-beijing.aliyuncs.com',
      accessKeyId: 'LTAIyAUK8AD04P5S',
      accessKeySecret: 'DHmRtFlw2Zr3KaRwUFeiu7FWATnmla'
    });
    this.client.useBucket('gsb-zc');
  }
  async downfile(key){
    var me=this;
    var result=await co(function* () {
      var result = yield me.client.get(key, '/tmp/'+key);
      return result;
    });
    return result;
  }
  async upfile(key,filepath){
    var me=this;
    var result=await co(function* () {
      var result = yield me.client.put(key, filepath);
      return result;
    })
    return result;
  }
  async putBuffer (key,buf) {
    try {
      var result = await this.client.put(key, buf);
      console.log(result);
      return result
    } catch (e) {
      console.log(e);
      return null
    }
  }
}
module.exports=OSSClient;
// var oss=new OSSClient();
// var key="GT.pdf"
// oss.upfile(key,"./GT.pdf").then(function(result){
//   console.log(result);
// });
// key="ST.pdf"
// oss.upfile(key,"./ST.pdf").then(function(result){
//   console.log(result);
// });
// key="ITQ.pdf"
// oss.upfile(key,"./ITQ.pdf").then(function(result){
//   console.log(result);
// });
// key="ITY.pdf"
// oss.upfile(key,"./ITY.pdf").then(function(result){
//   console.log(result);
// });

