var co = require('co');
var OSS = require('ali-oss');
class OSSClient{
  constructor(){
    this.client=new OSS({
      endpoint: 'https://oss-cn-beijing.aliyuncs.com',
      accessKeyId: 'LTAI4GC5tSKvqsH2hMqj6pvd',
      accessKeySecret: '3KV9nIwW8qkTGlrPmAe3HnR3fzM6r5'
    });
    this.client.useBucket('gsb-zc');
  }
  async downfile(key,destfile){
    var me=this;
    var destfile=destfile?destfile:key
    var result=await co(function* () {
      var result = yield me.client.get(key, '/tmp/'+destfile);
      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);
// });

