Skip to content

Instantly share code, notes, and snippets.

@qgy18
Created March 23, 2024 05:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qgy18/c7b1d2b61377c9ea888cd90160b348b3 to your computer and use it in GitHub Desktop.
Save qgy18/c7b1d2b61377c9ea888cd90160b348b3 to your computer and use it in GitHub Desktop.
update certificate to tencent cdn
const fs = require('fs');
const tencentcloud = require('tencentcloud-sdk-nodejs');
const TENCENTCLOUD_SECRET_ID = 'your secret id';
const TENCENTCLOUD_SECRET_KEY = 'your secret key';
const CERTIFICATE_PUBLIC_KEY_FILE = 'your public cert file';
const CERTIFICATE_PRIVATE_KEY_FILE = 'your private cert file';
const CERTIFICATE_DOMAIN = 'your domain';
async function uploadCertificate() {
const client = new tencentcloud.ssl.v20191205.Client({
credential: {
secretId: TENCENTCLOUD_SECRET_ID,
secretKey: TENCENTCLOUD_SECRET_KEY,
},
region: '',
profile: {
httpProfile: {
endpoint: 'ssl.tencentcloudapi.com',
},
},
});
const params = {
CertificatePublicKey : fs.readFileSync(CERTIFICATE_PUBLIC_KEY_FILE).toString(),
CertificatePrivateKey : fs.readFileSync(CERTIFICATE_PRIVATE_KEY_FILE).toString(),
Alias : `AUTO-${new Date().getFullYear()}-${new Date().getMonth() + 1}-${new Date().getDate()}`,
CertificateUse : 'CDN',
};
try {
let data = await client.UploadCertificate(params);
return data.CertificateId;
} catch (err) {
console.error('error', err);
return '';
}
}
async function updateDomainConfig(certificateId) {
const client = new tencentcloud.cdn.v20180606.Client({
credential: {
secretId: TENCENTCLOUD_SECRET_ID,
secretKey: TENCENTCLOUD_SECRET_KEY,
},
region: '',
profile: {
httpProfile: {
endpoint: 'cdn.tencentcloudapi.com',
},
},
});
const params = {
Domain : CERTIFICATE_DOMAIN,
Https : {
Switch : 'on',
CertInfo : {
CertId : certificateId,
},
}
};
try {
await client.UpdateDomainConfig(params);
} catch (err) {
console.error('error', err);
}
}
async function main() {
let certificateId = await uploadCertificate();
if (!certificateId) {
return;
}
console.log(`CertificateId: ${certificateId}`);
await updateDomainConfig(certificateId);
console.log('done.');
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment