檢查匯入工作
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Cloud KMS 中查看匯入工作的狀態。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
Go
如要瞭解如何安裝及使用 Cloud KMS 的用戶端程式庫,請參閱這篇文章。
如要向 Cloud KMS 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Java
如要瞭解如何安裝及使用 Cloud KMS 的用戶端程式庫,請參閱這篇文章。
如要向 Cloud KMS 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Node.js
如要瞭解如何安裝及使用 Cloud KMS 的用戶端程式庫,請參閱這篇文章。
如要向 Cloud KMS 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
Python
如要瞭解如何安裝及使用 Cloud KMS 的用戶端程式庫,請參閱這篇文章。
如要向 Cloud KMS 進行驗證,請設定應用程式預設憑證。
詳情請參閱「為本機開發環境設定驗證」。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Check import job\n\nCheck the state of an import job in Cloud KMS.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Import a key version into Cloud KMS](/kms/docs/importing-a-key)\n\nCode sample\n-----------\n\n### Go\n\n\nTo learn how to install and use the client library for Cloud KMS, see\n[Cloud KMS client libraries](/kms/docs/reference/libraries).\n\n\nTo authenticate to Cloud KMS, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tkms \"cloud.google.com/go/kms/apiv1\"\n \t\"cloud.google.com/go/kms/apiv1/kmspb\"\n )\n\n // checkStateImportedKey checks the state of a CryptoKeyVersion in KMS.\n func checkStateImportedKey(w io.Writer, name string) error {\n \t// name := \"projects/PROJECT_ID/locations/global/keyRings/my-key-ring/cryptoKeys/my-imported-key/cryptoKeyVersions/1\"\n\n \t// Create the client.\n \tctx := context.Background()\n \tclient, err := kms.https://cloud.google.com/go/docs/reference/cloud.google.com/go/kms/latest/apiv1.html#cloud_google_com_go_kms_apiv1_KeyManagementClient_NewKeyManagementClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to create kms client: %w\", err)\n \t}\n \tdefer client.Close()\n\n \t// Call the API.\n \tresult, err := client.GetCryptoKeyVersion(ctx, &kmspb.GetCryptoKeyVersionRequest{\n \t\tName: name,\n \t})\n \tif err != nil {\n \t\treturn fmt.Errorf(\"failed to get crypto key version: %w\", err)\n \t}\n \tfmt.Fprintf(w, \"Current state of crypto key version %q: %s\\n\", result.https://cloud.google.com/go/docs/reference/cloud.google.com/go/kms/latest/apiv1.html#cloud_google_com_go_kms_apiv1_CreateKeyHandleOperation_Name, result.State)\n \treturn nil\n }\n\n### Java\n\n\nTo learn how to install and use the client library for Cloud KMS, see\n[Cloud KMS client libraries](/kms/docs/reference/libraries).\n\n\nTo authenticate to Cloud KMS, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.kms.v1.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.CryptoKeyVersion.html;\n import com.google.cloud.kms.v1.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.CryptoKeyVersionName.html;\n import com.google.cloud.kms.v1.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.KeyManagementServiceClient.html;\n import java.io.IOException;\n\n public class CheckStateImportedKey {\n\n public void checkStateImportedKey() throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n String projectId = \"your-project-id\";\n String locationId = \"us-east1\";\n String keyRingId = \"my-key-ring\";\n String cryptoKeyId = \"my-crypto-key\";\n String cryptoKeyVersionId = \"1\";\n checkStateImportedKey(projectId, locationId, keyRingId, cryptoKeyId, cryptoKeyVersionId);\n }\n\n // Check the state of an imported key in Cloud KMS.\n public void checkStateImportedKey(\n String projectId,\n String locationId,\n String keyRingId,\n String cryptoKeyId,\n String cryptoKeyVersionId)\n throws IOException {\n // Initialize client that will be used to send requests. This client only\n // needs to be created once, and can be reused for multiple requests. After\n // completing all of your requests, call the \"close\" method on the client to\n // safely clean up any remaining background resources.\n try (https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.KeyManagementServiceClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.KeyManagementServiceClient.html.create()) {\n // Build the version name from its path components.\n https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.CryptoKeyVersionName.html versionName =\n https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.CryptoKeyVersionName.html.of(\n projectId, locationId, keyRingId, cryptoKeyId, cryptoKeyVersionId);\n\n // Retrieve the state of an existing version.\n https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.CryptoKeyVersion.html version = client.getCryptoKeyVersion(versionName);\n System.out.printf(\n \"Current state of crypto key version %s: %s%n\", version.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.CryptoKeyVersion.html#com_google_cloud_kms_v1_CryptoKeyVersion_getName__(), version.https://cloud.google.com/java/docs/reference/google-cloud-kms/latest/com.google.cloud.kms.v1.CryptoKeyVersion.html#com_google_cloud_kms_v1_CryptoKeyVersion_getState__());\n }\n }\n }\n\n### Node.js\n\n\nTo learn how to install and use the client library for Cloud KMS, see\n[Cloud KMS client libraries](/kms/docs/reference/libraries).\n\n\nTo authenticate to Cloud KMS, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n //\n // TODO(developer): Uncomment these variables before running the sample.\n //\n // const projectId = 'my-project';\n // const locationId = 'us-east1';\n // const keyRingId = 'my-key-ring';\n // const cryptoKeyId = 'my-imported-key';\n // const cryptoKeyVersionId = '1';\n\n // Imports the Cloud KMS library\n const {KeyManagementServiceClient} = require('https://cloud.google.com/nodejs/docs/reference/kms/latest/overview.html');\n\n // Instantiates a client\n const client = new https://cloud.google.com/nodejs/docs/reference/kms/latest/overview.html();\n\n // Build the key version name\n const keyVersionName = client.cryptoKeyVersionPath(\n projectId,\n locationId,\n keyRingId,\n cryptoKeyId,\n cryptoKeyVersionId\n );\n\n async function checkStateCryptoKeyVersion() {\n const [keyVersion] = await client.getCryptoKeyVersion({\n name: keyVersionName,\n });\n\n console.log(\n `Current state of key version ${keyVersion.name}: ${keyVersion.state}`\n );\n return keyVersion;\n }\n\n return checkStateCryptoKeyVersion();\n\n### Python\n\n\nTo learn how to install and use the client library for Cloud KMS, see\n[Cloud KMS client libraries](/kms/docs/reference/libraries).\n\n\nTo authenticate to Cloud KMS, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import kms\n\n\n def check_state_imported_key(\n project_id: str, location_id: str, key_ring_id: str, import_job_id: str\n ) -\u003e None:\n \"\"\"\n Check the state of an import job in Cloud KMS.\n\n Args:\n project_id (string): Google Cloud project ID (e.g. 'my-project').\n location_id (string): Cloud KMS location (e.g. 'us-east1').\n key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').\n import_job_id (string): ID of the import job (e.g. 'my-import-job').\n \"\"\"\n\n # Create the client.\n client = kms.https://cloud.google.com/python/docs/reference/cloudkms/latest/google.cloud.kms_v1.services.key_management_service.KeyManagementServiceClient.html()\n\n # Retrieve the fully-qualified import_job string.\n import_job_name = client.https://cloud.google.com/python/docs/reference/cloudkms/latest/google.cloud.kms_v1.services.key_management_service.KeyManagementServiceClient.html#google_cloud_kms_v1_services_key_management_service_KeyManagementServiceClient_import_job_path(\n project_id, location_id, key_ring_id, import_job_id\n )\n\n # Retrieve the state from an existing import job.\n import_job = client.https://cloud.google.com/python/docs/reference/cloudkms/latest/google.cloud.kms_v1.services.key_management_service.KeyManagementServiceClient.html#google_cloud_kms_v1_services_key_management_service_KeyManagementServiceClient_get_import_job(name=import_job_name)\n\n print(f\"Current state of import job {import_job.name}: {import_job.state}\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=kms)."]]