Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

qn

node-modules709MIT1.3.0

Another qiniu API client for Node.js.

qn, qiniu, qbox, node-qiniu

readme

qn

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Another qiniu API client for Node.js.

Install

$ npm install qn --save

Usage

Upload

var qn = require('qn');

var client = qn.create({
  accessKey: 'your access key',
  secretKey: 'your secret key',
  bucket: 'your bucket name',
  origin: 'http://{bucket}.u.qiniudn.com',
  // timeout: 3600000, // default rpc timeout: one hour, optional
  // if your app outside of China, please set `uploadURL` to `http://up.qiniug.com/`
  // uploadURL: 'http://up.qiniu.com/',
});

// upload a file with custom key
client.uploadFile(filepath, {key: 'qn/lib/client.js'}, function (err, result) {
  console.log(result);
  // {
  //   hash: 'FhGbwBlFASLrZp2d16Am2bP5A9Ut',
  //   key: 'qn/lib/client.js',
  //   url: 'http://qtestbucket.qiniudn.com/qn/lib/client.js'
  //   "x:ctime": "1378150371",
  //   "x:filename": "client.js",
  //   "x:mtime": "1378150359",
  //   "x:size": "21944",
  // }
});

// upload a stream
client.upload(fs.createReadStream(filepath), function (err, result) {
  console.log(result);
  // {
  //   hash: 'FvnDEnGu6pjzxxxc5d6IlNMrbDnH',
  //   key: 'FvnDEnGu6pjzxxxc5d6IlNMrbDnH',
  //   url: 'http://qtestbucket.qiniudn.com/FvnDEnGu6pjzxxxc5d6IlNMrbDnH',
  //   "x:filename": "foo.txt",
  // }
});

// you also can upload a string or Buffer directly
client.upload('哈哈', {key: 'haha.txt'}, function (err, result) {
  console.log(result);
  // hash: 'FptOdeKmWhcYHUXa5YmNZxJC934B',
  // key: 'haha.txt',
  // url: 'http://qtestbucket.qiniudn.com/haha.txt',
});

// xVariables
client.upload(filepath, { 'x:foo': 'bar' }, function (err, result) {
  console.log(result);
  // hash: 'FptOdeKmWhcYHUXa5YmNZxJC934B',
  // key: 'foobar.txt',
  // url: 'http://qtestbucket.qiniudn.com/foobar.txt',
  // x:foo: 'bar'
});

uploadToken

var token = client.uploadToken();

or with options

  • scope
  • deadline
var token = client.uploadToken({
  deadline: utility.timestamp() + 10
});

Download

// download to Buffer
client.download('foo.txt', function (err, content, res) {
  // content is a Buffer instance.
  console.log('content size: %d', content.length);
});

// save as url
var url = client.saveAsURL('qn/test/dl/foo.txt', '哈哈foo.txt');
// http://qtestbucket.qiniudn.com/qn/test/dl/foo.txt?download/%E5%93%88%E5%93%88foo.txt

RS Operations

// stat
client.stat('foo.txt', function (err, stat) {
  console.log(stat);
  // fsize: 8,
  // hash: 'FvnDEnGu6pjzxxxc5d6IlNMrbDnH',
  // mimeType: 'text/plain',
  // putTime: 13783134309588504
});

// move
client.move('foo.txt', 'qn/bar.txt', function (err) {

});

// copy
client.copy('foo.txt', 'qn/bar.txt', function (err) {

});

// delete
client.delete('foo.txt', function (err) {

});

// list
client.list('/', function (err, result) {
  console.log(result);
  // marker: 'eyJjIjowLCJrIjoicW4vYmlnLnR4dCJ9'
  // items: [
  //   {
  //     fsize: 21944,
  //     putTime: 13783144546186030,
  //     key: 'qn/logo.png',
  //     hash: 'FvzqAF1oWlYgQ9t62k_xn_mzZ1Ki',
  //     mimeType: 'image/png'
  //   }, ...
  // ]
});

Image operations

// imageInfo
client.imageInfo('qn/logo.png', function (err, info) {
  console.log(info);
  // { format: 'png', width: 190, height: 150, colorModel: 'nrgba' }
});

// exif
client.exif('qn/logo.png', function (err, exif) {

});

// imageView
var url = client.imageView('qn/logo.png', {mode: 1, width: 100, height: 100, q: 50, format: 'png'});
// http://qtestbucket.qiniudn.com/qn/logo.png?imageView/1/w/100/h/100/q/50/format/png

// imageMogr
var url = client.imageMogr('qn/fixtures/gogopher.jpg', {
  thumbnail: '!50p',
  gravity: 'NorthWest',
  quality: 50,
  rotate: -50,
  format: 'gif'
});
// http://qtestbucket.qiniudn.com/qn/fixtures/gogopher.jpg?imageMogr/v2/auto-orient/thumbnail/!50p/gravity/NorthWest/quality/50/rotate/-50/format/gif

// watermark
var url = client.watermark('qn/logo.png', {
  mode: 2,
  text: 'Node.js 哈哈',
  font: '宋体',
  fontsize: 500,
  fill: 'red',
  dissolve: 100,
  gravity: 'SouthEast',
  dx: 100,
  dy: 90
});
// http://qtestbucket.qiniudn.com/qn/fixtures/gogopher.jpg?watermark/2/text/Tm9kZS5qcyDlk4jlk4g=/font/5a6L5L2T/fontsize/500/fill/cmVk/dissolve/100/gravity/SouthEast/dx/100/dy/90

Document Operations

// markdown to html
var url = client.md2html('qn/test/fixtures/readme.md', {
  css: 'http://qtestbucket.qiniudn.com/qn/test/fixtures/github.css'
});
// http://qtestbucket.qiniudn.com/qn/test/fixtures/readme.md?md2html/0/css/aHR0cDovL3F0ZXN0YnVja2V0LnFpbml1ZG4uY29tL3FuL3Rlc3QvZml4dHVyZXMvZ2l0aHViLmNzcw==

TODO

  • <input checked="" disabled="" type="checkbox"> RS Operations
  • <input disabled="" type="checkbox"> HTTP Keep-alive
  • <input checked="" disabled="" type="checkbox"> Image Operations
  • <input disabled="" type="checkbox"> Media Operations
  • <input checked="" disabled="" type="checkbox"> Doc Operations
  • <input disabled="" type="checkbox"> Pipeline Operations
  • <input checked="" disabled="" type="checkbox"> QR code Operations

License

(The MIT License)

Copyright (c) 2013 - 2014 fengmk2 <fengmk2@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

changelog

1.3.0 / 2016-06-26

  • deps: use ^ instead of ~ (#90)

1.2.0 / 2016-04-06

  • deps: upgrade formstream to 1.0.0
  • feat: add rs /fetch action
  • doc: add uploadToken usage
  • deps: agentkeepalive@2.1.1
  • chore(package): update urllib to version 2.8.0
  • chore(package): update utility to version 1.6.0

1.1.1 / 2015-11-22

  • fix: Update client.js
  • chore(package): update mocha to version 2.3.4

1.1.0 / 2015-10-15

  • feat: use http keepalive
  • add 0.10 and iojs-1.2 test matrix
  • fix tests on windows
  • fix Duplicate key
  • add appveyor
  • chore: breaking change notification and fallback
  • chore: change domain to origin
  • fix example

1.0.1 / 2014-10-11

  • upload url can be config

1.0.0 / 2014-09-19

  • update deps
  • fix empty string test
  • Fix client.upload() example

0.2.2 / 2014-05-15

  • update urllib to 0.5.16

0.2.1 / 2014-03-10

  • update deps

0.2.0 / 2013-12-06

  • set content-length on rs op
  • add npm image
  • update test cases
  • download work with options.writeStream
  • move signData() to Qiniu

0.1.3 / 2013-09-09

  • use global default timeout if options.timeout not set.

0.1.2 / 2013-09-08

  • add download() and fix delete() key

0.1.1 / 2013-09-06

  • fixed key start with "/" bug

0.1.0 / 2013-09-06

  • add md2html()
  • fix tests

0.0.5 / 2013-09-05

  • add image operations
  • add qrcode(key, mode, level)
  • Refactor test dir
  • Refactor
  • fix tests for ci

0.0.4 / 2013-09-05

  • add batch() operations and list()

0.0.3 / 2013-09-05

  • add stat(), move(), copy() and delete()

0.0.2 / 2013-09-04

  • upload Stream without size work now.

0.0.1 / 2013-09-03

  • uploadFile()
  • add more test cases
  • add upload() with string and buffer
  • add logo
  • first commit