检查导入作业
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
检查 Cloud KMS 中导入作业的状态。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。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)."]]