文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
ImportCertificate
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 ImportCertificate
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- C++
-
- C++ 的 SDK
-
注意
還有更多 on 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 詳細資訊,請參閱 ImportCertificate AWS SDK for C++ 參考中的 API。
-
- CLI
-
- AWS CLI
-
將憑證匯入 ACM。
下列
import-certificate
命令會將憑證匯入 ACM。將檔案名稱取代為您自己的檔案名稱:aws acm import-certificate --certificate
file://Certificate.pem
--certificate-chainfile://CertificateChain.pem
--private-keyfile://PrivateKey.pem
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ImportCertificate
。
-
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on 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 ImportCert { public static void main(String[] args) { final String usage = """ Usage: <certificatePath> <privateKeyPath> Where: certificatePath - the path to the SSL/TLS certificate file. privateKeyPath - the path to the private key file associated with the SSL/TLS certificate. """; if (args.length != 2) { System.out.println(usage); return; } String certificatePath = args[0]; String privateKeyPath = args[1]; String certificateArn = importCertificate(certificatePath, privateKeyPath); System.out.println("Certificate imported with ARN: " + certificateArn); } /** * Imports an SSL/TLS certificate and private key into AWS Certificate Manager (ACM) for use with * AWS services. * * @param certificatePath the file path to the SSL/TLS certificate * @param privateKeyPath the file path to the private key associated with the certificate * @throws IOException if there is an error reading the certificate or private key files */ public static String importCertificate(String certificatePath, String privateKeyPath) { AcmClient acmClient = AcmClient.create(); try { byte[] certificateBytes = readFileBytes(certificatePath); byte[] privateKeyBytes = readFileBytes(privateKeyPath); ImportCertificateRequest request = ImportCertificateRequest.builder() .certificate(SdkBytes.fromByteBuffer(ByteBuffer.wrap(certificateBytes))) .privateKey(SdkBytes.fromByteBuffer(ByteBuffer.wrap(privateKeyBytes))) .build(); ImportCertificateResponse response = acmClient.importCertificate(request); String certificateArn = response.certificateArn(); return certificateArn; } catch (IOException e) { System.err.println("Error reading certificate or private key file: " + e.getMessage()); } return ""; } private static byte[] readFileBytes(String filePath) throws IOException { try (InputStream inputStream = new FileInputStream(filePath)) { return IoUtils.toByteArray(inputStream); } } }
-
如需 API 詳細資訊,請參閱 ImportCertificate AWS SDK for Java 2.x 參考中的 API。
-
- Python
-
- SDK for Python (Boto3)
-
注意
還有更多 on 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 SDK for Python (Boto3) Word 參考中的 API。
-
GetCertificate
ListCertificates