与 AWS SDK或ImportCertificate一起使用 CLI - AWS SDK代码示例

AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

与 AWS SDK或ImportCertificate一起使用 CLI

以下代码示例演示如何使用 ImportCertificate

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

C++
SDK对于 C++
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

//! Import an AWS Certificate Manager (ACM) certificate. /*! \param certificateFile: Path to certificate to import. \param privateKeyFile: Path to file containing a private key. \param certificateChainFile: Path to file containing a PEM encoded certificate chain. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::ACM::importCertificate(const Aws::String &certificateFile, const Aws::String &privateKeyFile, const Aws::String &certificateChainFile, const Aws::Client::ClientConfiguration &clientConfiguration) { std::ifstream certificateInStream(certificateFile.c_str()); if (!certificateInStream) { std::cerr << "Error: The certificate file '" << certificateFile << "' does not exist." << std::endl; return false; } std::ifstream privateKeyInstream(privateKeyFile.c_str()); if (!privateKeyInstream) { std::cerr << "Error: The private key file '" << privateKeyFile << "' does not exist." << std::endl; return false; } std::ifstream certificateChainInStream(certificateChainFile.c_str()); if (!certificateChainInStream) { std::cerr << "Error: The certificate chain file '" << certificateChainFile << "' does not exist." << std::endl; return false; } Aws::String certificate; certificate.assign(std::istreambuf_iterator<char>(certificateInStream), std::istreambuf_iterator<char>()); Aws::String privateKey; privateKey.assign(std::istreambuf_iterator<char>(privateKeyInstream), std::istreambuf_iterator<char>()); Aws::String certificateChain; certificateChain.assign(std::istreambuf_iterator<char>(certificateChainInStream), std::istreambuf_iterator<char>()); Aws::ACM::ACMClient acmClient(clientConfiguration); Aws::ACM::Model::ImportCertificateRequest request; request.WithCertificate(Aws::Utils::ByteBuffer((unsigned char *) certificate.c_str(), certificate.size())) .WithPrivateKey(Aws::Utils::ByteBuffer((unsigned char *) privateKey.c_str(), privateKey.size())) .WithCertificateChain(Aws::Utils::ByteBuffer((unsigned char *) certificateChain.c_str(), certificateChain.size())); Aws::ACM::Model::ImportCertificateOutcome outcome = acmClient.ImportCertificate(request); if (!outcome.IsSuccess()) { std::cerr << "Error: ImportCertificate: " << outcome.GetError().GetMessage() << std::endl; return false; } else { std::cout << "Success: Certificate associated with ARN '" << outcome.GetResult().GetCertificateArn() << "' imported." << std::endl; return true; } }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 ImportCertificate” 中的。

CLI
AWS CLI

将证书导入ACM。

以下import-certificate命令将证书导入到ACM。将文件名替换为您自己的文件名:

aws acm import-certificate --certificate file://Certificate.pem --certificate-chain file://CertificateChain.pem --private-key file://PrivateKey.pem
Python
SDK适用于 Python (Boto3)
注意

还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库中进行设置和运行。

class AcmCertificate: """ Encapsulates ACM functions. """ def __init__(self, acm_client): """ :param acm_client: A Boto3 ACM client. """ self.acm_client = acm_client def import_certificate(self, certificate_body, private_key): """ Imports a self-signed certificate to ACM. :param certificate_body: The body of the certificate, in PEM format. :param private_key: The unencrypted private key of the certificate, in PEM format. :return: The ARN of the imported certificate. """ try: response = self.acm_client.import_certificate( Certificate=certificate_body, PrivateKey=private_key ) certificate_arn = response["CertificateArn"] logger.info("Imported certificate.") except ClientError: logger.exception("Couldn't import certificate.") raise else: return certificate_arn
  • 有关API详细信息,请参阅ImportCertificate中的 AWS SDKPython (Boto3) API 参考。