自动上传ssl证书到阿里云证书管理控制台中
由于oss需要自定义域名启动https,但是证书需要上传到阿里云的证书控制台上,而域名证书有效期是3个月,所以写个自动脚本更新上传
vscode新建个目录cert
然后执行
cd ./cert
npm init #然后一直回车
npm i @alicloud/openapi-client @alicloud/cas20200407 @alicloud/tea-util @alicloud/tea-typescript
新建index.js
文件,需准备好证书,生成证书请看:acme.sh生成证书
注意 config.endpoint
指定上传到哪里,可以到这里看填什么https://api.alibabacloud.com/product/cas
"use strict";
// This file is auto-generated, don't edit it
// Dependent modules can be viewed by downloading the module dependency file in the project or obtaining SDK dependency information in the upper right corner
const cas20200407 = require("@alicloud/cas20200407");
const OpenApi = require("@alicloud/openapi-client");
const Util = require("@alicloud/tea-util");Tea
// const Tea = require("@alicloud/tea-typescript");
const fs = require("fs");
class Client {
/**
* Initialize the Client with the AccessKey of the account
* @return Client
* @throws Exception
*/
static createClient() {
// The project code leakage may result in the leakage of AccessKey, posing a threat to the security of all resources under the account. The following code examples are for reference only.
// It is recommended to use the more secure STS credential. For more credentials, please refer to: https://www.alibabacloud.com/help/en/alibaba-cloud-sdk-262060/latest/credentials-settings-5.
let config = new OpenApi.Config({
// Required, please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID is set.
accessKeyId: process.env["ALIBABA_CLOUD_ACCESS_KEY_ID"],
// Required, please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_SECRET is set.
accessKeySecret: process.env["ALIBABA_CLOUD_ACCESS_KEY_SECRET"],
});
// See https://api.alibabacloud.com/product/cas.
config.endpoint = `cas.ap-southeast-1.aliyuncs.com`;
return new cas20200407.default(config);
}
static async main(args) {
let client = Client.createClient();
let uploadUserCertificateRequest =
new cas20200407.UploadUserCertificateRequest({
name: "your-custom-name",
cert: fs.readFileSync("./fullchain.pem").toString(),
key: fs.readFileSync("./privkey.pem").toString(),
});
let runtime = new Util.RuntimeOptions({});
try {
// Copy the code to run, please print the return value of the API by yourself.
await client.uploadUserCertificateWithOptions(
uploadUserCertificateRequest,
runtime
);
} catch (error) {
// Only a printing example. Please be careful about exception handling and do not ignore exceptions directly in engineering projects.
// print error message
console.log(error.message);
// Please click on the link below for diagnosis.
console.log(error.data["Recommend"]);
Util.default.assertAsString(error.message);
}
}
}
exports.Client = Client;
Client.main(process.argv.slice(2));
在ubuntu
服务器新增定时器,请确保服务器安装了nodejs
(本站有安装教程:安装nodejs)
sudo crontab -e
#添加 每月1号凌晨3点钟执行
0 3 1 * * /usr/bin/node /path/to/index.js
#确保脚本具有可执行权限
sudo chmod +x /path/to/index.js
License:
CC BY 4.0