-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cdnDomain自定义 #1
Comments
同步文件 |
这是一个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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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怎么改成自己的链接地址,改完后无法同步
The text was updated successfully, but these errors were encountered: