新浪雲存儲(SCS) SDK for Node.js使用方法,node粉絲此次有福氣了!

node粉絲此次有福氣了!node

新浪雲存儲:http://open.sinastorage.com/
能夠去微博私信 @新浪雲存儲 獲取scs的邀請碼npm

使用方法以下:json

Installation

npm install scs-sdk

Usage

初始化SinaCloud

var SinaCloud = require('scs-sdk');

配置

方法1:
var config = new SinaCloud.Config({
    accessKeyId: '你的accessKey', 
    secretAccessKey: '你的secretKey',
    sslEnabled: false
});

//全局生效:
SinaCloud.config = config;
方法2:

建立一個json文件config.json:ui

{
    "accessKeyId": "你的accessKey", 
    "secretAccessKey": "你的secretKey",
    "sslEnabled": true
}

加載config.json:url

//全局生效:
SinaCloud.config.loadFromPath('./config.json');
方法3:
var config = new SinaCloud.Config({
    accessKeyId: '你的accessKey', 
    secretAccessKey: '你的secretKey',
    sslEnabled: false
});

//實例化:
var s3 = new SinaCloud.S3();
//當前示例生效:
s3.config = config;

實例化

示例1:
var s3 = new SinaCloud.S3();
示例2:
var mybucket = new SinaCloud.S3({params: {Bucket: 'mybucket'}});
示例3:
var myobject = new SinaCloud.S3({params: {Bucket: 'mybucket', Key: 'mykey'}});

調用

建立一個bucket並上傳一個文件:
var SinaCloud = require('scs-sdk');
SinaCloud.config.loadFromPath('./config.json');

var myBucket = new SinaCloud.S3({params: {Bucket: 'myBucket'}});
myBucket.createBucket(function() {
    var data = {Key: 'myKey', Body: 'Hello!'};
    myBucket.putObject(data, function(err, data) {
        if (err) {
            console.log("Error uploading data: ", err);
        } else {
            console.log("Successfully uploaded data to myBucket/myKey");
        }
    });
});
列出全部bucket:
var s3 = new SinaCloud.S3();

s3.listBuckets(function(err, data) {
    if (err)
        console.log(err, err.stack); // an error occurred
    else
        console.log(data);           // successful response
});
列出bucket中的文件:
var params = {
    Bucket: 'bucket-name',  //required
    Delimiter: '/',         //用'/'摺疊
    Marker: '',             //分頁標籤
    MaxKeys: 100,           //最大成員數
    Prefix: 'xxx'           //按前綴查詢
};

s3.listObjects(params, function(err, data) {

    if (err) 
        console.log(err, err.stack); // an error occurred
    else     
        console.log(data);           // successful response
});
下載文件示例1:
var s3 = new SinaCloud.S3();
var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg'};
var file = require('fs').createWriteStream('/path/to/file.jpg');
s3.getObject(params).createReadStream().pipe(file);
下載文件示例2:
var s3 = new SinaCloud.S3();
var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg'};
var file = require('fs').createWriteStream('/path/to/file.jpg');

s3.getObject(params).on('httpData', function(chunk) {
    file.write(chunk); 
}).on('httpDone', function() {
    file.end();
}).on('httpDownloadProgress', function(progress) {
    console.log(progress);
}).on('error', function(error) {
    console.log(error);
}).on('success', function() {
    console.log('success');
}).on('httpHeaders', function(statusCode, headers) {
    console.log('statusCode: ' + statusCode + "\n", headers);
}).send();
上傳文件示例1:
var s3 = new SinaCloud.S3();
var file = require('fs').createReadStream('/path/to/file.jpg');
var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg', Body: file};
s3.putObject(params).on('httpHeaders', function(statusCode, headers) { 
    console.log(headers);
}).on('httpUploadProgress', function(progress) {
    console.log(progress);
}).on('error', function(error) {
    console.log(error);
}).on('success', function() {
    console.log('success');
}).send();
上傳文件示例2:
var fileName = '/file/path/IMG_3218.JPG';
var remoteFilename = 'IMG_3218.JPG';
var s3 = new SinaCloud.S3();
var fileBuffer = require('fs').readFileSync(fileName);

s3.putObject({
    ACL: 'public-read',
    Bucket: 'mybucket',
    Key: remoteFilename,
    Body: fileBuffer
}, function(error, response) {
    if (error) {
        console.log(error);
    } else {
        console.log('uploaded file[' + fileName + '] to [' + remoteFilename + ']');
    }
});
上傳文件示例3:
var myBucket = new SinaCloud.S3({params: {Bucket: 'myBucket'}});
var data = {Key: 'myKey', Body: 'Hello!'};
myBucket.putObject(data, function(err, data) {
    if (err) {
        console.log("Error uploading data: ", err);
    } else {
        console.log("Successfully uploaded data to myBucket/myKey");
    }
});
獲取bucket的acl信息:
var s3bucket = new SinaCloud.S3({params: {Bucket: 'myBucket'}});

s3bucket.getBucketAcl(function(err, data) {
    if (err) {
        console.log(err);   // an error occurred
    } else {
        console.log(data);  // successful response
    }
});
刪除一個文件:
var s3 = new SinaCloud.S3();
var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg'};
s3.deleteObject(params, function(err, data) {
    if (err) {
        console.log(err);   // an error occurred
    } else {
        console.log(data);  // successful response
    }
});
刪除一個bucket:
var s3 = new SinaCloud.S3();
var params = {Bucket: 'myBucket'};
s3.deleteBucket(params, function(err, data) {
    if (err) {
        console.log(err);   // an error occurred
    } else {
        console.log(data);  // successful response
    }
});
獲取一個帶有簽名的用於下載的url:
var s3 = new SinaCloud.S3();

// This URL will expire in one minute (60 seconds)
var params = {Bucket: 'myBucket', Key: 'myKey', Expires: 60};
var url = s3.getSignedUrl('getObject', params);
console.log("The URL is", url);
相關文章
相關標籤/搜索