

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

# 更新憑證的驗證方法
<a name="sdk-update"></a>

下列範例示範如何使用 [UpdateCertificateOptions](https://docs.aws.amazon.com/acm/latest/APIReference/API_UpdateCertificateOptions.html) 函數搭配 `ValidationMethod`選項，將現有的電子郵件驗證公有憑證遷移至 DNS 驗證。憑證 ARN 會在遷移期間保留。呼叫此函數後，請呼叫 [ListCertificateDomainValidations](https://docs.aws.amazon.com/acm/latest/APIReference/API_ListCertificateDomainValidations.html) 函數擷取 CNAME 記錄，並在 72 小時內將記錄新增至 DNS 組態。

```
package com.amazonaws.samples;

import com.amazonaws.AmazonClientException;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.regions.Regions;

import com.amazonaws.services.certificatemanager.AWSCertificateManagerClientBuilder;
import com.amazonaws.services.certificatemanager.AWSCertificateManager;

import com.amazonaws.services.certificatemanager.model.CertificateOptions;
import com.amazonaws.services.certificatemanager.model.UpdateCertificateOptionsRequest;
import com.amazonaws.services.certificatemanager.model.UpdateCertificateOptionsResult;

import com.amazonaws.services.certificatemanager.model.ConflictException;
import com.amazonaws.services.certificatemanager.model.InvalidArnException;
import com.amazonaws.services.certificatemanager.model.InvalidStateException;
import com.amazonaws.services.certificatemanager.model.LimitExceededException;
import com.amazonaws.services.certificatemanager.model.ResourceNotFoundException;
import com.amazonaws.services.certificatemanager.model.ValidationException;

public class UpdateCertificateOptions {

   public static void main(String[] args) throws Exception {

      // Retrieve your credentials from the C:\Users\name\.aws\credentials file in Windows
      // or the ~/.aws/credentials in Linux.
      AWSCredentials credentials = null;
      try {
          credentials = new ProfileCredentialsProvider().getCredentials();
      }
      catch (Exception ex) {
          throw new AmazonClientException("Cannot load your credentials from file.", ex);
      }

      // Create a client.
      AWSCertificateManager client = AWSCertificateManagerClientBuilder.standard()
              .withRegion(Regions.{{your_region}})
              .withCredentials(new AWSStaticCredentialsProvider(credentials))
              .build();

      // Build a CertificateOptions with ValidationMethod set to DNS, then create the
      // request. The certificate must be an issued public certificate that uses email
      // validation.
      CertificateOptions options = new CertificateOptions()
              .withValidationMethod("DNS");

      UpdateCertificateOptionsRequest req = new UpdateCertificateOptionsRequest()
              .withCertificateArn("arn:aws:acm:{{region}}:{{account}}:"
                      + "certificate/{{12345678-1234-1234-1234-123456789012}}")
              .withOptions(options);

      // Initiate the migration.
      UpdateCertificateOptionsResult result = null;
      try {
         result = client.updateCertificateOptions(req);
      }
      catch (ValidationException ex) {
         // Thrown when the certificate is not an issued public certificate,
         // or when the requested migration path is not supported (for example,
         // DNS to email).
         throw ex;
      }
      catch (ConflictException ex) {
         // Thrown when a migration is already in progress for this certificate.
         throw ex;
      }
      catch (InvalidArnException ex) {
         throw ex;
      }
      catch (InvalidStateException ex) {
         throw ex;
      }
      catch (LimitExceededException ex) {
         throw ex;
      }
      catch (ResourceNotFoundException ex) {
         throw ex;
      }

      // The response is empty on success. After this call returns, retrieve the CNAME
      // records by calling ListCertificateDomainValidations and add them to your DNS
      // configuration within 72 hours.
      System.out.println("Migration initiated. Retrieve CNAME records with "
              + "ListCertificateDomainValidations.");
   }
}
```