Open Color-l opened 1 year ago
同步文件 fileSynchronization
方法,改成自己的上传方法。如果前端直传,就看看oss 的示例。如果后端上传,需要后端提供接口
这是一个qiniu + egg.js 后端例子
// app/service/topics.js
const Service = require('egg').Service;
var qiniu = require('qiniu');
const qiNiuConfig = require('../../config/qiniu');
const cdnJsBase = `https://cdnjs.cloudflare.com/ajax/libs`;
// https://developer.qiniu.com/kodo/1289/nodejs#5
class ObjectStorageService extends Service {
constructor(ctx) {
super(ctx);
// this.root = 'https://cnodejs.org/api/v1';
}
async create(params) {
var config = new qiniu.conf.Config();
// 空间对应的机房
config.zone = qiniu.zone.Zone_z0;
var accessKey = qiNiuConfig.client.accessKeyId;
var secretKey = qiNiuConfig.client.accessKeySecret;
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
const options = {
scope: qiNiuConfig.client.bucket,
expires: 7200
};
const putPolicy = new qiniu.rs.PutPolicy(options);
const uploadToken = putPolicy.uploadToken(mac);
// https://cdnjs.cloudflare.com/ajax/libs/vue/3.2.47/vue.cjs.min.js
const formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
var result = await this.ctx.curl(`${cdnJsBase}/${params.name}/${params.version}/${params.key}`, {
timeout: [ 30000, 30000 ]
}); // 可读的流
this.checkCdnjsFileSuccess(result);
const bufferString = result.data.toString();
const key = `ajax/libs/${params.name}/${params.version}/${params.key}`;
const putFile = () => new Promise((resolve, reject) => {
formUploader.put(uploadToken, key, bufferString, putExtra, function(respErr,
respBody, respInfo) {
if (respErr) {
reject(respErr);
}
resolve(respInfo);
});
});
const uploadFileRes = await putFile();
if (uploadFileRes.statusCode !== 200) {
this.ctx.throw(500, uploadFileRes?.data?.error || 'remote response error', {
statusCode: uploadFileRes.statusCode,
data: uploadFileRes.data
});
}
return uploadFileRes.data;
}
// 封装统一的调用检查函数,可以在查询、创建和更新等 Service 中复用
checkSuccess(result) {
if (result.status !== 200) {
const errorMsg =
result.data && result.data.error_msg
? result.data.error_msg
: 'unknown error';
this.ctx.throw(result.status, errorMsg);
}
if (!result.data.success) {
// 远程调用返回格式错误
this.ctx.throw(500, 'remote response error', { data: result.data });
}
}
checkCdnjsFileSuccess(result) {
if (result.status !== 200) {
const errorMsg =
result.data && result.data.error_msg
? result.data.error_msg
: 'unknown error';
this.ctx.throw(result.status, errorMsg);
}
}
}
module.exports = ObjectStorageService;
export const cdnDomain = 'https://cdnjs.iox7.com/' export const cdnFileFolder = '/ajax/libs/' export const cdnBaseURL =
${cdnDomain}${cdnFileFolder}
export const copyScript = null export const copyURL = null
cdnDomain怎么改成自己的链接地址,改完后无法同步