Created
March 23, 2024 05:15
-
-
Save qgy18/c7b1d2b61377c9ea888cd90160b348b3 to your computer and use it in GitHub Desktop.
update certificate to tencent cdn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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