code-push-server 添加sae(新浪云)的存储

首先感谢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,末尾不要带斜杠

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值