ACMejemplos que se utilizan SDK para Java 2.x - AWS SDKEjemplos de código

Hay más AWS SDK ejemplos disponibles en el GitHub repositorio de AWS Doc SDK Examples.

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.

ACMejemplos que se utilizan SDK para Java 2.x

Los siguientes ejemplos de código muestran cómo realizar acciones e implementar escenarios comunes mediante el uso del AWS SDK for Java 2.x withACM.

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 funciones de servicio individuales, es posible ver las acciones en contexto en los escenarios relacionados.

Cada ejemplo incluye un enlace al código fuente completo, donde puede encontrar instrucciones sobre cómo configurar y ejecutar el código en su contexto.

Acciones

En el siguiente ejemplo de código se muestra cómo usar AddTagsToCertificate.

SDKpara Java 2.x
nota

Hay más información. 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 API obtener más información, consulte AddTagsToCertificatela AWS SDK for Java 2.x APIReferencia.

En el siguiente ejemplo de código se muestra cómo usar DeleteCertificate.

SDKpara Java 2.x
nota

Hay más información. 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 API obtener más información, consulte DeleteCertificatela AWS SDK for Java 2.x APIReferencia.

En el siguiente ejemplo de código se muestra cómo usar DescribeCertificate.

SDKpara Java 2.x
nota

Hay más información. 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 API obtener más información, consulte DescribeCertificatela AWS SDK for Java 2.x APIReferencia.

En el siguiente ejemplo de código se muestra cómo usar ExportCertificate.

SDKpara Java 2.x
nota

Hay más información. 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 API obtener más información, consulte ExportCertificatela AWS SDK for Java 2.x APIReferencia.

En el siguiente ejemplo de código se muestra cómo usar ImportCertificate.

SDKpara Java 2.x
nota

Hay más información. 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 API obtener más información, consulte ImportCertificatela AWS SDK for Java 2.x APIReferencia.

En el siguiente ejemplo de código se muestra cómo usar ListCertificates.

SDKpara Java 2.x
nota

Hay más información. 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 API obtener más información, consulte ListCertificatesla AWS SDK for Java 2.x APIReferencia.

En el siguiente ejemplo de código se muestra cómo usar ListTagsForCertificate.

SDKpara Java 2.x
nota

Hay más información. 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()); }); } }

En el siguiente ejemplo de código se muestra cómo usar RemoveTagsFromCertificate.

SDKpara Java 2.x
nota

Hay más información. 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()); } } }

En el siguiente ejemplo de código se muestra cómo usar RenewCertificate.

SDKpara Java 2.x
nota

Hay más información. 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 API obtener más información, consulte RenewCertificatela AWS SDK for Java 2.x APIReferencia.

En el siguiente ejemplo de código se muestra cómo usar RequestCertificate.

SDKpara Java 2.x
nota

Hay más información. 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 API obtener más información, consulte RequestCertificatela AWS SDK for Java 2.x APIReferencia.