发布一个 Node.js 的又拍云 SDK—— ez-upyun
发布在Node.js之撸2014年8月3日view:6559
在文章任何区域双击击即可给文章添加【评注】!浮到评注点上可以查看详情。

最近要做个项目,手机 APP 的,决定把图片放又拍云。

去 npm 找大多都是下载量寥寥无几的,不是太敢用啊,于是自己写了个。

—— 实际上我自己写的这个我自己放心,但是在你们眼里也是属于下载量寥寥无几不敢用的那种。


https://github.com/XadillaX/ez-upyun

这个是 Repo,以及

$ npm install ez-upyun

就可以用了。

并且文档贴上?

Eazy Upyun

Yet another upyun SDK for node.js which are eazy to use.

Installation

Via NPM

$ npm install ez-upyun

Via Git

$ git clone https://github.com/XadillaX/ez-upyun.git

Download Manually

$ wget https://github.com/XadillaX/ez-upyun/archive/master.zip

Or open the url above in your browser.

Usage

First you should create an Upyun instance:

var Upyun = require("ez-upyun").Upyun;
var upyun = new Upyun("bucket", "user", "password");

Attention: username and password is the operator username and password for that certain bucket.

Upyun Class

Methods of Upyun class are shown below:

  • testAuth([callback])
  • upload(filename|buffer, uploadFilename, [options], [callback])
  • download(filepath, [callback])
  • mkdir(directory, [autoParentFolder], [callback])
  • info(filepath, [callback])
  • list(directory, [callback])
  • rm(filepath, [callback])
  • fetchUsage([callback])
  • forceRemove(path, [callback]) *

testAuth

This method is just for testing whether your account is available. Callback function will return you two arguments, error and response header. (Someone may never call it)

Eg.

upyun.testAuth(function(err, header) {
    if(err) return console.log(err.message);
    console.log(header);
})

You may got:

{ server: 'nginx/upyun@403',
  date: 'Fri, 01 Aug 2014 13:12:07 GMT',
  'content-type': 'text/html',
  'transfer-encoding': 'chunked',
  connection: 'keep-alive',
  'access-control-allow-origin': '*' }

upload

Upload a file to upyun server.

The arguments are shown below:

  • filename | buffer: when you pass a existing filename, ez-upyun will read your file at first and then upload it. When it’s a binary buffer or string, ez-upyun will use it directly.
  • uploadFilename: this is the remote filename which you want to use. You can put a path name in it. Eg. foo/bar.jpg.
  • options: an object with some extra options:
    • mkdir: when true, Upyun will create all your precursor folder. Otherwise it will not. Default to true.
    • md5: when true, ez-upyun will calculate out the MD5 Value for your file and put it into header. Also you can calculate out the MD5 Value by your self and then put the 32 length string into this field. When false, it will never validate md5. Default to false.
    • secret: default to undefined. Once you put a string into this field, your picture bucket will protect your this upload with this secret value. Refer here.
    • mime: you can upload the file in a certain MIME type. Default to undefined.
  • callback: The callback function will pass two arguments, error and result. If successfully, result will be an object. And if your bucket is a picture one, this object will contains: {width: Number, height: Number, frames: Number, type: String}.

Eg.

fs.readFile("path", function(err, data) {
    if(err) ...;

    upyun.upload(data, "foo/bar.jpg", { md5: true, secret: "fuli" }, function(err, result) {
        if(err) ...;

        console.log(result);
    });
});

// Or like this...

upyun.upload("path", "foo/bar.jpg", function(err, result) {});

download

File path is a string, then you can download the file with that path. The binary data will pass through your callback function:

upyun.download("foo/bar.jpg", function(err, data) {
    fs.writeFile("path/bar.jpg", data, function(err) {});
});

mkdir

Make a directory on the remote upyun server. autoParentFolder is optional, it stands for whether upyun will create the precursor folders, default to true.

Eg.

upyun.mkdir("foo/bar/and/bar", true, function(err, directoryName) {});

// Or below is the same

upyun.mkdir("foo/bar/and/bar", function(err, directoryName) {});

// Or not to make precursor folders

upyun.mkdir("foo/bar/and/bar", false, function(...) {});

info

To get the information of one certain file or folder.

The second argument that callback throughs is an object:

{
    filename    : "filename",
    type        : "file|folder",    // `file` stands for a file and `folder` stands for a folder
    size        : Number,           // file size
    timestamp   : Number,           // a 10 length long integer stands for a timestamp
    date        : Date,             // a `Date` type object which matches timestamp
    dateString  : String            // a formatted date string
}

Eg.

upyun.info("foo.jpg", function(err, info) {
    if(err) ...;
    console.log(info.filename);
});

list

List all items in one folder (not recur). Result is an item array and each item is an object looks like below:

{
    filename    : "filename",
    type        : "N|F",            // `N` stands for a file and `F` stands for a folder
    size        : Number,           // file size
    timestamp   : Number,           // a 10 length long integer stands for a timestamp
    date        : Date,             // a `Date` type object which matches timestamp
    dateString  : String            // a formatted date string
}

Eg.

upyun.list("/", function(err, list) {
    if(err) ...;
    for(var i = 0; i < list.length; i++) {
        console.log(list[i].filename);
    }
});

rm

Delete a file or an empty folder.

Attention: this function can’t delete a non-empty folder.

The callback function will pass two arguments, error and filename deleted.

Eg.

upyun.rm("foo/bar.jpg", function(err, filename) {});

fetchUsage

This function will return the storage usage of your bucket. The result will be returned as an object like:

{
    b : Number,
    kb: Number,
    mb: Number,
    gb: Number,
    tb: Number
}

Each element is integral. You can use any of them.

Eg.

upyun.fetchUsage(function(err, usage) {
    if(err) ...;
    console.log("You have used " + usage.b + " byte.");
});

forceRemove*

Caution: use this function carefully. It can delete any remote file or folder (both empty and non-empty, it will delete recurred).

The two arguments of this function are filename / path and callback. When it’s a filename or en empty folder, this function will do the same thing as rm does. When it’s a non-empty folder, the function will delete this folder and all of its children file and folder.

The two arguments of callback passes are error and delete count. Delete count is an json object which contains both count of file and folder are deleted.

Eg.

upyun.forceRemove("foo", function(err, delCount) {
    if(err) ...;

    console.log(delCount.file + " file(s) are deleted.");
    console.log(delCount.dir + " folder(s) are deleted.")
});
评论
发表评论
暂无评论
WRITTEN BY
機巧死月不會碼代碼
这是一个伪宅,也是一只码畜。
TA的新浪微博
PUBLISHED IN
Node.js之撸

让我们走上一条Node.js之撸吧!

我的收藏