首先感谢lisong的开源代码
https://github.com/lisong/code-push-server
原本我也是用qiniu存储的,但2018年10月,七牛来邮件通知,测试域名要回收了
除非是绑定了自个的已备案的域名,不然只能凉凉了,备案要很久
决定改换新浪云存储
1、安装SDK
执行 npm install scs-sdk
2、修改两个文件后提交git
core/utils/common.js
文件开头添加导入
var sinaCloud = require('scs-sdk');//2018-11-26
添加函数
common.uploadFileToSae = function (key, filePath) {
return (
new Promise((resolve, reject) => {
var saeconfig = new sinaCloud.Config({
accessKeyId: _.get(config, "sae.accessKeyId"),
secretAccessKey: _.get(config, "sae.secretKey"),
sslEnabled: false
});
//实例化:
var saeBucket = new sinaCloud.S3();
saeBucket.config = saeconfig;
fs.readFile(filePath, (err, data) => {
var upSaeData = {
ACL: 'public-read',
Key: key,
Body: data,
Bucket: _.get(config, "sae.bucketName", "")
};
saeBucket.putObject(upSaeData, function(err, data) {
if (err) {
reject(new AppError.AppError(JSON.stringify(err)));
//console.log("Error uploading data: ", err);
} else {
//console.log("Successfully uploaded data to myBucket/myKey");
resolve(data)
}
});
});
})
);
};
更新函数
common.getBlobDownloadUrl = function (blobUrl) {
var downloadUrl = '';
var fileName = blobUrl;
if (_.get(config, 'common.storageType') === 'local') {
downloadUrl = _.get(config, 'local.downloadUrl');
fileName = blobUrl.substr(0, 2).toLowerCase() + '/' + blobUrl;
} else if (_.get(config, 'common.storageType') === 's3') {
downloadUrl = _.get(config, 's3.downloadUrl');
} else if (_.get(config, 'common.storageType') === 'oss') {
downloadUrl = _.get(config, 'oss.downloadUrl');
} else if (_.get(config, 'common.storageType') === 'qiniu') {
downloadUrl = _.get(config, 'qiniu.downloadUrl');
} else if (_.get(config, 'common.storageType') === 'sae') {
downloadUrl = _.get(config, 'sae.downloadUrl');
}
return `${downloadUrl}/${fileName}`
};
common.uploadFileToStorage = function (key, filePath) {
if (_.get(config, 'common.storageType') === 'local') {
return common.uploadFileToLocal(key, filePath);
} else if (_.get(config, 'common.storageType') === 's3') {
return common.uploadFileToS3(key, filePath);
} else if (_.get(config, 'common.storageType') === 'oss') {
return common.uploadFileToOSS(key, filePath);
} else if (_.get(config, 'common.storageType') === 'sae') {
return common.uploadFileToSae(key, filePath);
}
return common.uploadFileToQiniu(key, filePath);
};
config/config.js
在config.development 添加一个结构
// Config for sae(sina) cloud storage when storageType value is "sae". 2018-11-26
sae: {
accessKeyId: process.env.SAE_ACCESS_KEY_ID || "",
secretKey: process.env.SAE_SECRET_KEY || "",
bucketName: process.env.SAE_BUCKET_NAME || "",
downloadUrl: process.env.SAE_DOWNLOAD_URL || "" // Binary files download host address.
},
3、修改环境变量
添加四个变量
注意SAE_DOWNLOAD_URL
如果您的bucket名称为:my-bucket
, 则设置为
https://sinacloud.net/my-bucket,末尾不要带斜杠