cloud-object-storage
v3.0.1
Published
cloud object storage for QCloud, QiNiu and local
Downloads
1
Maintainers
Readme
cloud object storage for QCloud, QiNiu and local
Code example
var config = require('./config');
var express = require('express');
var busboy = require('connect-busboy');
const bodyParser = require('body-parser');
var bytes = require('bytes');
// Init and config here
var store = require('cloud-object-storage');
store.Config.configQCloud(config.cos_access);
var app = express();
app.use(bodyParser.json({limit: '1mb'}));
app.use(bodyParser.urlencoded({ extended: true, limit: '1mb' }));
app.use(busboy({
limits: {
fileSize: bytes(config.file_limit)
}
}));
app.post('/upload', function (req, res) {
var isFileLimit = false;
req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
file.on('limit', function () {
isFileLimit = true;
res.json({
success: false,
msg: 'File size too large. Max is ' + config.file_limit
})
});
// upload file to QCloud service
store.QCloud.upload(file, {filename: filename}, function (err, result) {
if (err) {
return res.json({
success: false,
message: err.message
});
}
if (isFileLimit) {
return;
}
res.json({
success: true,
url: result.url,
});
});
});
req.pipe(req.busboy);
});
var server = app.listen(7003, function () {
var host = server.address().address;
var port = server.address().port;
console.log("File app listening at http://%s:%s", host, port)
});
config file with data structure
/**
* config
*/
var path = require('path');
var config = {
qn_access: {
accessKey: 'JPlCnNEXts1bh***Elj8FK99vQoV0L',
secretKey: 'cd3tZQYvo8MI***Lj7x3QdYJGXtNED',
bucket: 'techt',
origin: 'http://opzmk10r6.bkt.clouddn.com',
uploadURL: 'https://up-z2.qbox.me',
},
cos_access: {
cosAppId: '1252484566',
cosRegion: 'cn-south',
cosSecretId: 'AKID8HG5jLSG***b1Wr3860nPq7ECjf9W',
cosSecretKey: 'NbeWHsIdqsn***Kfr24xiOzQhu',
cosFileBucket: 'little7',
cosUploadFolder: '/'
},
upload: {
path: path.join(__dirname, 'public/upload/'),
url: '/public/upload/'
},
file_limit: '1MB',
};
module.exports = config;