搭UploadServerCertificate配 AWS 開發套件或 CLI 使用 - AWS Identity and Access Management

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

UploadServerCertificate配 AWS 開發套件或 CLI 使用

下列程式碼範例會示範如何使用UploadServerCertificate

CLI
AWS CLI

將伺服器憑證上傳至您的 AWS 帳戶

下列upload-server-certificate指令會將伺服器憑證上傳至您的 AWS 帳戶。在此範例中,憑證位於檔案 public_key_cert_file.pem 中,相關聯的私密金鑰位於檔案 my_private_key.pem 中,而憑證授權機構 (CA) 提供的憑證鏈結位於 my_certificate_chain_file.pem 檔案中。檔案上傳完成後,即可在名稱下使用該檔案myServerCertificate。以 file:// 開頭的參數會告訴命令讀取檔案的內容,並將其用作參數值 (而不是檔案名稱本身)。

aws iam upload-server-certificate \ --server-certificate-name myServerCertificate \ --certificate-body file://public_key_cert_file.pem \ --private-key file://my_private_key.pem \ --certificate-chain file://my_certificate_chain_file.pem

輸出:

{ "ServerCertificateMetadata": { "Path": "/", "ServerCertificateName": "myServerCertificate", "ServerCertificateId": "ASCAEXAMPLE123EXAMPLE", "Arn": "arn:aws:iam::1234567989012:server-certificate/myServerCertificate", "UploadDate": "2019-04-22T21:13:44+00:00", "Expiration": "2019-10-15T22:23:16+00:00" } }

如需詳細資訊,請參閱《使用 IAM 指南》中的「建立、上傳和刪除伺服器憑證」。

JavaScript
適用於 JavaScript (v3) 的開發套件
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

import { UploadServerCertificateCommand, IAMClient } from "@aws-sdk/client-iam"; import { readFileSync } from "fs"; import { dirnameFromMetaUrl } from "@aws-doc-sdk-examples/lib/utils/util-fs.js"; import * as path from "path"; const client = new IAMClient({}); const certMessage = `Generate a certificate and key with the following command, or the equivalent for your system. openssl req -x509 -newkey rsa:4096 -sha256 -days 3650 -nodes \ -keyout example.key -out example.crt -subj "/CN=example.com" \ -addext "subjectAltName=DNS:example.com,DNS:www.example.net,IP:10.0.0.1" `; const getCertAndKey = () => { try { const cert = readFileSync( path.join(dirnameFromMetaUrl(import.meta.url), "./example.crt"), ); const key = readFileSync( path.join(dirnameFromMetaUrl(import.meta.url), "./example.key"), ); return { cert, key }; } catch (err) { if (err.code === "ENOENT") { throw new Error( `Certificate and/or private key not found. ${certMessage}`, ); } throw err; } }; /** * * @param {string} certificateName */ export const uploadServerCertificate = (certificateName) => { const { cert, key } = getCertAndKey(); const command = new UploadServerCertificateCommand({ ServerCertificateName: certificateName, CertificateBody: cert.toString(), PrivateKey: key.toString(), }); return client.send(command); };
PowerShell
適用的工具 PowerShell

範例 1:此範例會將新的伺服器憑證上傳至 IAM 帳戶。包含憑證主體、私密金鑰和 (選擇性) 憑證鏈結的檔案必須全部採用 PEM 編碼。請注意,參數需要檔案的實際內容,而不是檔案名稱。您必須使用 -Raw switch 參數才能成功處理檔案內容。

Publish-IAMServerCertificate -ServerCertificateName MyTestCert -CertificateBody (Get-Content -Raw server.crt) -PrivateKey (Get-Content -Raw server.key)

輸出:

Arn : arn:aws:iam::123456789012:server-certificate/MyTestCert Expiration : 1/14/2018 9:52:36 AM Path : / ServerCertificateId : ASCAJIEXAMPLE7J7HQZYW ServerCertificateName : MyTestCert UploadDate : 4/21/2015 11:14:16 AM

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱搭配 AWS SDK 使用此服務。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。