oss-client

aliyun oss client for node.js

npm install oss-client
1 downloads in the last day
16 downloads in the last week
85 downloads in the last month

NPM

a node.js module to connect aliyun oss

npm install oss-client

aliyun oss document

how to use

var OSS = require('oss-client');
var option = {
  accessKeyId: 'your access key id',
  accessKeySecret: 'your access key secret'
};

/*
 * host - default: oss.aliyuncs.com
 * port - default: 8080
 * timeout - default: 30000000
 * agent - default: agent.maxSockets = 20
 */

var oss = new OSS.OssClient(option);

参数说明:

{
  bucket: 'bucket name',
  object: 'object name',
  acl: 'bucket 访问规则'
}

object

创建object(by: file path)

/*
 * srcFile: 上传的文件路径
 * userMetas: 可选,object类型,用户自定义header,如: x-oss-meta-location
 */
putObject({
  bucket: bucket,
  object: object,
  srcFile: srcFile,
  userMetas: userMetas //optional
}, function (err) {});

创建object(by: buffer)

/*
 * userMetas: 可选,object类型,用户自定义header,如: x-oss-meta-location
 * contentType: 可选,但推荐添加(buffer无法自动获取contentType)
 */

oss.putObject({
  bucket: bucket,
  object: object,
  srcFile: new Buffer("hello,wolrd", "utf8"),
  contentType: 'image/jpeg'
}, function (error, result) {});

创建object(by: stream)

/*
 * userMetas: 可选,object类型,用户自定义header,如: x-oss-meta-location
 */
var input = fs.createReadStream(__filename);
oss.putObject({
  bucket: bucket,
  object: object,
  srcFile: input,
  contentLength: fs.statSync(__filename).size
}, function (error, result) {});

复制object

copyObject({
  bucket: bucket,
  object: object,
  srcObject: srcObject
}, function (err) {});

删除object

deleteObject({
  bucket: bucket,
  object: object
}, function (err) {});

获取object

/*
 * dstFile: 保存object的文件路径
 * userHeaders: 可选,object类型,用户自定义header,如 If-Unmodified-Since
 */
getObject({
  bucket: bucket,
  object: object,
  dstFile: dstFile,
  userHeaders: userHeaders
}, function (err) {});

获取object头信息

headObject({
  bucket: bucket,
  object: object
}, function (err, result) {});

获取object列表

/*
 * prefix: 可选,object 前缀
 * marker: 可选,列表起始object
 * delimiter: 可选,object分组字符,若为 '/' 则不列出路径深度大于等于二层的object
 * maxKeys: 可选, 列出的object最大个数
 */
listObject({
  bucket: bucket,
  prefix: prefix,
  marker: marker,
  delimiter: delimiter,
  maxKeys: maxKeys
}, function (err, result) {});

bucket

列出所有bucket

listBucket(function (err) {});

创建bucket

createBucket({
  bucket: bucket,
  acl: acl
}, function (err) {});

删除bucket

deleteBucket(bucket, function (err) {});

获取bucket访问规则

getBucketAcl(bucket, function (err, result) {});

设置bucket访问规则

setBucketAcl({
  bucket: bucket,
  acl: acl
}, function (err) {});

License

MIT

npm loves you