[转]新浪云存储SCS Node.js使用指南
转载自:https://github.com/SinaCloudStorage/scs-sdk-js
scs-sdk-js
新浪云存储(SCS) SDK for Node.js
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
:
{
"accessKeyId": "你的accessKey",
"secretAccessKey": "你的secretKey",
"sslEnabled": true
}
加载config.json
:
//全局生效:
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);
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系统下SQL Server数据库镜像配置全流程详解
· 现代计算机视觉入门之:什么是视频
· 你所不知道的 C/C++ 宏知识
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· 盘点!HelloGitHub 年度热门开源项目
· DeepSeek V3 两周使用总结
· 02现代计算机视觉入门之:什么是视频
· C#使用yield关键字提升迭代性能与效率
· 回顾我的软件开发经历(1)