huaweicloud / huaweicloud-sdk-nodejs-obs

Apache License 2.0
27 stars 10 forks source link

还维护? #19

Open lishaoxin123 opened 2 years ago

lishaoxin123 commented 2 years ago

https://support.huaweicloud.com/sdk-nodejs-devg-obs/obs_29_0404.html 只有文件的buffer流,怎么使用ObsClient 进行文件上传呢?总不能走一遍写入然后读取成功在删除吧。

noaccident commented 2 years ago

需要buffer转stream,然后再调用sdk的上传接口。可参考以下代码:

let Duplex = require('stream').Duplex;

function bufferToStream(buffer) {  
  let stream = new Duplex();
  stream.push(buffer);
  stream.push(null);
  return stream;
}

const buffers = []; // 文件buffer

const stream = bufferToStream(buffers);

obsClient.putObject({
    Bucket : bucketName,
    Key: 'objectname',
    Body: stream
}).then(res => console.log(res));