UpdateServerCertificate 搭配 AWS SDK或 使用 CLI - AWS SDK 程式碼範例

文件範例儲存庫中有更多 AWS SDK可用的範例。 AWS SDK GitHub

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

UpdateServerCertificate 搭配 AWS SDK或 使用 CLI

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

C++
SDK 適用於 C++
注意

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

bool AwsDoc::IAM::updateServerCertificate(const Aws::String &currentCertificateName, const Aws::String &newCertificateName, const Aws::Client::ClientConfiguration &clientConfig) { Aws::IAM::IAMClient iam(clientConfig); Aws::IAM::Model::UpdateServerCertificateRequest request; request.SetServerCertificateName(currentCertificateName); request.SetNewServerCertificateName(newCertificateName); auto outcome = iam.UpdateServerCertificate(request); bool result = true; if (outcome.IsSuccess()) { std::cout << "Server certificate " << currentCertificateName << " successfully renamed as " << newCertificateName << std::endl; } else { if (outcome.GetError().GetErrorType() != Aws::IAM::IAMErrors::NO_SUCH_ENTITY) { std::cerr << "Error changing name of server certificate " << currentCertificateName << " to " << newCertificateName << ":" << outcome.GetError().GetMessage() << std::endl; result = false; } else { std::cout << "Certificate '" << currentCertificateName << "' not found." << std::endl; } } return result; }
CLI
AWS CLI

變更 AWS 帳戶中伺服器憑證的路徑或名稱

下列 update-server-certificate 命令會將憑證名稱從 myServerCertificate 變更為 myUpdatedServerCertificate。它也會變更 的路徑,/cloudfront/以便 Amazon CloudFront 服務可以存取。此命令不會產生輸出。您可以透過執行 list-server-certificates 命令來查看更新的結果。

aws-iam update-server-certificate \ --server-certificate-name myServerCertificate \ --new-server-certificate-name myUpdatedServerCertificate \ --new-path /cloudfront/

此命令不會產生輸出。

如需詳細資訊,請參閱 AWS IAM 使用者指南 中的管理伺服器憑證IAM

JavaScript
SDK 適用於 JavaScript (v3)
注意

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

更新伺服器憑證。

import { UpdateServerCertificateCommand, IAMClient } from "@aws-sdk/client-iam"; const client = new IAMClient({}); /** * * @param {string} currentName * @param {string} newName */ export const updateServerCertificate = (currentName, newName) => { const command = new UpdateServerCertificateCommand({ ServerCertificateName: currentName, NewServerCertificateName: newName, }); return client.send(command); };
SDK 適用於 JavaScript (v2)
注意

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

// Load the AWS SDK for Node.js var AWS = require("aws-sdk"); // Set the region AWS.config.update({ region: "REGION" }); // Create the IAM service object var iam = new AWS.IAM({ apiVersion: "2010-05-08" }); var params = { ServerCertificateName: "CERTIFICATE_NAME", NewServerCertificateName: "NEW_CERTIFICATE_NAME", }; iam.updateServerCertificate(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
PowerShell
適用於 的工具 PowerShell

範例 1:此範例會將名為 的憑證重新命名MyServerCertificateMyRenamedServerCertificate

Update-IAMServerCertificate -ServerCertificateName MyServerCertificate -NewServerCertificateName MyRenamedServerCertificate

範例 2:此範例會將名為 MyServerCertificate的憑證移至 path /Org1/Org2/。這會將 資源ARN的 變更為 arn:aws:iam::123456789012:server-certificate/Org1/Org2/MyServerCertificate

Update-IAMServerCertificate -ServerCertificateName MyServerCertificate -NewPath /Org1/Org2/
Ruby
SDK 適用於 Ruby
注意

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

列出、更新和刪除伺服器憑證。

class ServerCertificateManager def initialize(iam_client, logger: Logger.new($stdout)) @iam_client = iam_client @logger = logger @logger.progname = 'ServerCertificateManager' end # Creates a new server certificate. # @param name [String] the name of the server certificate # @param certificate_body [String] the contents of the certificate # @param private_key [String] the private key contents # @return [Boolean] returns true if the certificate was successfully created def create_server_certificate(name, certificate_body, private_key) @iam_client.upload_server_certificate({ server_certificate_name: name, certificate_body: certificate_body, private_key: private_key }) true rescue Aws::IAM::Errors::ServiceError => e puts "Failed to create server certificate: #{e.message}" false end # Lists available server certificate names. def list_server_certificate_names response = @iam_client.list_server_certificates if response.server_certificate_metadata_list.empty? @logger.info('No server certificates found.') return end response.server_certificate_metadata_list.each do |certificate_metadata| @logger.info("Certificate Name: #{certificate_metadata.server_certificate_name}") end rescue Aws::IAM::Errors::ServiceError => e @logger.error("Error listing server certificates: #{e.message}") end # Updates the name of a server certificate. def update_server_certificate_name(current_name, new_name) @iam_client.update_server_certificate( server_certificate_name: current_name, new_server_certificate_name: new_name ) @logger.info("Server certificate name updated from '#{current_name}' to '#{new_name}'.") true rescue Aws::IAM::Errors::ServiceError => e @logger.error("Error updating server certificate name: #{e.message}") false end # Deletes a server certificate. def delete_server_certificate(name) @iam_client.delete_server_certificate(server_certificate_name: name) @logger.info("Server certificate '#{name}' deleted.") true rescue Aws::IAM::Errors::ServiceError => e @logger.error("Error deleting server certificate: #{e.message}") false end end