Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.
Ejemplos de ACM con SDK for Java 2.x
Los siguientes ejemplos de código muestran cómo realizar acciones e implementar escenarios comunes mediante el uso de AWS SDK for Java 2.x ACM.
Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las distintas funciones de servicio, es posible ver las acciones en contexto en los escenarios relacionados.
En cada ejemplo se incluye un enlace al código de origen completo, con instrucciones de configuración y ejecución del código en el contexto.
Temas
Acciones
En el siguiente ejemplo de código, se muestra cómo utilizar AddTagsToCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 AddTagsToCertificate { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; addTags(certArn); } /** * Adds tags to a certificate in AWS Certificate Manager (ACM). * * @param certArn the Amazon Resource Name (ARN) of the certificate to add tags to */ public static void addTags(String certArn) { AcmClient acmClient = AcmClient.create(); List<Tag> expectedTags = List.of(Tag.builder().key("key").value("value").build()); AddTagsToCertificateRequest addTagsToCertificateRequest = AddTagsToCertificateRequest.builder() .certificateArn(certArn) .tags(expectedTags) .build(); try { acmClient.addTagsToCertificate(addTagsToCertificateRequest); System.out.println("Successfully added tags to a certificate"); } catch (AcmException e) { System.out.println(e.getMessage()); } } }
-
Para obtener más información sobre la API, consulta AddTagsToCertificatela Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar DeleteCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 DeleteCert { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; deleteCertificate(certArn); } /** * Deletes an SSL/TLS certificate from the AWS Certificate Manager (ACM). * * @param certArn the Amazon Resource Name (ARN) of the certificate to be deleted */ public static void deleteCertificate( String certArn) { AcmClient acmClient = AcmClient.create(); DeleteCertificateRequest request = DeleteCertificateRequest.builder() .certificateArn(certArn) .build(); try { acmClient.deleteCertificate(request); System.out.println("The certificate was deleted"); } catch (AcmException e) { System.out.println(e.getMessage()); } } }
-
Para obtener más información sobre la API, consulta DeleteCertificatela Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar DescribeCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 DescribeCert { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; describeCertificate(certArn); } /** * Describes the details of an SSL/TLS certificate. * * @param certArn the Amazon Resource Name (ARN) of the certificate to describe * @throws AcmException if an error occurs while describing the certificate */ public static void describeCertificate(String certArn) { AcmClient acmClient = AcmClient.create(); DescribeCertificateRequest req = DescribeCertificateRequest.builder() .certificateArn(certArn) .build(); try { DescribeCertificateResponse response = acmClient.describeCertificate(req); // Print the certificate details. System.out.println("Certificate ARN: " + response.certificate().certificateArn()); System.out.println("Domain Name: " + response.certificate().domainName()); System.out.println("Issued By: " + response.certificate().issuer()); System.out.println("Issued On: " + response.certificate().issuedAt()); System.out.println("Status: " + response.certificate().status()); } catch (AcmException e) { System.out.println(e.getMessage()); } } }
-
Para obtener más información sobre la API, consulta DescribeCertificatela Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar ExportCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 ExportCertificate { public static void main(String[] args) throws Exception { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; exportCert(certArn); } /** * Exports an SSL/TLS certificate and its associated private key and certificate chain from AWS Certificate Manager (ACM). * * @param certArn The Amazon Resource Name (ARN) of the certificate that you want to export. * @throws IOException If an I/O error occurs while reading the private key passphrase file or exporting the certificate. */ public static void exportCert(String certArn) throws IOException { AcmClient acmClient = AcmClient.create(); // Initialize a file descriptor for the passphrase file. RandomAccessFile filePassphrase = null; ByteBuffer bufPassphrase = null; // Create a file stream for reading the private key passphrase. try { filePassphrase = new RandomAccessFile("C:\\AWS\\password.txt", "r"); } catch (IllegalArgumentException | SecurityException | FileNotFoundException ex) { throw ex; } // Create a channel to map the file. FileChannel channelPassphrase = filePassphrase.getChannel(); // Map the file to the buffer. try { bufPassphrase = channelPassphrase.map(FileChannel.MapMode.READ_ONLY, 0, channelPassphrase.size()); channelPassphrase.close(); filePassphrase.close(); } catch (IOException ex) { throw ex; } // Create a request object. ExportCertificateRequest req = ExportCertificateRequest.builder() .certificateArn(certArn) .passphrase(SdkBytes.fromByteBuffer(bufPassphrase)) .build(); // Export the certificate. ExportCertificateResponse result = null; try { result = acmClient.exportCertificate(req); } catch (InvalidArnException | InvalidTagException | ResourceNotFoundException ex) { throw ex; } // Clear the buffer. bufPassphrase.clear(); // Display the certificate and certificate chain. String certificate = result.certificate(); System.out.println(certificate); String certificateChain = result.certificateChain(); System.out.println(certificateChain); // This example retrieves but does not display the private key. String privateKey = result.privateKey(); System.out.println("The example is complete"); } }
-
Para obtener más información sobre la API, consulta ExportCertificatela Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar ImportCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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); } } }
-
Para obtener más información sobre la API, consulta ImportCertificatela Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar ListCertificates
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 ListCerts { public static void main(String[] args) { listCertificates(); } /** * Lists all the certificates managed by AWS Certificate Manager (ACM) that have a status of "ISSUED". */ public static void listCertificates() { AcmClient acmClient = AcmClient.create(); try { ListCertificatesRequest listRequest = ListCertificatesRequest.builder() .certificateStatuses(CertificateStatus.ISSUED) .maxItems(100) .build(); ListCertificatesIterable listResponse = acmClient.listCertificatesPaginator(listRequest); // Print the certificate details using streams listResponse.certificateSummaryList().stream() .forEach(certificate -> { System.out.println("Certificate ARN: " + certificate.certificateArn()); System.out.println("Certificate Domain Name: " + certificate.domainName()); System.out.println("Certificate Status: " + certificate.statusAsString()); System.out.println("---"); }); } catch (AcmException e) { System.err.println(e.getMessage()); } } }
-
Para obtener más información sobre la API, consulta ListCertificatesla Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar ListTagsForCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 ListCertTags { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; listCertTags(certArn); } /** * Lists the tags associated with an AWS Certificate Manager (ACM) certificate. * * @param certArn the Amazon Resource Name (ARN) of the ACM certificate */ public static void listCertTags(String certArn) { AcmClient acmClient = AcmClient.create(); ListTagsForCertificateRequest request = ListTagsForCertificateRequest.builder() .certificateArn(certArn) .build(); ListTagsForCertificateResponse response = acmClient.listTagsForCertificate(request); List<Tag> tagList = response.tags(); tagList.forEach(tag -> { System.out.println("Key: " + tag.key()); System.out.println("Value: " + tag.value()); }); } }
-
Para obtener más información sobre la API, consulta ListTagsForCertificatela Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar RemoveTagsFromCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 RemoveTagsFromCert { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; removeTags(certArn); } /** * Removes tags from an AWS Certificate Manager (ACM) certificate. * * @param certArn the Amazon Resource Name (ARN) of the certificate from which to remove tags */ public static void removeTags(String certArn) { AcmClient acmClient = AcmClient.create(); List<Tag> expectedTags = List.of(Tag.builder().key("key").value("value").build()); RemoveTagsFromCertificateRequest req = RemoveTagsFromCertificateRequest.builder() .certificateArn(certArn) .tags(expectedTags) .build(); try { acmClient.removeTagsFromCertificate(req); System.out.println("Successfully removed tags from the certificate"); } catch (AcmException e) { System.err.println(e.getMessage()); } } }
-
Para obtener más información sobre la API, consulta RemoveTagsFromCertificatela Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar RenewCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 RenewCert { public static void main(String[] args) { final String usage = """ Usage: <certArn> Where: certArn - the ARN of the certificate. """; if (args.length != 1) { System.out.println(usage); return; } String certArn = args[0]; renewCertificate(certArn); } /** * Renews an existing SSL/TLS certificate in AWS Certificate Manager (ACM). * * @param certArn The Amazon Resource Name (ARN) of the certificate to be renewed. * @throws AcmException If there is an error renewing the certificate. */ public static void renewCertificate(String certArn) { AcmClient acmClient = AcmClient.create(); RenewCertificateRequest certificateRequest = RenewCertificateRequest.builder() .certificateArn(certArn) .build(); try { acmClient.renewCertificate(certificateRequest); System.out.println("The certificate was renewed"); } catch(AcmException e){ System.out.println(e.getMessage()); } } }
-
Para obtener más información sobre la API, consulta RenewCertificatela Referencia AWS SDK for Java 2.x de la API.
-
En el siguiente ejemplo de código, se muestra cómo utilizar RequestCertificate
.
- SDK para Java 2.x
-
nota
Hay más información al respecto GitHub. Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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()); } } }
-
Para obtener más información sobre la API, consulta RequestCertificatela Referencia AWS SDK for Java 2.x de la API.
-