npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

scs-sdk

v0.1.4

Published

SCS SDK for JavaScript

Downloads

15

Readme

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);