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

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

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

与 AWS SDK或RequestCertificate一起使用 CLI

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

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

C++
SDK对于 C++
注意

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

//! Request an AWS Certificate Manager (ACM) certificate. /*! \param domainName: A fully qualified domain name. \param idempotencyToken: Customer chosen string for idempotency. \param clientConfiguration: AWS client configuration. \return bool: Function succeeded. */ bool AwsDoc::ACM::requestCertificate(const Aws::String &domainName, const Aws::String &idempotencyToken, const Aws::Client::ClientConfiguration &clientConfiguration) { Aws::ACM::ACMClient acmClient(clientConfiguration); Aws::ACM::Model::RequestCertificateRequest request; request.WithDomainName(domainName) .WithIdempotencyToken(idempotencyToken); Aws::ACM::Model::RequestCertificateOutcome outcome = acmClient.RequestCertificate(request); if (!outcome.IsSuccess()) { std::cerr << "RequestCertificate error: " << outcome.GetError().GetMessage() << std::endl; return false; } else { std::cout << "Success: The newly requested certificate's " "ARN is '" << outcome.GetResult().GetCertificateArn() << "'." << std::endl; return true; } }
  • 有关API详细信息,请参阅 “AWS SDK for C++ API参考 RequestCertificate” 中的。

CLI
AWS CLI

申请新ACM证书

以下request-certificate命令使用验证请求 www.example.com 域名的新证书:DNS

aws acm request-certificate --domain-name www.example.com --validation-method DNS

您可以输入幂等性令牌,以区分对 request-certificate 的调用:

aws acm request-certificate --domain-name www.example.com --validation-method DNS --idempotency-token 91adc45q

您可以输入一个或多个主题备用名称来请求能够保护多个顶级域的证书:

aws acm request-certificate --domain-name example.com --validation-method DNS --idempotency-token 91adc45q --subject-alternative-names www.example.net

您可以输入一个备用名称,该名称也可用于访问您的网站:

aws acm request-certificate --domain-name example.com --validation-method DNS --idempotency-token 91adc45q --subject-alternative-names www.example.com

您可以使用星号(*)作为通配符为同一域中的多个子域创建证书:

aws acm request-certificate --domain-name example.com --validation-method DNS --idempotency-token 91adc45q --subject-alternative-names *.example.com

您也可以输入多个备用名称:

aws acm request-certificate --domain-name example.com --validation-method DNS --subject-alternative-names b.example.com c.example.com d.example.com

如果您使用电子邮件进行验证,则可以输入域验证选项来指定将验证电子邮件发送到的域:

aws acm request-certificate --domain-name example.com --validation-method EMAIL --subject-alternative-names www.example.com --domain-validation-options DomainName=example.com,ValidationDomain=example.com

以下命令会在您请求新证书时选择退出证书透明度日志:

aws acm request-certificate --domain-name www.example.com --validation-method DNS --options CertificateTransparencyLoggingPreference=DISABLED --idempotency-token 184627
Java
SDK适用于 Java 2.x
注意

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

/** * Before running this Java V2 code example, set up your development * environment, including your credentials. * <p> * For more information, see the following documentation topic: * <p> * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class RequestCert { public static void main(String[] args) { requestCertificate(); } /** * Requests a certificate from the AWS Certificate Manager (ACM) service. */ public static void requestCertificate() { AcmClient acmClient = AcmClient.create(); ArrayList<String> san = new ArrayList<>(); san.add("www.example.com"); RequestCertificateRequest req = RequestCertificateRequest.builder() .domainName("example.com") .idempotencyToken("1Aq25pTy") .subjectAlternativeNames(san) .build(); try { RequestCertificateResponse response = acmClient.requestCertificate(req); System.out.println("Cert ARN IS " + response.certificateArn()); } catch (AcmException e) { System.err.println(e.getMessage()); } } }
  • 有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 RequestCertificate” 中的。

PowerShell
用于 PowerShell

示例 1:创建新证书。该服务会ARN返回新证书的。

New-ACMCertificate -DomainName "www.example.com"

输出:

arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012

示例 2:创建新证书。该服务会ARN返回新证书的。

New-ACMCertificate -DomainName "www.example.com" -SubjectAlternativeName "example.com","www.example.net"

输出:

arn:aws:acm:us-east-1:123456789012:certificate/12345678-1234-1234-1234-123456789012
  • 有关API详细信息,请参阅 AWS Tools for PowerShell Cmdlet 参考RequestCertificate中的。

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 request_validation( self, domain, alternate_domains, method, validation_domains=None ): """ Starts a validation request that results in a new certificate being issued by ACM. DNS validation requires that you add CNAME records to your DNS provider. Email validation sends email to a list of email addresses that are associated with the domain. For more information, see _Issuing and managing certificates_ in the ACM user guide. https://docs.aws.amazon.com/acm/latest/userguide/gs.html :param domain: The primary domain to associate with the certificate. :param alternate_domains: Subject Alternate Names (SANs) for the certificate. :param method: The validation method, either DNS or EMAIL. :param validation_domains: Alternate domains to use for email validation, when the email domain differs from the primary domain of the certificate. :return: The ARN of the requested certificate. """ try: kwargs = { "DomainName": domain, "ValidationMethod": method, "SubjectAlternativeNames": alternate_domains, } if validation_domains is not None: kwargs["DomainValidationOptions"] = [ {"DomainName": key, "ValidationDomain": value} for key, value in validation_domains.items() ] response = self.acm_client.request_certificate(**kwargs) certificate_arn = response["CertificateArn"] logger.info( "Requested %s validation for domain %s. Certificate ARN is %s.", method, domain, certificate_arn, ) except ClientError: logger.exception( "Request for %s validation of domain %s failed.", method, domain ) raise else: return certificate_arn
  • 有关API详细信息,请参阅RequestCertificate中的 AWS SDKPython (Boto3) API 参考。