轉載自:https://github.com/SinaCloudStorage/scs-sdk-jsjavascript
新浪雲存儲(SCS) SDK for Node.jsjava
npm install scs-sdk
SinaCloud
var SinaCloud = require('scs-sdk');
var config = new SinaCloud.Config({ accessKeyId: '你的accessKey', secretAccessKey: '你的secretKey', sslEnabled: false }); //全局生效: SinaCloud.config = config;
建立一個json文件config.json
:git
{ "accessKeyId": "你的accessKey", "secretAccessKey": "你的secretKey", "sslEnabled": true }
加載config.json
:github
//全局生效: SinaCloud.config.loadFromPath('./config.json');
var config = new SinaCloud.Config({ accessKeyId: '你的accessKey', secretAccessKey: '你的secretKey', sslEnabled: false }); //實例化: var s3 = new SinaCloud.S3(); //當前實例生效: s3.config = config;
var s3 = new SinaCloud.S3();
var mybucket = new SinaCloud.S3({params: {Bucket: 'mybucket'}});
var myobject = new SinaCloud.S3({params: {Bucket: 'mybucket', Key: 'mykey'}});
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"); } }); });
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 });
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 });
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);
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();
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();
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 + ']'); } });
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"); } });
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 } });
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 } });
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);