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){
    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="netsharp_QSzjD4HdKdTmRR6b5486pEA3AbsW8Pr8.jpg"
// oss.upfile(key,"/usr/devws/OMC/igirl-api/r3.jpg").then(function(result){
//   console.log(result);
// });
// oss.downfile(key).then(function(result){
//   console.log(result);
// });
