Contoh ACM menggunakan SDK for Java 2.x - AWS Contoh Kode SDK

Ada lebih banyak contoh AWS SDK yang tersedia di repo Contoh SDK AWS Doc. GitHub

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Contoh ACM menggunakan SDK for Java 2.x

Contoh kode berikut menunjukkan cara melakukan tindakan dan mengimplementasikan skenario umum dengan menggunakan ACM AWS SDK for Java 2.x with.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanAddTagsToCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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()); } } }

Contoh kode berikut menunjukkan cara menggunakanDeleteCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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()); } } }

Contoh kode berikut menunjukkan cara menggunakanDescribeCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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()); } } }

Contoh kode berikut menunjukkan cara menggunakanExportCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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"); } }

Contoh kode berikut menunjukkan cara menggunakanImportCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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); } } }

Contoh kode berikut menunjukkan cara menggunakanListCertificates.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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()); } } }
  • Untuk detail API, lihat ListCertificatesdi Referensi AWS SDK for Java 2.x API.

Contoh kode berikut menunjukkan cara menggunakanListTagsForCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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()); }); } }

Contoh kode berikut menunjukkan cara menggunakanRemoveTagsFromCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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()); } } }

Contoh kode berikut menunjukkan cara menggunakanRenewCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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()); } } }
  • Untuk detail API, lihat RenewCertificatedi Referensi AWS SDK for Java 2.x API.

Contoh kode berikut menunjukkan cara menggunakanRequestCertificate.

SDK untuk Java 2.x
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode 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()); } } }